Skip to content

Commit 3c7e464

Browse files
committed
Override the implementation of file move / rename operation in mongodb.
1 parent 6269438 commit 3c7e464

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

src/main/java/picoded/dstack/mongodb/MongoDB_FileWorkspaceMap.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,40 @@ public boolean backend_fileExist(String oid, String filepath) {
695695
// Check against the full file path
696696
return fullRawPathExist(oid + "/" + filepath);
697697
}
698+
699+
/**
700+
* Move or rename a file.
701+
* Note that in MongoDB GridFS, there is no native "move" or "rename" operation, so we perform this by updating the "filename" metadata of the file.
702+
* @param oid ObjectID of workspace
703+
* @param sourceFile the current file path to move from
704+
* @param destinationFile the new file path to move to
705+
*/
706+
@Override
707+
public void backend_moveFile(String oid, String sourceFile, String destinationFile) {
708+
709+
// in mongodb gridFS, "move" is performed by updating the "filename" metadata
710+
711+
// first, check that the source file exists
712+
if(!backend_fileExist(oid, sourceFile)){
713+
throw new RuntimeException("Source file does not exist: " + sourceFile);
714+
}
715+
716+
// then, ensure that file at destination does not exists
717+
if(backend_fileExist(oid, destinationFile)){
718+
throw new RuntimeException("Destination file already exists: " + destinationFile);
719+
}
720+
721+
// finally, perform the "move" by updating the filename metadata of the source file to the destination file
722+
723+
String sourceFullPath = oid + "/" + sourceFile;
724+
String destinationFullPath = oid + "/" + destinationFile;
725+
726+
Bson query = Filters.eq("filename", sourceFullPath);
727+
Bson update = new Document("$set", new Document("filename", destinationFullPath));
728+
729+
gridFSBucket.getFilesCollection().updateOne(query, update);
730+
731+
}
698732

699733
@Override
700734
public void backend_removeFile(String oid, String filepath) {
@@ -704,7 +738,7 @@ public void backend_removeFile(String oid, String filepath) {
704738
ensureParentPath(oid, filepath);
705739
// Remove the respective file
706740
removeFilePath(oid, filepath);
707-
}
741+
}
708742

709743
// Folder Pathing support
710744
//--------------------------------------------------------------------------

0 commit comments

Comments
 (0)