2727import com .nextcloud .client .account .User ;
2828import com .nextcloud .client .database .NextcloudDatabase ;
2929import com .nextcloud .client .database .dao .UploadDao ;
30+ import com .nextcloud .client .database .entity .UploadEntity ;
31+ import com .nextcloud .client .database .entity .UploadEntityKt ;
3032import com .nextcloud .client .jobs .upload .FileUploadHelper ;
3133import com .nextcloud .client .jobs .upload .FileUploadWorker ;
3234import com .nextcloud .utils .autoRename .AutoRename ;
@@ -189,9 +191,22 @@ private ContentValues getContentValues(OCUpload ocUpload) {
189191 * @param ocUpload Upload object with state to update
190192 * @return num of updated uploads.
191193 */
192- public int updateUpload (OCUpload ocUpload ) {
194+ public synchronized int updateUpload (OCUpload ocUpload ) {
193195 Log_OC .v (TAG , "Updating " + ocUpload .getLocalPath () + " with status=" + ocUpload .getUploadStatus ());
194196
197+ OCUpload existingUpload = getUploadById (ocUpload .getUploadId ());
198+ if (existingUpload == null ) {
199+ Log_OC .e (TAG , "Upload not found for ID: " + ocUpload .getUploadId ());
200+ return 0 ;
201+ }
202+
203+ if (!existingUpload .getAccountName ().equals (ocUpload .getAccountName ())) {
204+ Log_OC .e (TAG , "Account mismatch for upload ID " + ocUpload .getUploadId () +
205+ ": expected " + existingUpload .getAccountName () +
206+ ", got " + ocUpload .getAccountName ());
207+ return 0 ;
208+ }
209+
195210 ContentValues cv = new ContentValues ();
196211 cv .put (ProviderTableMeta .UPLOADS_LOCAL_PATH , ocUpload .getLocalPath ());
197212 cv .put (ProviderTableMeta .UPLOADS_REMOTE_PATH , ocUpload .getRemotePath ());
@@ -204,8 +219,8 @@ public int updateUpload(OCUpload ocUpload) {
204219
205220 int result = getDB ().update (ProviderTableMeta .CONTENT_URI_UPLOADS ,
206221 cv ,
207- ProviderTableMeta ._ID + "=?" ,
208- new String []{String .valueOf (ocUpload .getUploadId ())}
222+ ProviderTableMeta ._ID + "=? AND " + ProviderTableMeta . UPLOADS_ACCOUNT_NAME + "=? " ,
223+ new String []{String .valueOf (ocUpload .getUploadId ()), ocUpload . getAccountName () }
209224 );
210225
211226 Log_OC .d (TAG , "updateUpload returns with: " + result + " for file: " + ocUpload .getLocalPath ());
@@ -437,6 +452,20 @@ OCUpload getUploadById(long id) {
437452 return result ;
438453 }
439454
455+ public List <OCUpload > getUploadsByIds (long [] uploadIds , String accountName ) {
456+ final List <OCUpload > result = new ArrayList <>();
457+
458+ final List <UploadEntity > entities = uploadDao .getUploadsByIds (uploadIds , accountName );
459+ entities .forEach (uploadEntity -> {
460+ OCUpload ocUpload = createOCUploadFromEntity (uploadEntity );
461+ if (ocUpload != null ) {
462+ result .add (ocUpload );
463+ }
464+ });
465+
466+ return result ;
467+ }
468+
440469 private OCUpload [] getUploads (@ Nullable String selection , @ Nullable String ... selectionArgs ) {
441470 final List <OCUpload > uploads = new ArrayList <>();
442471 long page = 0 ;
@@ -555,6 +584,15 @@ private List<OCUpload> getUploadPage(long limit, final long afterId, final boole
555584 return uploads ;
556585 }
557586
587+ @ Nullable
588+ private OCUpload createOCUploadFromEntity (UploadEntity entity ) {
589+ if (entity == null ) {
590+ return null ;
591+ }
592+ initOCCapability ();
593+ return UploadEntityKt .toOCUpload (entity , capability );
594+ }
595+
558596 private OCUpload createOCUploadFromCursor (Cursor c ) {
559597 initOCCapability ();
560598
@@ -610,16 +648,6 @@ public long[] getCurrentUploadIds(final @NonNull String accountName) {
610648 .toArray ();
611649 }
612650
613- /**
614- * Gets a page of uploads after <code>afterId</code>, where uploads are sorted by ascending upload id.
615- * <p>
616- * If <code>afterId</code> is -1, returns the first page
617- */
618- public List <OCUpload > getCurrentAndPendingUploadsForAccountPageAscById (final long afterId , final @ NonNull String accountName ) {
619- final String selection = getInProgressAndDelayedUploadsSelection ();
620- return getUploadPage (QUERY_PAGE_SIZE , afterId , false , selection , accountName );
621- }
622-
623651 /**
624652 * Get all failed uploads.
625653 */
@@ -656,13 +684,6 @@ public OCUpload[] getCancelledUploadsForCurrentAccount() {
656684 ProviderTableMeta .UPLOADS_ACCOUNT_NAME + IS_EQUAL , user .getAccountName ());
657685 }
658686
659- /**
660- * Get all uploads which where successfully completed.
661- */
662- public OCUpload [] getFinishedUploads () {
663- return getUploads (ProviderTableMeta .UPLOADS_STATUS + EQUAL + UploadStatus .UPLOAD_SUCCEEDED .value , (String []) null );
664- }
665-
666687 public OCUpload [] getFailedButNotDelayedUploadsForCurrentAccount () {
667688 User user = currentAccountProvider .getUser ();
668689
@@ -679,25 +700,6 @@ public OCUpload[] getFailedButNotDelayedUploadsForCurrentAccount() {
679700 user .getAccountName ());
680701 }
681702
682- /**
683- * Get all failed uploads, except for those that were not performed due to lack of Wifi connection.
684- *
685- * @return Array of failed uploads, except for those that were not performed due to lack of Wifi connection.
686- */
687- public OCUpload [] getFailedButNotDelayedUploads () {
688-
689- return getUploads (ProviderTableMeta .UPLOADS_STATUS + EQUAL + UploadStatus .UPLOAD_FAILED .value + AND +
690- ProviderTableMeta .UPLOADS_LAST_RESULT + ANGLE_BRACKETS + UploadResult .LOCK_FAILED .getValue () +
691- AND + ProviderTableMeta .UPLOADS_LAST_RESULT +
692- ANGLE_BRACKETS + UploadResult .DELAYED_FOR_WIFI .getValue () +
693- AND + ProviderTableMeta .UPLOADS_LAST_RESULT +
694- ANGLE_BRACKETS + UploadResult .DELAYED_FOR_CHARGING .getValue () +
695- AND + ProviderTableMeta .UPLOADS_LAST_RESULT +
696- ANGLE_BRACKETS + UploadResult .DELAYED_IN_POWER_SAVE_MODE .getValue (),
697- (String []) null
698- );
699- }
700-
701703 private ContentResolver getDB () {
702704 return contentResolver ;
703705 }
@@ -823,39 +825,6 @@ public void updateDatabaseUploadStart(UploadFileOperation upload) {
823825 );
824826 }
825827
826- /**
827- * Changes the status of any in progress upload from UploadStatus.UPLOAD_IN_PROGRESS to UploadStatus.UPLOAD_FAILED
828- *
829- * @return Number of uploads which status was changed.
830- */
831- public int failInProgressUploads (UploadResult fail ) {
832- Log_OC .v (TAG , "Updating state of any killed upload" );
833-
834- ContentValues cv = new ContentValues ();
835- cv .put (ProviderTableMeta .UPLOADS_STATUS , UploadStatus .UPLOAD_FAILED .getValue ());
836- cv .put (
837- ProviderTableMeta .UPLOADS_LAST_RESULT ,
838- fail != null ? fail .getValue () : UploadResult .UNKNOWN .getValue ()
839- );
840- cv .put (ProviderTableMeta .UPLOADS_UPLOAD_END_TIMESTAMP , Calendar .getInstance ().getTimeInMillis ());
841-
842- int result = getDB ().update (
843- ProviderTableMeta .CONTENT_URI_UPLOADS ,
844- cv ,
845- ProviderTableMeta .UPLOADS_STATUS + "=?" ,
846- new String []{String .valueOf (UploadStatus .UPLOAD_IN_PROGRESS .getValue ())}
847- );
848-
849- if (result == 0 ) {
850- Log_OC .v (TAG , "No upload was killed" );
851- } else {
852- Log_OC .w (TAG , Integer .toString (result ) + " uploads where abruptly interrupted" );
853- notifyObserversNow ();
854- }
855-
856- return result ;
857- }
858-
859828 @ VisibleForTesting
860829 public void removeAllUploads () {
861830 Log_OC .v (TAG , "Delete all uploads!" );
0 commit comments