Exemplo n.º 1
0
        public async Task <byte[]> GetChunkBytes(VChunk chunk, CancellationToken cancellationToken)
        {
            var chunkPath = Path.Combine(UserSettings.Default.OutputDirectory, ".data", $"{chunk.Id}.chunk");

            byte[] chunkBytes;

            if (File.Exists(chunkPath))
            {
                chunkBytes         = new byte[chunk.Size];
                await using var fs = new FileStream(chunkPath, FileMode.Open, FileAccess.Read, FileShare.Read);
                await fs.ReadAsync(chunkBytes, cancellationToken).ConfigureAwait(false);
            }
            else
            {
                using var response = await _client.GetAsync(chunk.GetUrl(), cancellationToken).ConfigureAwait(false);

                if (response.StatusCode == HttpStatusCode.OK)
                {
                    chunkBytes = await response.Content.ReadAsByteArrayAsync(cancellationToken).ConfigureAwait(false);

                    await using var fs = new FileStream(chunkPath, FileMode.Create, FileAccess.Write, FileShare.Read);
                    await response.Content.CopyToAsync(fs, cancellationToken).ConfigureAwait(false);
                }
                else
                {
                    chunkBytes = null; // Maybe add logging?
                }
            }

            return(chunkBytes);
        }
Exemplo n.º 2
0
        public async ValueTask PrefetchChunk(VChunk chunk, CancellationToken cancellationToken)
        {
            var chunkPath = Path.Combine(UserSettings.Default.OutputDirectory, ".data", $"{chunk.Id}.chunk");

            if (File.Exists(chunkPath))
            {
                return;
            }
            using var response = await _client.GetAsync(chunk.GetUrl(), cancellationToken).ConfigureAwait(false);

            if (response.StatusCode == HttpStatusCode.OK)
            {
                await using var fileStream = new FileStream(chunkPath, FileMode.Create, FileAccess.Write, FileShare.Read);
                await response.Content.CopyToAsync(fileStream, cancellationToken).ConfigureAwait(false);
            }
        }