handle large uploads with parent id

This commit is contained in:
skilion 2017-12-31 16:11:02 +01:00
parent c8d5e03be8
commit fba3ed999e
3 changed files with 25 additions and 12 deletions

View file

@ -185,11 +185,11 @@ final class OneDriveApi
return post(url, item.toString()); return post(url, item.toString());
} }
// https://dev.onedrive.com/items/upload_large_files.htm // https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_createuploadsession
JSONValue createUploadSession(const(char)[] path, const(char)[] eTag = null) JSONValue createUploadSession(const(char)[] parentDriveId, const(char)[] parentId, const(char)[] filename, const(char)[] eTag = null)
{ {
checkAccessTokenExpired(); checkAccessTokenExpired();
string url = itemByPathUrl ~ encodeComponent(path) ~ ":/createUploadSession"; const(char)[] url = driveByIdUrl ~ parentDriveId ~ "/items/" ~ parentId ~ ":/" ~ encodeComponent(filename) ~ ":/createUploadSession";
if (eTag) http.addRequestHeader("If-Match", eTag); if (eTag) http.addRequestHeader("If-Match", eTag);
return post(url, null); return post(url, null);
} }

View file

@ -277,6 +277,7 @@ final class SyncEngine
} }
// sync remote folder // sync remote folder
// https://github.com/OneDrive/onedrive-api-docs/issues/764
/*if (isItemRemote(driveItem)) { /*if (isItemRemote(driveItem)) {
log.log("Syncing remote folder: ", path); log.log("Syncing remote folder: ", path);
applyDifferences(item.remoteDriveId, item.remoteId); applyDifferences(item.remoteDriveId, item.remoteId);
@ -507,13 +508,14 @@ final class SyncEngine
string eTag = item.eTag; string eTag = item.eTag;
if (!testFileHash(path, item)) { if (!testFileHash(path, item)) {
log.vlog("The file content has changed"); log.vlog("The file content has changed");
log.log("Uploading: ", path); write("Uploading ", path, "...");
JSONValue response; JSONValue response;
if (getSize(path) <= thresholdFileSize) { if (getSize(path) <= thresholdFileSize) {
response = onedrive.simpleUpload(path, item.driveId, item.id, item.eTag); response = onedrive.simpleUploadReplace(path, item.driveId, item.id, item.eTag);
writeln(" done.");
} else { } else {
// TODO: upload by id writeln("");
response = session.upload(path, path, eTag); response = session.upload(path, item.parentDriveId, item.parentId, baseName(path), eTag);
} }
/*saveItem(response); /*saveItem(response);
id = response["id"].str; id = response["id"].str;
@ -593,15 +595,16 @@ final class SyncEngine
JSONValue response; JSONValue response;
if (getSize(path) <= thresholdFileSize) { if (getSize(path) <= thresholdFileSize) {
response = onedrive.simpleUpload(path, parent.driveId, parent.id, baseName(path)); response = onedrive.simpleUpload(path, parent.driveId, parent.id, baseName(path));
writeln(" done.");
} else { } else {
response = session.upload(path, path); writeln("");
response = session.upload(path, parent.driveId, parent.id, baseName(path));
} }
string id = response["id"].str; string id = response["id"].str;
string cTag = response["cTag"].str; string cTag = response["cTag"].str;
SysTime mtime = timeLastModified(path).toUTC(); SysTime mtime = timeLastModified(path).toUTC();
// 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
uploadLastModifiedTime(parent.driveId, id, cTag, mtime); uploadLastModifiedTime(parent.driveId, id, cTag, mtime);
writeln(" done.");
} }
private void uploadDeleteItem(Item item, const(char)[] path) private void uploadDeleteItem(Item item, const(char)[] path)

View file

@ -21,9 +21,9 @@ struct UploadSession
this.verbose = verbose; this.verbose = verbose;
} }
JSONValue upload(string localPath, string remotePath, const(char)[] eTag = null) JSONValue upload(string localPath, const(char)[] parentDriveId, const(char)[] parentId, const(char)[] filename, const(char)[] eTag = null)
{ {
session = onedrive.createUploadSession(remotePath, eTag); session = onedrive.createUploadSession(parentDriveId, parentId, filename, eTag);
session["localPath"] = localPath; session["localPath"] = localPath;
save(); save();
return upload(); return upload();
@ -47,7 +47,17 @@ struct UploadSession
return false; return false;
} }
// request the session status // request the session status
auto response = onedrive.requestUploadStatus(session["uploadUrl"].str); JSONValue response;
try {
response = onedrive.requestUploadStatus(session["uploadUrl"].str);
} catch (OneDriveException e) {
if (e.httpStatusCode == 400) {
log.vlog("Upload session not found");
return false;
} else {
throw e;
}
}
session["expirationDateTime"] = response["expirationDateTime"]; session["expirationDateTime"] = response["expirationDateTime"];
session["nextExpectedRanges"] = response["nextExpectedRanges"]; session["nextExpectedRanges"] = response["nextExpectedRanges"];
if (session["nextExpectedRanges"].array.length == 0) { if (session["nextExpectedRanges"].array.length == 0) {