Skip to content

Commit

Permalink
Handle HTTP request returned status code 412 (Precondition Failed) fo…
Browse files Browse the repository at this point in the history
…r 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
  • Loading branch information
abraunegg authored Nov 11, 2018
1 parent 11e477f commit 86ea576
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
28 changes: 23 additions & 5 deletions src/sync.d
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,14 @@ final class SyncEngine
try {
response = onedrive.simpleUploadReplace(path, item.driveId, item.id, item.eTag);
} 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
if ((e.httpStatusCode == 409) || (e.httpStatusCode == 423)) {
// The file is currently checked out or locked for editing by another user
Expand All @@ -990,10 +998,10 @@ final class SyncEngine

if (e.httpStatusCode == 412) {
// 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
onedrive.deleteById(item.driveId, item.id, item.eTag);
// Delete from the local database
// 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;
}

if (e.httpStatusCode == 504) {
Expand All @@ -1006,7 +1014,17 @@ final class SyncEngine
writeln("done.");
} else {
writeln("");
response = session.upload(path, item.driveId, item.parentId, baseName(path), item.eTag);
try {
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.");
}
} else {
Expand Down Expand Up @@ -1034,7 +1052,7 @@ final class SyncEngine
saveItem(response);
}
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;
}
if (accountType == "personal"){
Expand Down
16 changes: 12 additions & 4 deletions src/upload.d
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,17 @@ struct UploadSession
} else {
// unable to read the local file
log.vlog("Restore file upload session failed - unable to read the local file");
remove(sessionFilePath);
if (exists(sessionFilePath)) {
remove(sessionFilePath);
}
return false;
}
} else {
// session file contains an error - cant resume
log.vlog("Restore file upload session failed - cleaning up session resume");
remove(sessionFilePath);
if (exists(sessionFilePath)) {
remove(sessionFilePath);
}
return false;
}
}
Expand Down Expand Up @@ -136,14 +140,18 @@ struct UploadSession
save();
} catch (OneDriveException e) {
// there was an error remove session file
remove(sessionFilePath);
if (exists(sessionFilePath)) {
remove(sessionFilePath);
}
return response;
}
}
// upload complete
p.next();
writeln();
remove(sessionFilePath);
if (exists(sessionFilePath)) {
remove(sessionFilePath);
}
return response;
}

Expand Down

0 comments on commit 86ea576

Please sign in to comment.