Ejemplo n.º 1
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());
            }
        }
Ejemplo n.º 2
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);
                }
            }
        }
Ejemplo n.º 3
0
        private bool disposedValue; // To detect redundant calls

        public FSProvider(IHttpCloud cloud, StatisticUpdateDelegate statisticUpdate)
        {
            onStatisticsUpdated = statisticUpdate;

            this.cloud      = cloud;
            SmallFilesCache = new SmallFilesCache(cloud)
            {
                OnDownloadStarted =
                    info =>
                {
                    onStatisticsUpdated(cloud, StatisticUpdateReason.DownloadAdded, new DownloadStatisticInfo(info));
                },
                OnDownloaded =
                    info =>
                {
                    onStatisticsUpdated(
                        cloud,
                        StatisticUpdateReason.DownloadFinished,
                        new DownloadStatisticInfo(info));
                },
                OnDownloadFailed =
                    info =>
                {
                    onStatisticsUpdated(cloud, StatisticUpdateReason.DownloadFailed, new DownloadStatisticInfo(info));
                }
            };

            uploadService = new UploadService(2, cloud)
            {
                OnUploadFailed   = UploadFailed,
                OnUploadFinished = UploadFinished,
                OnUploadProgress =
                    async(item, done) =>
                {
                    await onStatisticsUpdated(
                        cloud,
                        StatisticUpdateReason.Progress,
                        new UploadStatisticInfo(item) { Done = done });
                },
                OnUploadAdded = async item =>
                {
                    itemsTreeCache.Add(item.ToFSItem());
                    await onStatisticsUpdated(cloud, StatisticUpdateReason.UploadAdded, new UploadStatisticInfo(item));
                },
                OnUploadState = async(item, state) =>
                {
                    await onStatisticsUpdated(
                        cloud,
                        StatisticUpdateReason.UploadState,
                        new UploadStatisticInfo(item) { State = state });
                }
            };

            uploadService.Start();
        }
Ejemplo n.º 4
0
        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    itemsTreeCache.Dispose();
                    UploadService.Dispose();
                }

                // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
                // TODO: set large fields to null.
                disposedValue = true;
            }
        }
Ejemplo n.º 5
0
 public void Stop()
 {
     UploadService.Stop();
 }
Ejemplo n.º 6
0
#pragma warning disable RECS0154 // Parameter is never used
        public async Task <IBlockStream> OpenFile(string filePath, FileMode mode, FileAccess fileAccess, FileShare share, FileOptions options)
#pragma warning restore RECS0154 // Parameter is never used
        {
            var item = await FetchNode(filePath);

            if (fileAccess == FileAccess.Read)
            {
                if (item == null)
                {
                    return(null);
                }

                Log.Trace($"Opening {filePath} for Read");

                if (!item.IsUploading && item.Length < SmallFileSizeLimit)
                {
                    return(SmallFilesCache.OpenReadWithDownload(item));
                }

                var result = SmallFilesCache.OpenReadCachedOnly(item);
                if (result != null)
                {
                    return(result);
                }

                await WaitForReal(item, 25000);

                await onStatisticsUpdated(cloud, StatisticUpdateReason.DownloadAdded, new DownloadStatisticInfo(item));

                var buffered = new BufferedHttpCloudBlockReader(item, cloud);
                buffered.OnClose = async() =>
                {
                    await onStatisticsUpdated(cloud, StatisticUpdateReason.DownloadFinished, new DownloadStatisticInfo(item));
                };

                return(buffered);
            }

            if (item == null || item.Length == 0)
            {
                Log.Trace($"Creating {filePath} as New because mode:{mode} and {((item == null) ? "item is null" : "length is 0")}");

                var dir     = Path.GetDirectoryName(filePath);
                var name    = Path.GetFileName(filePath);
                var dirItem = await FetchNode(dir);

                item = FSItem.MakeUploading(filePath, Guid.NewGuid().ToString(), dirItem.Id, 0);

                var file = UploadService.OpenNew(item);
                SmallFilesCache.AddAsLink(item, file.UploadCachePath);

                itemsTreeCache.Add(item);

                return(file);
            }

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

            await WaitForReal(item, 25000);

            if ((mode == FileMode.Create || mode == FileMode.Truncate) && item.Length > 0)
            {
                Log.Trace($"Opening {filePath} as Truncate because mode:{mode} and length {item.Length}");
                item.Length = 0;
                SmallFilesCache.Delete(item);
                item.MakeUploading();
                var file = UploadService.OpenTruncate(item);

                return(file);
            }

            if (mode == FileMode.Open || mode == FileMode.Append || mode == FileMode.OpenOrCreate)
            {
                Log.Trace($"Opening {filePath} as ReadWrite because mode:{mode} and length {item.Length}");
                if (item.Length < SmallFileSizeLimit)
                {
                    var file = SmallFilesCache.OpenReadWrite(item);
                    file.OnChangedAndClosed = async(it, path) =>
                    {
                        it.LastWriteTime = DateTime.UtcNow;

                        if (!it.IsUploading)
                        {
                            it.MakeUploading();
                            var olditemPath = Path.Combine(SmallFilesCache.CachePath, item.Id);
                            var newitemPath = Path.Combine(UploadService.CachePath, item.Id);

                            if (File.Exists(newitemPath))
                            {
                                File.Delete(newitemPath);
                            }

                            HardLink.Create(olditemPath, newitemPath);
                            SmallFilesCache.AddExisting(it);
                        }

                        await UploadService.AddOverwrite(it);
                    };

                    return(file);
                }

                Log.Warn("File is too big for ReadWrite: " + filePath);
            }

            return(null);
        }
Ejemplo n.º 7
0
 public void CancelUpload(string id)
 {
     UploadService.CancelUpload(id);
 }