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