handle moving items between drives

This commit is contained in:
skilion 2018-01-01 18:38:08 +01:00
parent c54d1927e9
commit 9bedec0d7c
2 changed files with 24 additions and 14 deletions

View file

@ -190,7 +190,7 @@ void performSync(SyncEngine sync)
sync.applyDifferences();
if (!downloadOnly) {
sync.scanForDifferences();
// HACK: file metadata are often changed by OneDrive after an upload
// ensure that the current state is updated
sync.applyDifferences();
}
count = -1;

View file

@ -655,30 +655,40 @@ final class SyncEngine
itemdb.upsert(item);
}
// https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_move
void uploadMoveItem(string from, string to)
{
log.log("Moving remote item: ", from, " -> ", to);
log.log("Moving ", from, " to ", to);
Item fromItem, toItem, parentItem;
if (!itemdb.selectByPath(from, fromItem)) {
throw new SyncException("Can't move an unsynced item");
}
if (itemdb.selectByPath(to, toItem)) {
// the destination has been overridden
// the destination has been overwritten
uploadDeleteItem(toItem, to);
}
if (!itemdb.selectByPath(to.dirName, parentItem)) {
if (!itemdb.selectByPath(dirName(to), parentItem)) {
throw new SyncException("Can't move an item to an unsynced directory");
}
JSONValue diff = ["name": baseName(to)];
diff["parentReference"] = JSONValue([
"id": parentItem.id
]);
auto res = onedrive.updateById(fromItem.driveId, fromItem.id, diff, fromItem.eTag);
saveItem(res);
string driveId = res["parentReference"]["driveId"].str;
string id = res["id"].str;
string eTag = res["eTag"].str;
uploadLastModifiedTime(driveId, id, eTag, timeLastModified(to).toUTC());
if (fromItem.driveId != parentItem.driveId) {
// items cannot be moved between drives
uploadDeleteItem(fromItem, from);
uploadNewFile(to);
} else {
SysTime mtime = timeLastModified(to).toUTC();
JSONValue diff = [
"name": JSONValue(baseName(to)),
"parentReference": JSONValue([
"id": parentItem.id
]),
"fileSystemInfo": JSONValue([
"lastModifiedDateTime": mtime.toISOExtString()
])
];
auto res = onedrive.updateById(fromItem.driveId, fromItem.id, diff, fromItem.eTag);
// update itemdb
saveItem(res);
}
}
void deleteByPath(const(char)[] path)