half working uploadDifference

This commit is contained in:
skilion 2015-09-06 22:42:44 +02:00
parent f8e87a2d4f
commit ac4584f795

View file

@ -43,6 +43,7 @@ final class SyncEngine
OneDriveApi onedrive; OneDriveApi onedrive;
ItemCache itemCache; ItemCache itemCache;
string[] itemToDelete; // array of items to be deleted string[] itemToDelete; // array of items to be deleted
JSONValue folderItem;
this(Config cfg, OneDriveApi onedrive) this(Config cfg, OneDriveApi onedrive)
{ {
@ -50,6 +51,11 @@ final class SyncEngine
this.cfg = cfg; this.cfg = cfg;
this.onedrive = onedrive; this.onedrive = onedrive;
itemCache.init(); itemCache.init();
folderItem = parseJSON("{
\"name\": \"\",
\"folder\": {},
\"fileSystemInfo\": { \"lastModifiedDateTime\": \"\" }
}");
} }
void applyDifferences() void applyDifferences()
@ -68,7 +74,7 @@ final class SyncEngine
JSONValue changes; JSONValue changes;
do { do {
chdir(syncDir); chdir(syncDir);
changes = onedrive.viewChangesByPath("Politecnico", statusToken); changes = onedrive.viewChangesByPath("test", statusToken);
foreach (item; changes["value"].array) { foreach (item; changes["value"].array) {
applyDifference(item); applyDifference(item);
} }
@ -266,16 +272,20 @@ final class SyncEngine
private void deleteFiles() private void deleteFiles()
{ {
writeln("Deleting marked files ...");
foreach_reverse (ref path; itemToDelete) { foreach_reverse (ref path; itemToDelete) {
if (isFile(path)) { if (isFile(path)) {
remove(path); remove(path);
} else { } else {
// TODO: test not empty folder try {
rmdir(path); rmdir(path);
} catch (FileException e) {
writeln("Keeping dir \"", path, "\" not empty");
}
} }
} }
assumeSafeAppend(itemToDelete);
itemToDelete.length = 0; itemToDelete.length = 0;
assumeSafeAppend(itemToDelete);
} }
// scan the directory for unsynced files and upload them // scan the directory for unsynced files and upload them
@ -285,8 +295,8 @@ final class SyncEngine
string currDir = getcwd(); string currDir = getcwd();
string syncDir = cfg.get("sync_dir"); string syncDir = cfg.get("sync_dir");
chdir(syncDir); chdir(syncDir);
foreach (DirEntry entry; dirEntries(".", SpanMode.breadth, false)) { foreach (DirEntry entry; dirEntries("test", SpanMode.breadth, false)) {
uploadDifference(entry.name[2 .. $]); uploadDifference(entry.name);
} }
// TODO: check deleted files // TODO: check deleted files
chdir(currDir); chdir(currDir);
@ -294,14 +304,76 @@ final class SyncEngine
private void uploadDifference(const(char)[] path) private void uploadDifference(const(char)[] path)
{ {
writeln(path);
assert(exists(path)); assert(exists(path));
Item item; Item item;
bool needDelete, uploadFile, createDir, changedLastModifiedTime;
if (itemCache.selectByPath(path, item)) { if (itemCache.selectByPath(path, item)) {
if (!isItemSynced(item)) { final switch (item.type) {
onedrive.simpleUpload(path.dup, path, item.eTag); case ItemType.file:
if (isFile(item.path)) {
SysTime localModifiedTime = timeLastModified(item.path);
import core.time: Duration;
item.mtime.fracSecs = Duration.zero; // HACK
if (localModifiedTime != item.mtime) {
writeln("Different last modified time");
changedLastModifiedTime = true;
if (item.crc32) {
string localCrc32 = computeCrc32(item.path);
if (localCrc32 != item.crc32) {
writeln("Different content");
uploadFile = true;
}
} else {
writeln("The local item has no hash, assuming it's different");
uploadFile = true;
}
}
} else {
writeln("The local item is changed in a directory");
needDelete = true;
createDir = true;
}
break;
case ItemType.dir:
if (isDir(item.path)) {
// ignore folder mtime
} else {
writeln("The local item changed in a file");
needDelete = true;
uploadFile = true;
}
break;
} }
} else { } else {
onedrive.simpleUpload(path.dup, path); if (isFile(path)) uploadFile = true;
else createDir = true;
}
string id = item.id;
string eTag = item.eTag;
if (needDelete) {
assert(id);
writeln("Deleting ...");
onedrive.deleteById(id, eTag);
}
if (uploadFile) {
writeln("Uploading file ...");
auto returnedItem = onedrive.simpleUpload(path.dup, path, eTag);
id = returnedItem["id"].str;
eTag = returnedItem["eTag"].str;
}
if (uploadFile || changedLastModifiedTime) {
writeln("Updating last modified time ...");
JSONValue mtime = ["fileSystemInfo": JSONValue(["lastModifiedDateTime": timeLastModified(path).toUTC().toISOExtString()])];
onedrive.updateById(id, mtime, eTag);
}
if (createDir) {
writeln("Creating folder ...");
import std.path;
folderItem["name"] = baseName(path);
folderItem["fileSystemInfo"].object["lastModifiedDateTime"] = timeLastModified(path).toUTC().toISOExtString();
onedrive.createByPath(dirName(path), folderItem);
} }
} }
} }