public async Task DownloadAsync(string fileName, Stream stream, BytesRange range = default, CancellationToken ct = default) { Guard.NotNull(stream, nameof(stream)); var name = GetFileName(fileName, nameof(fileName)); var client = await GetClientAsync(ct); try { await using (var ftpStream = await client.OpenReadAsync(name, range.From ?? 0, ct)) { await ftpStream.CopyToAsync(stream, range, ct, false); } } catch (FtpException ex) when(IsNotFound(ex)) { throw new AssetNotFoundException(fileName, ex); } finally { pool.Return(client); } }
public async Task DownloadAsync(string fileName, Stream stream, BytesRange range = default, CancellationToken ct = default) { Guard.NotNull(stream, nameof(stream)); var key = GetKey(fileName, nameof(fileName)); try { var request = new GetObjectRequest { BucketName = options.Bucket, Key = key }; if (range.IsDefined) { request.ByteRange = new ByteRange(range.ToString()); } using (var response = await s3Client.GetObjectAsync(request, ct)) { await response.ResponseStream.CopyToAsync(stream, BufferSize, ct); } } catch (AmazonS3Exception ex) when(ex.StatusCode == HttpStatusCode.NotFound) { throw new AssetNotFoundException(fileName, ex); } }
private static void TestBytesRange(BytesRange sut, long?from, long?to, long length, bool defined, string formatted) { Assert.Equal(from, sut.From); Assert.Equal(to, sut.To); Assert.Equal(length, sut.Length); Assert.Equal(defined, sut.IsDefined); Assert.Equal(formatted, sut.ToString()); }
public static async Task CopyToAsync(this Stream source, Stream target, BytesRange range, CancellationToken ct, bool skip = true) { var buffer = Pool.Rent(8192); try { if (skip && range.From > 0) { source.Seek(range.From.Value, SeekOrigin.Begin); } var bytesLeft = range.Length; while (true) { if (bytesLeft <= 0) { return; } ct.ThrowIfCancellationRequested(); var readLength = (int)Math.Min(buffer.Length, bytesLeft); var read = await source.ReadAsync(buffer, 0, readLength, ct); bytesLeft -= read; if (read == 0) { return; } ct.ThrowIfCancellationRequested(); await target.WriteAsync(buffer, 0, read, ct); } } finally { Pool.Return(buffer); } }
public async Task DownloadAsync(string fileName, Stream stream, BytesRange range = default, CancellationToken ct = default) { Guard.NotNull(stream, nameof(stream)); var name = GetFileName(fileName, nameof(fileName)); try { var options = range.IsDefined ? DownloadSeekable : DownloadDefault; await using (var readStream = await bucket.OpenDownloadStreamAsync(name, options, ct)) { await readStream.CopyToAsync(stream, range, ct); } } catch (GridFSFileNotFoundException ex) { throw new AssetNotFoundException(fileName, ex); } }
public async Task DownloadAsync(string fileName, Stream stream, BytesRange range = default, CancellationToken ct = default) { var name = GetFileName(fileName, nameof(fileName)); try { var downloadOptions = new DownloadObjectOptions(); if (range.IsDefined) { downloadOptions.Range = new RangeHeaderValue(range.From, range.To); } await storageClient.DownloadObjectAsync(bucketName, name, stream, downloadOptions, ct); } catch (GoogleApiException ex) when(ex.HttpStatusCode == HttpStatusCode.NotFound) { throw new AssetNotFoundException(fileName, ex); } }
public async Task DownloadAsync(string fileName, Stream stream, BytesRange range = default, CancellationToken ct = default) { Guard.NotNull(stream, nameof(stream)); var name = GetFileName(fileName, nameof(fileName)); try { var blob = blobContainer.GetBlobClient(name); var result = await blob.DownloadStreamingAsync(new HttpRange(range.From ?? 0, range.To), cancellationToken : ct); await using (result.Value.Content) { await result.Value.Content.CopyToAsync(stream, ct); } } catch (RequestFailedException ex) when(ex.Status == 404) { throw new AssetNotFoundException(fileName, ex); } }
public async Task DownloadAsync(string fileName, Stream stream, BytesRange range = default, CancellationToken ct = default) { Guard.NotNull(stream, nameof(stream)); var file = GetFile(fileName, nameof(fileName)); try { await using (var fileStream = file.OpenRead()) { await fileStream.CopyToAsync(stream, range, ct); } } catch (FileNotFoundException ex) { throw new AssetNotFoundException(fileName, ex); } catch (DirectoryNotFoundException ex) { throw new AssetNotFoundException(fileName, ex); } }
public virtual async Task DownloadAsync(string fileName, Stream stream, BytesRange range = default, CancellationToken ct = default) { Guard.NotNull(stream, nameof(stream)); var name = GetFileName(fileName, nameof(fileName)); if (!streams.TryGetValue(name, out var sourceStream)) { throw new AssetNotFoundException(fileName); } using (await readerLock.LockAsync()) { try { await sourceStream.CopyToAsync(stream, range, ct); } finally { sourceStream.Position = 0; } } }
public void Should_create_with_from_and_to() { var sut = new BytesRange(3, 15); TestBytesRange(sut, 3, 15, 13, true, "bytes=3-15"); }
public void Should_create_default_manually() { var sut = new BytesRange(null, null); TestBytesRange(sut, null, null, long.MaxValue, false, null); }
public Task DownloadAsync(string fileName, Stream stream, BytesRange range = default, CancellationToken ct = default) { throw new NotSupportedException(); }
public void Should_create_with_from() { var sut = new BytesRange(12, null); TestBytesRange(sut, 12, null, long.MaxValue - 11, true, "bytes=12-"); }
public void Should_create_with_to() { var sut = new BytesRange(null, 12); TestBytesRange(sut, null, 12, 13, true, "bytes=-12"); }
public void Should_fix_length_for_negative_range() { var sut = new BytesRange(-5, -3); TestBytesRange(sut, -5, -3, 0, false, null); }
public void Should_fix_length() { var sut = new BytesRange(5, 3); TestBytesRange(sut, 5, 3, 0, false, null); }
public void Should_create_with_single_byte() { var sut = new BytesRange(3, 3); TestBytesRange(sut, 3, 3, 1, true, "bytes=3-3"); }