Ejemplo n.º 1
0
        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));
        }
Ejemplo n.º 2
0
        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()));
        }
Ejemplo n.º 3
0
        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));
        }
Ejemplo n.º 4
0
 internal abstract Task <TRecord> GetEntryAsync(StorageItemDownloadInfo item, Stream stream,
                                                CancellationToken?cancellationToken = null);