@@ -502,21 +502,15 @@ public void backend_fileWrite(String oid, String filepath, byte[] data) {
502502
503503 // Lets get the time "NOW"
504504 long now = System .currentTimeMillis ();
505-
506- // Lets build the query for the file involved
507- Bson query = Filters .eq ("filename" , fullPath );
508-
505+
506+ GridFSFile fileObj = getLatestCopy (fullPath );
507+
509508 // Read timestamp, and objectid
510509 ObjectId readObjId = null ;
511510 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- }
511+ if (fileObj != null ) {
512+ readUploadTimestamp = fileObj .getUploadDate ().getTime ();
513+ readObjId = fileObj .getObjectId ();
520514 }
521515
522516 // Check if the current file is less then 2 seconds old
@@ -529,12 +523,10 @@ public void backend_fileWrite(String oid, String filepath, byte[] data) {
529523 }
530524
531525 // 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- }
526+ fileObj = getLatestCopy (fullPath );
527+ if (fileObj != null ) {
528+ readUploadTimestamp = fileObj .getUploadDate ().getTime ();
529+ readObjId = fileObj .getObjectId ();
538530 }
539531 }
540532
@@ -854,22 +846,10 @@ public long backend_createdTimestamp(final String oid, final String filepath) {
854846 * @return DataObject created timestamp in ms
855847 */
856848 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 );
866849
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- }
850+ GridFSFile file = getLatestCopy (oid + "/" + filepath );
851+ if (file != null ){
852+ return file .getUploadDate ().getTime ();
873853 }
874854
875855 // Fail, as the search found no iterations
@@ -881,7 +861,29 @@ public long backend_modifiedTimestamp(final String oid, final String filepath) {
881861 // Query, and listing support
882862 //
883863 //--------------------------------------------------------------------------
884-
864+
865+ /**
866+ * Get the latest copy of a file
867+ * @param fullPath the full path of the file
868+ * @return the latest copy of the file
869+ */
870+ private GridFSFile getLatestCopy (String fullPath ){
871+
872+ GridFSFindIterable search = gridFSBucket
873+ .find (Filters .eq ("filename" , fullPath ))
874+ // GridFS uses an index on the files collection using the filename and uploadDate fields.
875+ // Make sure to sort the query by uploadDate descending (-1) to ensure that we get the latest file.
876+ .sort ((new Document ()).append ("uploadDate" , -1 /* descending*/ ))
877+ .limit (1 );
878+
879+ MongoCursor <GridFSFile > cursor = search .iterator ();
880+ if (cursor .hasNext ()){
881+ return cursor .next ();
882+ }
883+ return null ;
884+
885+ }
886+
885887 /**
886888 * List all the various files and folders found in the given folderPath
887889 *
0 commit comments