Skip to content

Commit

Permalink
Clear old UploadsStorageManager code
Browse files Browse the repository at this point in the history
  • Loading branch information
JuancaG05 committed Jul 14, 2022
1 parent dc694f5 commit fcf8489
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 198 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,42 +94,6 @@ public UploadsStorageManager(ContentResolver contentResolver) {
mContentResolver = contentResolver;
}

/**
* Stores an upload object in DB.
*
* @param ocUpload Upload object to store
* @return upload id, -1 if the insert process fails.
*/
public long storeUpload(OCUpload ocUpload) {
Timber.v("Inserting " + ocUpload.getLocalPath() + " with status=" + ocUpload.getUploadStatus());

ContentValues cv = new ContentValues();
cv.put(ProviderTableMeta.UPLOADS_LOCAL_PATH, ocUpload.getLocalPath());
cv.put(ProviderTableMeta.UPLOADS_REMOTE_PATH, ocUpload.getRemotePath());
cv.put(ProviderTableMeta.UPLOADS_ACCOUNT_NAME, ocUpload.getAccountName());
cv.put(ProviderTableMeta.UPLOADS_FILE_SIZE, ocUpload.getFileSize());
cv.put(ProviderTableMeta.UPLOADS_STATUS, ocUpload.getUploadStatus().value);
cv.put(ProviderTableMeta.UPLOADS_LOCAL_BEHAVIOUR, ocUpload.getLocalAction());
cv.put(ProviderTableMeta.UPLOADS_FORCE_OVERWRITE, ocUpload.isForceOverwrite() ? 1 : 0);
cv.put(ProviderTableMeta.UPLOADS_IS_CREATE_REMOTE_FOLDER, ocUpload.createsRemoteFolder() ? 1 : 0);
cv.put(ProviderTableMeta.UPLOADS_LAST_RESULT, ocUpload.getLastResult().getValue());
cv.put(ProviderTableMeta.UPLOADS_CREATED_BY, ocUpload.getCreatedBy());
cv.put(ProviderTableMeta.UPLOADS_TRANSFER_ID, ocUpload.getTransferId());

Uri result = getDB().insert(ProviderTableMeta.CONTENT_URI_UPLOADS, cv);

Timber.d("storeUpload returns with: " + result + " for file: " + ocUpload.getLocalPath());
if (result == null) {
Timber.e("Failed to insert item " + ocUpload.getLocalPath() + " into upload db.");
return -1;
} else {
long new_id = Long.parseLong(result.getPathSegments().get(1));
ocUpload.setUploadId(new_id);
notifyObserversNow();
return new_id;
}
}

/**
* Update an upload object in DB.
*
Expand Down Expand Up @@ -164,63 +128,6 @@ public int updateUpload(OCUpload ocUpload) {
return result;
}

private int updateUploadInternal(Cursor c, UploadStatus status, UploadResult result, String remotePath,
String localPath) {
int r = 0;
while (c.moveToNext()) {
// read upload object and update
OCUpload upload = createOCUploadFromCursor(c);

String path = getStringFromColumnOrThrow(c, ProviderTableMeta.UPLOADS_LOCAL_PATH);
Timber.v("Updating " + path + " with status:" + status + " and result:" + (result == null ? "null" :
result.toString()) + " (old:" + upload.toFormattedString() + ")");

upload.setUploadStatus(status);
upload.setLastResult(result);
upload.setRemotePath(remotePath);
if (localPath != null) {
upload.setLocalPath(localPath);
}
upload.setUploadEndTimestamp(Calendar.getInstance().getTimeInMillis());

// store update upload object to db
r = updateUpload(upload);
}

return r;
}

/**
* Update upload status of file uniquely referenced by id.
*
* @param id upload id.
* @param status new status.
* @param result new result of upload operation
* @param remotePath path of the file to upload in the ownCloud storage
* @param localPath path of the file to upload in the device storage
* @return 1 if file status was updated, else 0.
*/
public int updateUploadStatus(long id, UploadStatus status, UploadResult result, String remotePath,
String localPath) {
int returnValue = 0;
Cursor c = getDB().query(
ProviderTableMeta.CONTENT_URI_UPLOADS,
null,
ProviderTableMeta._ID + "=?",
new String[]{String.valueOf(id)},
null
);

if (c.getCount() != 1) {
Timber.e(c.getCount() + " items for id=" + id
+ " available in UploadDb. Expected 1. Failed to update upload db.");
} else {
returnValue = updateUploadInternal(c, status, result, remotePath, localPath);
}
c.close();
return returnValue;
}

/**
* Should be called when some value of this DB was changed. All observers
* are informed.
Expand All @@ -231,64 +138,6 @@ public void notifyObserversNow() {
notifyObservers();
}

/**
* Remove an upload from the uploads list, known its target account and remote path.
*
* @param upload Upload instance to remove from persisted storage.
* @return true when the upload was stored and could be removed.
*/
public int removeUpload(OCUpload upload) {
int result = getDB().delete(
ProviderTableMeta.CONTENT_URI_UPLOADS,
ProviderTableMeta._ID + "=?",
new String[]{Long.toString(upload.getUploadId())}
);
Timber.d("delete returns " + result + " for upload " + upload);
if (result > 0) {
notifyObserversNow();
}
return result;
}

/**
* Remove an upload from the uploads list, known its target account and remote path.
*
* @param accountName Name of the OC account target of the upload to remove.
* @param remotePath Absolute path in the OC account target of the upload to remove.
* @return true when one or more upload entries were removed
*/
public int removeUpload(String accountName, String remotePath) {
int result = getDB().delete(
ProviderTableMeta.CONTENT_URI_UPLOADS,
ProviderTableMeta.UPLOADS_ACCOUNT_NAME + "=? AND " + ProviderTableMeta.UPLOADS_REMOTE_PATH + "=?",
new String[]{accountName, remotePath}
);
Timber.d("delete returns " + result + " for file " + remotePath + " in " + accountName);
if (result > 0) {
notifyObserversNow();
}
return result;
}

/**
* Remove all the uploads of a given account from the uploads list.
*
* @param accountName Name of the OC account target of the uploads to remove.
* @return true when one or more upload entries were removed
*/
public int removeUploads(String accountName) {
int result = getDB().delete(
ProviderTableMeta.CONTENT_URI_UPLOADS,
ProviderTableMeta.UPLOADS_ACCOUNT_NAME + "=?",
new String[]{accountName}
);
Timber.d("delete returns " + result + " for uploads in " + accountName);
if (result > 0) {
notifyObserversNow();
}
return result;
}

public OCUpload[] getAllStoredUploads() {
return getUploads(null, null, null);
}
Expand Down Expand Up @@ -368,19 +217,6 @@ public OCUpload[] getCurrentAndPendingUploads() {
);
}

/**
* Get all failed uploads.
*/
public OCUpload[] getFailedUploads() {
return getUploads(
ProviderTableMeta.UPLOADS_STATUS + "== ?",
new String[]{
String.valueOf(UploadStatus.UPLOAD_FAILED.value)
},
null
);
}

/**
* Get all uploads which where successfully completed.
*/
Expand Down Expand Up @@ -442,37 +278,4 @@ public long clearSuccessfulUploads() {
}
return result;
}

/**
* Changes the status of any in progress upload from UploadStatus.UPLOAD_IN_PROGRESS
* to UploadStatus.UPLOAD_FAILED
*
* @return Number of uploads which status was changed.
*/
public int failInProgressUploads(UploadResult uploadResult) {
Timber.v("Updating state of any killed upload");

ContentValues cv = new ContentValues();
cv.put(ProviderTableMeta.UPLOADS_STATUS, UploadStatus.UPLOAD_FAILED.getValue());
cv.put(
ProviderTableMeta.UPLOADS_LAST_RESULT,
uploadResult != null ? uploadResult.getValue() : UploadResult.UNKNOWN.getValue()
);
cv.put(ProviderTableMeta.UPLOADS_UPLOAD_END_TIMESTAMP, Calendar.getInstance().getTimeInMillis());

int result = getDB().update(
ProviderTableMeta.CONTENT_URI_UPLOADS,
cv,
ProviderTableMeta.UPLOADS_STATUS + "=?",
new String[]{String.valueOf(UploadStatus.UPLOAD_IN_PROGRESS.getValue())}
);

if (result == 0) {
Timber.v("No upload was killed");
} else {
Timber.w("%s uploads where abruptly interrupted", result);
notifyObserversNow();
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,8 @@ private View getView(OCUpload[] uploadsItems, int position, View convertView, Vi
rightButton.setImageResource(R.drawable.ic_action_delete_grey);
rightButton.setVisibility(View.VISIBLE);
rightButton.setOnClickListener(v -> {
mUploadsStorageManager.removeUpload(upload);
OCTransferRepository transferRepository = new OCTransferRepository(new OCLocalTransferDataSource(OwncloudDatabase.Companion.getDatabase(v.getContext()).transferDao()));
transferRepository.removeTransferById(upload.getUploadId());
refreshView();
});

Expand Down

0 comments on commit fcf8489

Please sign in to comment.