Resolve Key not found: expirationDateTime on session resume (Issue #174) (#176)

* Handle an invalid response on session resume when a 4xx / 5xx response is generated from the OneDrive service
This commit is contained in:
abraunegg 2018-10-04 09:38:23 +10:00 committed by GitHub
parent 14b2de8f4c
commit 74b9163b06
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -53,40 +53,51 @@ struct UploadSession
if (exists(sessionFilePath)) { if (exists(sessionFilePath)) {
log.vlog("Trying to restore the upload session ..."); log.vlog("Trying to restore the upload session ...");
session = readText(sessionFilePath).parseJSON(); session = readText(sessionFilePath).parseJSON();
auto expiration = SysTime.fromISOExtString(session["expirationDateTime"].str);
if (expiration < Clock.currTime()) { // Check the session resume file for expirationDateTime
log.vlog("The upload session is expired"); if ("expirationDateTime" in session){
return false; // expirationDateTime in the file
} auto expiration = SysTime.fromISOExtString(session["expirationDateTime"].str);
if (!exists(session["localPath"].str)) { if (expiration < Clock.currTime()) {
log.vlog("The file does not exist anymore"); log.vlog("The upload session is expired");
return false; return false;
} }
// Can we read the file - as a permissions issue or file corruption will cause a failure on resume if (!exists(session["localPath"].str)) {
// https://github.com/abraunegg/onedrive/issues/113 log.vlog("The file does not exist anymore");
if (readLocalFile(session["localPath"].str)){ return false;
// able to read the file }
// request the session status // Can we read the file - as a permissions issue or file corruption will cause a failure on resume
JSONValue response; // https://github.com/abraunegg/onedrive/issues/113
try { if (readLocalFile(session["localPath"].str)){
response = onedrive.requestUploadStatus(session["uploadUrl"].str); // able to read the file
} catch (OneDriveException e) { // request the session status
if (e.httpStatusCode == 400) { JSONValue response;
log.vlog("Upload session not found"); try {
return false; response = onedrive.requestUploadStatus(session["uploadUrl"].str);
} else { } catch (OneDriveException e) {
throw e; if (e.httpStatusCode == 400) {
} log.vlog("Upload session not found");
} return false;
session["expirationDateTime"] = response["expirationDateTime"]; } else {
session["nextExpectedRanges"] = response["nextExpectedRanges"]; throw e;
if (session["nextExpectedRanges"].array.length == 0) { }
log.vlog("The upload session is completed"); }
session["expirationDateTime"] = response["expirationDateTime"];
session["nextExpectedRanges"] = response["nextExpectedRanges"];
if (session["nextExpectedRanges"].array.length == 0) {
log.vlog("The upload session is completed");
return false;
}
return true;
} else {
// unable to read the local file
log.vlog("Restore file upload session failed - unable to read the local file");
remove(sessionFilePath);
return false; return false;
} }
return true;
} else { } else {
// unable to read the local file // session file contains an error - cant resume
log.vlog("Restore file upload session failed - cleaning up session resume");
remove(sessionFilePath); remove(sessionFilePath);
return false; return false;
} }
@ -108,19 +119,26 @@ struct UploadSession
while (true) { while (true) {
p.next(); p.next();
long fragSize = fragmentSize < fileSize - offset ? fragmentSize : fileSize - offset; long fragSize = fragmentSize < fileSize - offset ? fragmentSize : fileSize - offset;
response = onedrive.uploadFragment( // If the resume upload fails, we need to check for a return code here
session["uploadUrl"].str, try {
session["localPath"].str, response = onedrive.uploadFragment(
offset, session["uploadUrl"].str,
fragSize, session["localPath"].str,
fileSize offset,
); fragSize,
offset += fragmentSize; fileSize
if (offset >= fileSize) break; );
// update the session offset += fragmentSize;
session["expirationDateTime"] = response["expirationDateTime"]; if (offset >= fileSize) break;
session["nextExpectedRanges"] = response["nextExpectedRanges"]; // update the session details
save(); session["expirationDateTime"] = response["expirationDateTime"];
session["nextExpectedRanges"] = response["nextExpectedRanges"];
save();
} catch (OneDriveException e) {
// there was an error remove session file
remove(sessionFilePath);
return response;
}
} }
// upload complete // upload complete
p.next(); p.next();