internal override async Task <FileStorageCacheRecord> GetEntryAsync(StorageItemDownloadInfo item, Stream stream, CancellationToken?cancellationToken = null) { var tempFileName = CreateMD5(Guid.NewGuid().ToString()); var split = tempFileName.Select((c, index) => new { c, index }) .GroupBy(x => x.index / 2) .Select(group => group.Select(elem => elem.c)) .Select(chars => new string(chars.ToArray())).ToArray(); var dirPath = Path.Combine(split); var directoryPath = Path.Combine(Options.CacheDirectoryPath, dirPath); if (!Directory.Exists(directoryPath)) { Directory.CreateDirectory(directoryPath); } var filePath = Path.Combine(directoryPath, Guid.NewGuid().ToString()); var fileStream = File.OpenWrite(filePath); if (!fileStream.CanWrite) { throw new Exception($"Can't write to file {filePath}"); } await stream.CopyToAsync(fileStream, cancellationToken ?? CancellationToken.None); fileStream.Close(); return(new FileStorageCacheRecord(item.Metadata, item.FileSize, item.Date, filePath)); }
internal override async Task <InMemoryStorageCacheRecord> GetEntryAsync(StorageItemDownloadInfo item, Stream stream, CancellationToken?cancellationToken = null) { using var memoryStream = new MemoryStream(); await stream.CopyToAsync(memoryStream, cancellationToken ?? CancellationToken.None); return (new InMemoryStorageCacheRecord(item.Metadata, item.FileSize, item.Date, memoryStream.ToArray())); }
internal override Task <StorageItemDownloadInfo?> DoGetFileAsync(string path, CancellationToken?cancellationToken = null) { StorageItemDownloadInfo?result = null; var fullPath = Path.Combine(Options.StoragePath, path); var fileInfo = new FileInfo(fullPath); if (fileInfo.Exists) { result = new StorageItemDownloadInfo(fileInfo.Length, fileInfo.LastWriteTimeUtc, () => new FileStream(fullPath, FileMode.Open)); } return(Task.FromResult(result)); }
internal abstract Task <TRecord> GetEntryAsync(StorageItemDownloadInfo item, Stream stream, CancellationToken?cancellationToken = null);