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());
}
// https://dev.onedrive.com/items/upload_large_files.htm
JSONValue createUploadSession(const(char)[] path, const(char)[] eTag = null)
// https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_createuploadsession
JSONValue createUploadSession(const(char)[] parentDriveId, const(char)[] parentId, const(char)[] filename, const(char)[] eTag = null)
{
checkAccessTokenExpired();
string url = itemByPathUrl ~ encodeComponent(path) ~ ":/createUploadSession";
const(char)[] url = driveByIdUrl ~ parentDriveId ~ "/items/" ~ parentId ~ ":/" ~ encodeComponent(filename) ~ ":/createUploadSession";
if (eTag) http.addRequestHeader("If-Match", eTag);
return post(url, null);
}

View file

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

View file

@ -21,9 +21,9 @@ struct UploadSession
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;
save();
return upload();
@ -47,7 +47,17 @@ struct UploadSession
return false;
}
// 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["nextExpectedRanges"] = response["nextExpectedRanges"];
if (session["nextExpectedRanges"].array.length == 0) {