private Downloader StartDownload(FSItem item, string path)
        {
            var downloader = new Downloader(item, path);

            lock (DownloadersLock)
            {
                if (Downloaders.TryGetValue(item.Path, out Downloader result))
                {
                    if (result.Task == null)
                    {
                        throw new Exception("Downloader Task is Null");
                    }

                    return(result);
                }

                Directory.CreateDirectory(cachePath);

                var fileinfo = new FileInfo(path);

                if (fileinfo.Exists && fileinfo.Length == item.Length)
                {
                    return(Downloader.CreateCompleted(item, path, item.Length));
                }

                Stream writer;

                if (!fileinfo.Exists || fileinfo.Length < item.Length)
                {
                    writer = new FileStream(
                        path,
                        FileMode.Append,
                        FileAccess.Write,
                        FileShare.ReadWrite,
                        4096,
                        FileOptions.Asynchronous | FileOptions.SequentialScan);
                    if (writer.Length > 0)
                    {
                        Log.Warn(
                            $"File was not totally downloaded before. Should be {item.Length} but was {writer.Length}: {item.Path} - {item.Id}");
                        downloader.Downloaded = writer.Length;
                    }
                }
                else
                {
                    writer = new FileStream(
                        path,
                        FileMode.Create,
                        FileAccess.Write,
                        FileShare.ReadWrite,
                        4096,
                        FileOptions.Asynchronous | FileOptions.SequentialScan);
                }

                downloader.Task = Task.Factory.StartNew(async() => await Download(item, writer, downloader), TaskCreationOptions.LongRunning);
                Downloaders.Add(item.Path, downloader);

                return(downloader);
            }
        }
Beispiel #2
0
        public SmallFileBlockStream OpenReadCachedOnly(FSItem item, Downloader downloader = null)
        {
            CacheEntry entry;
            var        path = Path.Combine(cachePath, item.Id);

            if (!File.Exists(path))
            {
                if (access.TryRemove(item.Id, out entry))
                {
                    TotalSizeDecrease(item.Length);
                }

                return(null);
            }

            if (access.TryGetValue(item.Id, out entry))
            {
                entry.AccessTime = DateTime.UtcNow;
            }
            else
            {
                AddExisting(item);
            }

            Log.Trace("Opened cached: " + item.Id);
            return(SmallFileBlockStream.OpenReadonly(item, path, downloader));
        }
Beispiel #3
0
 public UploadInfo(FSItem item)
 {
     Id       = item.Id;
     Path     = item.Path;
     ParentId = item.ParentIds.First();
     Length   = item.Length;
 }
Beispiel #4
0
        private async Task DeleteItem(string filePath, FSItem item)
        {
            try
            {
                if (item.ParentIds.Count == 1)
                {
                    if (item.IsUploading)
                    {
                        UploadService.CancelUpload(item.Id);
                    }
                    else
                    {
                        await cloud.Nodes.Trash(item.Id);
                    }
                }
                else
                {
                    var dir     = Path.GetDirectoryName(filePath);
                    var dirItem = await FetchNode(dir);

                    await cloud.Nodes.Remove(dirItem.Id, item.Id);
                }
            }
            catch (AggregateException ex)
            {
                throw ex.Flatten();
            }
            catch (CloudException ex) when(ex.Error == HttpStatusCode.NotFound || ex.Error == HttpStatusCode.Conflict)
            {
                Log.Warn(ex.Error.ToString());
            }
        }
Beispiel #5
0
        private async Task MakeUploads(FSItem dest, List <string> files)
        {
            var currentfiles = new Queue <UploadTaskItem>(files.Select(f => new UploadTaskItem {
                Parent = dest, File = f
            }));

            while (currentfiles.Count > 0)
            {
                var item = currentfiles.Dequeue();
                if (Directory.Exists(item.File))
                {
                    var created = await CheckCreateFolder(item.Parent, Path.GetFileName(item.File));

                    if (created != null)
                    {
                        foreach (var file in Directory.EnumerateFileSystemEntries(item.File))
                        {
                            currentfiles.Enqueue(new UploadTaskItem {
                                Parent = created, File = file
                            });
                        }
                    }
                }
                else
                {
                    await UploadService.AddUpload(item.Parent, item.File);
                }
            }
        }
Beispiel #6
0
        public async Task AddUpload(FSItem parent, string file)
        {
            Directory.CreateDirectory(cachePath);
            var fileinfo = new FileInfo(file);
            var infoId = Guid.NewGuid().ToString();

            // To copy a source file to file in cache folder
            var tempFile = Path.Combine(cachePath, infoId) + ".temp";
            var tempFileInfo = new FileInfo(tempFile);
            File.Copy(file, tempFile, true);
            BoschHelper.Encrypt(file, tempFile);

            var info = new UploadInfo
            {
                Id = infoId,
                Length = tempFileInfo.Length,
                Path = Path.Combine(parent.Path, Path.GetFileName(file)),
                ParentId = parent.Id,
                SourcePath = tempFile
            };

            var path = Path.Combine(cachePath, info.Id);
            await WriteInfo(path + ".info", info);
            leftUploads.Add(info);
            allUploads.TryAdd(info.Id, info);
            OnUploadAdded?.Invoke(info);
        }
Beispiel #7
0
        public async Task AddOverwrite(FSItem item)
        {
            try
            {
                Directory.CreateDirectory(cachePath);

                // To copy a source file to file in cache folder
                var sourceFile = Path.Combine(cachePath, item.Id);
                var sourceFileInfoPath = sourceFile + ".info";
                var tempFile = sourceFile + ".temp";
                File.Copy(sourceFile, tempFile, true);
                BoschHelper.Encrypt(sourceFile, tempFile);
                var tempFileInfo = new FileInfo(tempFile);

                File.Delete(sourceFileInfoPath);
                var info = new UploadInfo(item)
                {
                    Length = tempFileInfo.Length,
                    Path = tempFile,
                    SourcePath = tempFile,
                    Overwrite = true
                };

                await WriteInfo(sourceFileInfoPath, info);
                leftUploads.Add(info);
                allUploads.TryAdd(info.Id, info);
                OnUploadAdded?.Invoke(info);
            }
            catch(Exception ex)
            {

            }
        }
        public void CopyFile(ISPCFolder folder, ISPCItem item, string newFileName)
        {
            FSFolder fsFolder = (FSFolder)folder;
            FSItem   fsItem   = (FSItem)item;

            File.Copy(fsItem.URL, fsFolder.Path + "\\" + newFileName);
        }
        public bool CheckFileExistency(ISPCFolder folder, ISPCItem item, string newFileName)
        {
            FSFolder fsFolder = (FSFolder)folder;
            FSItem   fsItem   = (FSItem)item;

            return(File.Exists(fsFolder.Path + "\\" + newFileName));
        }
        private string GetFilePath(FSItem item)
        {
            var path = Path.Combine(cachePath, item.Id);

            if (!access.TryGetValue(item.Id, out CacheEntry entry))
            {
                if (!File.Exists(path))
                {
                    return(null);
                }

                entry = AddExisting(item);
            }

            entry.AccessTime = DateTime.UtcNow;

            if (entry.LinkPath != null && File.Exists(entry.LinkPath))
            {
                path = entry.LinkPath;
            }

            if (!File.Exists(path))
            {
                access.TryRemove(item.Id, out _);
                return(null);
            }

            return(path);
        }
Beispiel #11
0
        public void MoveFile(IItem item, Folder folder, string newFileName)
        {
            FSFolder fsFolder = (FSFolder)folder;
            FSItem   fsItem   = (FSItem)item;

            File.Move(fsItem.URL, fsFolder.Path + "\\" + newFileName);
        }
Beispiel #12
0
        public async Task BuildItemInfo(FSItem item)
        {
            var info = await cloud.Nodes.GetNodeExtended(item.Id);

            var str = JsonConvert.SerializeObject(info);

            item.Info = Encoding.UTF8.GetBytes(str);
        }
Beispiel #13
0
 public static Downloader CreateCompleted(FSItem item, string path, long length)
 {
     return(new Downloader(item, path)
     {
         Downloaded = length,
         Task = Task.FromResult(true),
     });
 }
Beispiel #14
0
 public void AddExisting(FSItem item)
 {
     if (access.TryAdd(item.Id, new CacheEntry {
         Id = item.Id, AccessTime = DateTime.UtcNow
     }))
     {
         TotalSizeIncrease(item.Length);
     }
 }
Beispiel #15
0
        public static SmallFileBlockStream OpenWriteable(FSItem item, string filePath, Downloader downloader)
        {
            if (downloader == null)
            {
                Log.Warn("No downloader");
            }

            return(new SmallFileBlockStream(item, filePath, downloader, true));
        }
Beispiel #16
0
        private async Task AddUpload(FSItem item)
        {
            var info = new UploadInfo(item);

            var path = Path.Combine(cachePath, item.Id);
            await WriteInfo(path + ".info", info);
            leftUploads.Add(info);
            allUploads.TryAdd(info.Id, info);
            await (OnUploadAdded?.Invoke(info) ?? Task.FromResult(0));
        }
Beispiel #17
0
        private NtStatus ProcessItemInfo(string[] streamNameGroups, FSItem item, DokanFileInfo info)
        {
            if (streamNameGroups.Length == 1)
            {
                return(OpenAsByteArray(item.Info, info));
            }

            var result = Wait(provider.GetExtendedInfo(streamNameGroups, item));

            return(OpenAsByteArray(result, info));
        }
 public void AddItemOnly(FSItem item)
 {
     lok.EnterWriteLock();
     try
     {
         pathToNode[item.Path] = item;
     }
     finally
     {
         lok.ExitWriteLock();
     }
 }
        public SmallFileBlockStream OpenReadCachedOnly(FSItem item, Downloader downloader = null)
        {
            var path = GetFilePath(item);

            if (path == null)
            {
                return(null);
            }

            Log.Trace("Opened cached: " + item.Id);
            return(SmallFileBlockStream.OpenReadonly(item, path, downloader));
        }
 public void Update(FSItem newitem)
 {
     lok.EnterWriteLock();
     try
     {
         pathToNode[newitem.Path] = newitem;
     }
     finally
     {
         lok.ExitWriteLock();
     }
 }
        public IBlockStream OpenReadWithDownload(FSItem item)
        {
            var path       = Path.Combine(cachePath, item.Id);
            var downloader = StartDownload(item, path);

            if (access.TryGetValue(item.Id, out CacheEntry entry))
            {
                entry.AccessTime = DateTime.UtcNow;
            }

            return(OpenReadCachedOnly(item, downloader));
        }
 public void MoveFile(string oldPath, FSItem newNode)
 {
     lok.EnterWriteLock();
     try
     {
         DeleteFile(oldPath);
         Add(newNode);
     }
     finally
     {
         lok.ExitWriteLock();
     }
 }
Beispiel #23
0
        public async Task <ActionResult <FSItemDTO> > PostFSItem(FSItemDTO fSItemDTO)
        {
            var fSItem = new FSItem
            {
                IsComplete = fSItemDTO.IsComplete,
                Name       = fSItemDTO.Name
            };

            _context.FSItems.Add(fSItem);
            await _context.SaveChangesAsync();

            return(CreatedAtAction(nameof(GetFSItem), new { id = fSItem.Id }, ItemToDTO(fSItem)));
        }
        public SmallFileBlockStream OpenReadWrite(FSItem item)
        {
            var path       = Path.Combine(cachePath, item.Id);
            var downloader = StartDownload(item, path);

            if (access.TryGetValue(item.Id, out CacheEntry entry))
            {
                entry.AccessTime = DateTime.UtcNow;
            }

            Log.Trace("Opened ReadWrite cached: " + item.Id);
            return(SmallFileBlockStream.OpenWriteable(item, path, downloader));
        }
Beispiel #25
0
 public void Delete(FSItem item)
 {
     try
     {
         var path = Path.Combine(cachePath, item.Id);
         File.Delete(path);
         CacheEntry outitem;
         access.TryRemove(item.Id, out outitem);
     }
     catch (Exception)
     {
         Log.Warn($"Could not delete small file in cache {item.Name}");
     }
 }
Beispiel #26
0
        private async Task <FSItem> CheckCreateFolder(FSItem parent, string name)
        {
            var node = await cloud.Nodes.GetChild(parent.Id, name);

            if (node != null)
            {
                return(node.SetParentPath(parent.Path).Build());
            }

            var result = (await cloud.Nodes.CreateFolder(parent.Id, name)).SetParentPath(parent.Path).Build();

            itemsTreeCache.Add(result);
            return(result);
        }
Beispiel #27
0
        public async Task <byte[]> GetExtendedInfo(string[] streamNameGroups, FSItem item)
        {
            switch (streamNameGroups[1])
            {
            case CloudDokanNetAssetInfo.StreamNameShareReadOnly:
                return(Encoding.UTF8.GetBytes(await cloud.Nodes.ShareNode(item.Id, NodeShareType.ReadOnly)));

            case CloudDokanNetAssetInfo.StreamNameShareReadWrite:
                return(Encoding.UTF8.GetBytes(await cloud.Nodes.ShareNode(item.Id, NodeShareType.ReadWrite)));

            default:
                return(new byte[0]);
            }
        }
        public CacheEntry AddExisting(FSItem item, string sourcePath = null)
        {
            var newentry = new CacheEntry {
                Id = item.Id, AccessTime = DateTime.UtcNow, LinkPath = sourcePath
            };

            if (access.TryAdd(item.Id, newentry))
            {
                TotalSizeIncrease(item.Length);
                return(newentry);
            }

            return(null);
        }
Beispiel #29
0
        public async Task AddOverwrite(FSItem item)
        {
            var info = new UploadInfo(item)
            {
                Overwrite = true
            };

            var path = Path.Combine(cachePath, item.Id);

            await WriteInfo(path + ".info", info);

            leftUploads.Add(info);
            allUploads.TryAdd(info.Id, info);
            await OnUploadAdded?.Invoke(info);
        }
Beispiel #30
0
        public NewFileBlockWriter OpenNew(FSItem item)
        {
            var path   = Path.Combine(cachePath, item.Id);
            var result = new NewFileBlockWriter(item, path);

            result.OnClose = async() =>
            {
                if (!result.Cancelled)
                {
                    await AddUpload(item);
                }
            };

            return(result);
        }