public void CreateDir(string filePath) { var dir = Path.GetDirectoryName(filePath); var dirNode = GetItem(dir); var name = Path.GetFileName(filePath); var node = amazon.Nodes.CreateFolder(dirNode.Id, name).Result; itemsTreeCache.Add(FSItem.FromNode(filePath, node)); }
public async Task <IList <FSItem> > GetDirItems(string folderPath) { var cached = itemsTreeCache.GetDir(folderPath); if (cached != null) { // Log.Warn("Got cached dir:\r\n " + string.Join("\r\n ", cached)); return((await Task.WhenAll(cached.Select(i => FetchNode(i)))).Where(i => i != null).ToList()); } var folderNode = GetItem(folderPath); var nodes = await amazon.Nodes.GetChildren(folderNode?.Id); var items = new List <FSItem>(nodes.Count); var curdir = folderPath; if (curdir == "\\") { curdir = string.Empty; } foreach (var node in nodes.Where(n => FsItemKinds.Contains(n.kind))) { if (node.status != AmazonNodeStatus.AVAILABLE) { continue; } var path = curdir + "\\" + node.name; items.Add(FSItem.FromNode(path, node)); } // Log.Warn("Got real dir:\r\n " + string.Join("\r\n ", items.Select(i => i.Path))); itemsTreeCache.AddDirItems(folderPath, items); return(items); }
private async Task <FSItem> FetchNode(string itemPath) { if (itemPath == "\\" || itemPath == string.Empty) { return(FSItem.FromRoot(await amazon.Nodes.GetRoot())); } var cached = itemsTreeCache.GetItem(itemPath); if (cached != null) { if (cached.NotExistingDummy) { // Log.Warn("NonExisting path from cache: " + itemPath); return(null); } return(cached); } var folders = new LinkedList <string>(); var curpath = itemPath; FSItem item = null; do { folders.AddFirst(Path.GetFileName(curpath)); curpath = Path.GetDirectoryName(curpath); if (curpath == "\\" || string.IsNullOrEmpty(curpath)) { break; } item = itemsTreeCache.GetItem(curpath); }while (item == null); if (item == null) { item = FSItem.FromRoot(await amazon.Nodes.GetRoot()); } foreach (var name in folders) { if (curpath == "\\") { curpath = string.Empty; } curpath = curpath + "\\" + name; var newnode = await amazon.Nodes.GetChild(item.Id, name); if (newnode == null || newnode.status != AmazonNodeStatus.AVAILABLE) { itemsTreeCache.AddItemOnly(FSItem.MakeNotExistingDummy(curpath)); // Log.Error("NonExisting path from server: " + itemPath); return(null); } item = FSItem.FromNode(curpath, newnode); itemsTreeCache.Add(item); } return(item); }
public void MoveFile(string oldPath, string newPath, bool replace) { if (oldPath == newPath) { return; } Log.Trace($"Move: {oldPath} to {newPath} replace:{replace}"); var oldDir = Path.GetDirectoryName(oldPath); var oldName = Path.GetFileName(oldPath); var newDir = Path.GetDirectoryName(newPath); var newName = Path.GetFileName(newPath); var item = GetItem(oldPath); item = WaitForReal(item, 25000); if (oldName != newName) { if (item.Length > 0 || item.IsDir) { item = FSItem.FromNode(Path.Combine(oldDir, newName), amazon.Nodes.Rename(item.Id, newName).Result); } else { item = new FSItem(item) { Path = Path.Combine(oldDir, newName) }; } if (item == null) { throw new InvalidOperationException("Can not rename"); } } if (oldDir != newDir) { var oldDirNodeTask = FetchNode(oldDir); var newDirNodeTask = FetchNode(newDir); Task.WaitAll(oldDirNodeTask, newDirNodeTask); if (item.Length > 0 || item.IsDir) { item = FSItem.FromNode(newPath, amazon.Nodes.Move(item.Id, oldDirNodeTask.Result.Id, newDirNodeTask.Result.Id).Result); if (item == null) { throw new InvalidOperationException("Can not move"); } } else { item = new FSItem(item) { Path = newPath }; } } if (item.IsDir) { itemsTreeCache.MoveDir(oldPath, item); } else { itemsTreeCache.MoveFile(oldPath, item); } }
public FSProvider(AmazonDrive amazon) { this.amazon = amazon; SmallFilesCache = new SmallFilesCache(amazon); SmallFilesCache.OnDownloadStarted = (id) => { Interlocked.Increment(ref downloadingCount); OnStatisticsUpdated?.Invoke(downloadingCount, uploadingCount); }; SmallFilesCache.OnDownloaded = (id) => { Interlocked.Decrement(ref downloadingCount); OnStatisticsUpdated?.Invoke(downloadingCount, uploadingCount); }; SmallFilesCache.OnDownloadFailed = (id) => { Interlocked.Decrement(ref downloadingCount); OnStatisticsUpdated?.Invoke(downloadingCount, uploadingCount); }; UploadService = new UploadService(2, amazon); UploadService.OnUploadFailed = (uploaditem, reason) => { Interlocked.Decrement(ref uploadingCount); OnStatisticsUpdated?.Invoke(downloadingCount, uploadingCount); var olditemPath = Path.Combine(UploadService.CachePath, uploaditem.Id); File.Delete(olditemPath); switch (reason) { case FailReason.ZeroLength: var item = GetItem(uploaditem.Path); item?.MakeNotUploading(); return; case FailReason.Conflict: return; } itemsTreeCache.DeleteFile(uploaditem.Path); }; UploadService.OnUploadFinished = (item, node) => { Interlocked.Decrement(ref uploadingCount); OnStatisticsUpdated?.Invoke(downloadingCount, uploadingCount); var newitem = FSItem.FromNode(item.Path, node); var olditemPath = Path.Combine(UploadService.CachePath, item.Id); var newitemPath = Path.Combine(SmallFilesCache.CachePath, node.id); if (!File.Exists(newitemPath)) { File.Move(olditemPath, newitemPath); } else { File.Delete(olditemPath); } SmallFilesCache.AddExisting(newitem); itemsTreeCache.Update(newitem); }; UploadService.OnUploadResumed = item => { itemsTreeCache.Add(item); Interlocked.Increment(ref uploadingCount); OnStatisticsUpdated?.Invoke(downloadingCount, uploadingCount); }; UploadService.Start(); }