1616import java .sql .Date ;
1717
1818// JavaCommons imports
19+ import com .mongodb .client .gridfs .model .GridFSDownloadOptions ;
1920import picoded .core .common .EmptyArray ;
2021import picoded .core .file .FileUtil ;
2122import picoded .dstack .FileWorkspace ;
@@ -502,21 +503,15 @@ public void backend_fileWrite(String oid, String filepath, byte[] data) {
502503
503504 // Lets get the time "NOW"
504505 long now = System .currentTimeMillis ();
505-
506- // Lets build the query for the file involved
507- Bson query = Filters .eq ("filename" , fullPath );
508-
506+
507+ GridFSFile fileObj = getLatestCopy (fullPath );
508+
509509 // Read timestamp, and objectid
510510 ObjectId readObjId = null ;
511511 long readUploadTimestamp = -1 ;
512-
513- // Lets iterate the search result, and return true on an item
514- try (MongoCursor <GridFSFile > cursor = gridFSBucket .find (query ).limit (1 ).iterator ()) {
515- if (cursor .hasNext ()) {
516- GridFSFile fileObj = cursor .next ();
517- readUploadTimestamp = fileObj .getUploadDate ().getTime ();
518- readObjId = fileObj .getObjectId ();
519- }
512+ if (fileObj != null ) {
513+ readUploadTimestamp = fileObj .getUploadDate ().getTime ();
514+ readObjId = fileObj .getObjectId ();
520515 }
521516
522517 // Check if the current file is less then 2 seconds old
@@ -529,12 +524,10 @@ public void backend_fileWrite(String oid, String filepath, byte[] data) {
529524 }
530525
531526 // And get the latest objectID again (in case of any changes)
532- try (MongoCursor <GridFSFile > cursor = gridFSBucket .find (query ).limit (1 ).iterator ()) {
533- if (cursor .hasNext ()) {
534- GridFSFile fileObj = cursor .next ();
535- readUploadTimestamp = fileObj .getUploadDate ().getTime ();
536- readObjId = fileObj .getObjectId ();
537- }
527+ fileObj = getLatestCopy (fullPath );
528+ if (fileObj != null ) {
529+ readUploadTimestamp = fileObj .getUploadDate ().getTime ();
530+ readObjId = fileObj .getObjectId ();
538531 }
539532 }
540533
@@ -854,22 +847,10 @@ public long backend_createdTimestamp(final String oid, final String filepath) {
854847 * @return DataObject created timestamp in ms
855848 */
856849 public long backend_modifiedTimestamp (final String oid , final String filepath ) {
857- // Lets build the query for the "root file"
858- Bson query = Filters .eq ("filename" , oid + "/" + filepath );
859-
860- // Lets prepare the search
861- GridFSFindIterable search = gridFSBucket .find (query )
862- // GridFS uses an index on the files collection using the filename and uploadDate fields.
863- // Make sure to sort the query by uploadDate descending (-1) to ensure that we get the latest file.
864- .sort ((new Document ()).append ("uploadDate" , -1 /* descending*/ ))
865- .limit (1 );
866850
867- // Lets iterate the search result, and return true on an item
868- try (MongoCursor <GridFSFile > cursor = search .iterator ()) {
869- if (cursor .hasNext ()) {
870- GridFSFile fileObj = cursor .next ();
871- return fileObj .getUploadDate ().getTime ();
872- }
851+ GridFSFile file = getLatestCopy (oid + "/" + filepath );
852+ if (file != null ){
853+ return file .getUploadDate ().getTime ();
873854 }
874855
875856 // Fail, as the search found no iterations
@@ -881,7 +862,29 @@ public long backend_modifiedTimestamp(final String oid, final String filepath) {
881862 // Query, and listing support
882863 //
883864 //--------------------------------------------------------------------------
884-
865+
866+ /**
867+ * Get the latest copy of a file
868+ * @param fullPath the full path of the file
869+ * @return the latest copy of the file
870+ */
871+ private GridFSFile getLatestCopy (String fullPath ){
872+
873+ GridFSFindIterable search = gridFSBucket
874+ .find (Filters .eq ("filename" , fullPath ))
875+ // GridFS uses an index on the files collection using the filename and uploadDate fields.
876+ // Make sure to sort the query by uploadDate descending (-1) to ensure that we get the latest file.
877+ .sort ((new Document ()).append ("uploadDate" , -1 /* descending*/ ))
878+ .limit (1 );
879+
880+ MongoCursor <GridFSFile > cursor = search .iterator ();
881+ if (cursor .hasNext ()){
882+ return cursor .next ();
883+ }
884+ return null ;
885+
886+ }
887+
885888 /**
886889 * List all the various files and folders found in the given folderPath
887890 *
0 commit comments