Handle HTTP request returned status code 412 (Precondition Failed) for session uploads (#227)

* Handle HTTP request returned status code 412 (Precondition Failed) for session uploads to OneDrive Personal Accounts
* Fix Failed to remove file /root/.config/onedrive/resume_upload: No such file or directory if there is a session upload error and the resume file does not get created
* Handle response codes when using 2 different systems using --upload-only but the same OneDrive account and uploading the same filename to the same location
This commit is contained in:
abraunegg 2018-11-11 11:15:48 +11:00 committed by GitHub
parent 11e477f045
commit 86ea576144
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 9 deletions

View file

@ -977,6 +977,14 @@ final class SyncEngine
try { try {
response = onedrive.simpleUploadReplace(path, item.driveId, item.id, item.eTag); response = onedrive.simpleUploadReplace(path, item.driveId, item.id, item.eTag);
} catch (OneDriveException e) { } catch (OneDriveException e) {
if (e.httpStatusCode == 404) {
// HTTP request returned status code 404 - the eTag provided does not exist
// Delete record from the local database - file will be uploaded as a new file
log.vlog("OneDrive returned a 'HTTP 404 - eTag Issue' - gracefully handling error");
itemdb.deleteById(item.driveId, item.id);
return;
}
// Resolve https://github.com/abraunegg/onedrive/issues/36 // Resolve https://github.com/abraunegg/onedrive/issues/36
if ((e.httpStatusCode == 409) || (e.httpStatusCode == 423)) { if ((e.httpStatusCode == 409) || (e.httpStatusCode == 423)) {
// The file is currently checked out or locked for editing by another user // The file is currently checked out or locked for editing by another user
@ -990,10 +998,10 @@ final class SyncEngine
if (e.httpStatusCode == 412) { if (e.httpStatusCode == 412) {
// HTTP request returned status code 412 - ETag does not match current item's value // HTTP request returned status code 412 - ETag does not match current item's value
// Remove the offending file from OneDrive - file will be uploaded as a new file // Delete record from the local database - file will be uploaded as a new file
onedrive.deleteById(item.driveId, item.id, item.eTag); log.vlog("OneDrive returned a 'HTTP 412 - Precondition Failed' - gracefully handling error");
// Delete from the local database
itemdb.deleteById(item.driveId, item.id); itemdb.deleteById(item.driveId, item.id);
return;
} }
if (e.httpStatusCode == 504) { if (e.httpStatusCode == 504) {
@ -1006,7 +1014,17 @@ final class SyncEngine
writeln("done."); writeln("done.");
} else { } else {
writeln(""); writeln("");
try {
response = session.upload(path, item.driveId, item.parentId, baseName(path), item.eTag); response = session.upload(path, item.driveId, item.parentId, baseName(path), item.eTag);
} catch (OneDriveException e) {
if (e.httpStatusCode == 412) {
// HTTP request returned status code 412 - ETag does not match current item's value
// Delete record from the local database - file will be uploaded as a new file
log.vlog("OneDrive returned a 'HTTP 412 - Precondition Failed' - gracefully handling error");
itemdb.deleteById(item.driveId, item.id);
return;
}
}
writeln("done."); writeln("done.");
} }
} else { } else {
@ -1034,7 +1052,7 @@ final class SyncEngine
saveItem(response); saveItem(response);
} }
log.fileOnly("Uploading file ", path, " ... done."); log.fileOnly("Uploading file ", path, " ... done.");
// use the cTag instead of the eTag because OneDrive may update the metadata of files AFTER they have been uploaded // use the cTag instead of the eTag because OneDrive may update the metadata of files AFTER they have been uploaded via simple upload
eTag = response["cTag"].str; eTag = response["cTag"].str;
} }
if (accountType == "personal"){ if (accountType == "personal"){

View file

@ -92,13 +92,17 @@ struct UploadSession
} else { } else {
// unable to read the local file // unable to read the local file
log.vlog("Restore file upload session failed - unable to read the local file"); log.vlog("Restore file upload session failed - unable to read the local file");
if (exists(sessionFilePath)) {
remove(sessionFilePath); remove(sessionFilePath);
}
return false; return false;
} }
} else { } else {
// session file contains an error - cant resume // session file contains an error - cant resume
log.vlog("Restore file upload session failed - cleaning up session resume"); log.vlog("Restore file upload session failed - cleaning up session resume");
if (exists(sessionFilePath)) {
remove(sessionFilePath); remove(sessionFilePath);
}
return false; return false;
} }
} }
@ -136,14 +140,18 @@ struct UploadSession
save(); save();
} catch (OneDriveException e) { } catch (OneDriveException e) {
// there was an error remove session file // there was an error remove session file
if (exists(sessionFilePath)) {
remove(sessionFilePath); remove(sessionFilePath);
}
return response; return response;
} }
} }
// upload complete // upload complete
p.next(); p.next();
writeln(); writeln();
if (exists(sessionFilePath)) {
remove(sessionFilePath); remove(sessionFilePath);
}
return response; return response;
} }