Ejemplo n.º 1
0
        public async Task DeleteFile(string filePath)
        {
            var item = await FetchNode(filePath);

            if (item != null)
            {
                if (item.IsDir)
                {
                    throw new InvalidOperationException("Not file");
                }

                await DeleteItem(filePath, item);

                itemsTreeCache.DeleteFile(filePath);
            }
            else
            {
                throw new FileNotFoundException();
            }

            try
            {
                SmallFilesCache.Delete(item);
            }
            catch (FileNotFoundException)
            {
                Log.Trace("Skip, File is not in SmallFilesCache");
            }
        }
Ejemplo n.º 2
0
        public async Task <FileInformation?> GetItemInfo(string fileName)
        {
            var item = await FetchNode(fileName);

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

            var result = new FileInformation
            {
                Length         = item.Length,
                FileName       = item.Name,
                Attributes     = item.IsDir ? FileAttributes.Directory : FileAttributes.Normal,
                LastAccessTime = item.LastAccessTime,
                LastWriteTime  = item.LastWriteTime,
                CreationTime   = item.CreationTime
            };

            var info = SmallFilesCache.GetItemInfo(item);

            if (info != null)
            {
                result.LastAccessTime = item.LastAccessTime > info.LastAccessTimeUtc
                    ? item.LastAccessTime
                    : info.LastAccessTimeUtc;
                result.LastWriteTime = item.LastWriteTime > info.LastWriteTimeUtc
                    ? item.LastWriteTime
                    : info.LastWriteTimeUtc;
            }

            return(result);
        }
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
#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.º 5
0
 public async Task ClearSmallFilesCache()
 {
     await SmallFilesCache.ClearAllInBackground();
 }
Ejemplo n.º 6
0
        public async Task <IBlockStream> OpenFile(string filePath, FileMode mode, FileAccess fileAccess, FileShare share, FileOptions options)
        {
            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)
                {
                    OnClose =
                        async() =>
                    {
                        await
                        onStatisticsUpdated(
                            cloud,
                            StatisticUpdateReason.DownloadFinished,
                            new DownloadStatisticInfo(item));
                    }
                };

                return(buffered);
            }

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

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

                if (dirItem == null)
                {
                    throw new FileNotFoundException($"Parent folder not found: {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;

                return(new NewFileBlockWriter(null, string.Empty));
            }

            await WaitForReal(item, 25000);

            if (mode == FileMode.Create || mode == FileMode.Truncate)
            {
                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;

                return(new NewFileBlockWriter(null, string.Empty));
            }

            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);
                            }

                            try
                            {
                                File.Move(olditemPath, newitemPath);
                                SmallFilesCache.AddExisting(it);
                            }
                            catch (Exception e)
                            {
                                Log.Error(e);
                            }
                        }

                        await uploadService.AddOverwrite(it);
                    };

                    return(file);
                }

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

            return(null);
        }