示例#1
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);
        }
示例#2
0
 public void AddAsLink(FSItem item, string path)
 {
     Directory.CreateDirectory(cachePath);
     HardLink.Create(path, Path.Combine(cachePath, item.Id));
     AddExisting(item);
 }