upload files new with parent id

This commit is contained in:
skilion 2017-12-31 13:47:18 +01:00
parent 6907daa5e8
commit c8d5e03be8
2 changed files with 15 additions and 27 deletions

View file

@ -136,17 +136,6 @@ final class OneDriveApi
download(url, saveToPath); download(url, saveToPath);
} }
// https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_put_content
JSONValue simpleUpload(string localPath, const(char)[] remotePath, const(char)[] eTag = null)
{
checkAccessTokenExpired();
string url = itemByPathUrl ~ encodeComponent(remotePath) ~ ":/content";
// TODO: investigate why this fails for remote folders
//if (eTag) http.addRequestHeader("If-Match", eTag);
/*else*/ http.addRequestHeader("If-None-Match", "*");
return upload(localPath, url);
}
// https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_put_content // https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_put_content
JSONValue simpleUpload(string localPath, string parentDriveId, string parentId, string filename, const(char)[] eTag = null) JSONValue simpleUpload(string localPath, string parentDriveId, string parentId, string filename, const(char)[] eTag = null)
{ {
@ -159,7 +148,7 @@ final class OneDriveApi
} }
// https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_put_content // https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_put_content
JSONValue simpleUploadById(string localPath, string driveId, string id, const(char)[] eTag = null) JSONValue simpleUploadReplace(string localPath, string driveId, string id, const(char)[] eTag = null)
{ {
checkAccessTokenExpired(); checkAccessTokenExpired();
string url = driveByIdUrl ~ driveId ~ "/items/" ~ id ~ "/content"; string url = driveByIdUrl ~ driveId ~ "/items/" ~ id ~ "/content";

View file

@ -330,17 +330,16 @@ final class SyncEngine
rename(oldPath, newPath); rename(oldPath, newPath);
} }
// handle changed content and mtime // handle changed content and mtime
// HACK: use mtime instead of cTag because of https://github.com/OneDrive/onedrive-api-docs/issues/765 // HACK: use mtime+hash instead of cTag because of https://github.com/OneDrive/onedrive-api-docs/issues/765
if (newItem.type == ItemType.file && oldItem.mtime != newItem.mtime) { if (newItem.type == ItemType.file && oldItem.mtime != newItem.mtime && !testFileHash(newPath, newItem)) {
downloadFileItem(newItem, newPath); downloadFileItem(newItem, newPath);
} else { } else {
log.vlog("The item content has not changed"); log.vlog("The item content has not changed");
} }
// handle changed time // handle changed time
/* redundant because of the previous HACK if (newItem.type == ItemType.file && oldItem.mtime != newItem.mtime) {
if (newItem.type == ItemType.file) {
setTimes(newPath, newItem.mtime, newItem.mtime); setTimes(newPath, newItem.mtime, newItem.mtime);
}*/ }
} else { } else {
log.vlog("The item has not changed"); log.vlog("The item has not changed");
} }
@ -511,7 +510,7 @@ final class SyncEngine
log.log("Uploading: ", path); log.log("Uploading: ", path);
JSONValue response; JSONValue response;
if (getSize(path) <= thresholdFileSize) { if (getSize(path) <= thresholdFileSize) {
response = onedrive.simpleUploadById(path, item.driveId, item.id, item.eTag); response = onedrive.simpleUpload(path, item.driveId, item.id, item.eTag);
} else { } else {
// TODO: upload by id // TODO: upload by id
response = session.upload(path, path, eTag); response = session.upload(path, path, eTag);
@ -574,7 +573,7 @@ final class SyncEngine
private void uploadCreateDir(const(char)[] path) private void uploadCreateDir(const(char)[] path)
{ {
log.log("Uploading folder ", path); log.log("Creating folder ", path, "...");
Item parent; Item parent;
enforce(itemdb.selectByPath(dirName(path), parent), "The parent item is not in the database"); enforce(itemdb.selectByPath(dirName(path), parent), "The parent item is not in the database");
JSONValue driveItem = [ JSONValue driveItem = [
@ -583,25 +582,26 @@ final class SyncEngine
]; ];
auto res = onedrive.createById(parent.driveId, parent.id, driveItem); auto res = onedrive.createById(parent.driveId, parent.id, driveItem);
saveItem(res); saveItem(res);
writeln(" done.");
} }
private void uploadNewFile(string path) private void uploadNewFile(string path)
{ {
log.log("Uploading: ", path); write("Uploading file ", path, "...");
Item parent;
enforce(itemdb.selectByPath(dirName(path), parent), "The parent item is not in the database");
JSONValue response; JSONValue response;
if (getSize(path) <= thresholdFileSize) { if (getSize(path) <= thresholdFileSize) {
response = onedrive.simpleUpload(path, path); response = onedrive.simpleUpload(path, parent.driveId, parent.id, baseName(path));
} else { } else {
response = session.upload(path, path); response = session.upload(path, path);
} }
string driveId = response["parentReference"]["driveId"].str;
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 changes the // use the cTag instead of the eTag because Onedrive may update the metadata of files AFTER they have been uploaded
* metadata of some type of files (ex. images) AFTER they have been uploadLastModifiedTime(parent.driveId, id, cTag, mtime);
* uploaded */ writeln(" done.");
uploadLastModifiedTime(driveId, id, cTag, mtime);
} }
private void uploadDeleteItem(Item item, const(char)[] path) private void uploadDeleteItem(Item item, const(char)[] path)
@ -624,7 +624,6 @@ final class SyncEngine
]) ])
]; ];
auto response = onedrive.updateById(driveId, id, data, eTag); auto response = onedrive.updateById(driveId, id, data, eTag);
writeln(response);
saveItem(response); saveItem(response);
} }