Beispiel #1
0
        Result AddItem(HttpResponseMessage response, Uri uri, CancellationToken token)
        {
            var timestamp = Timestamp.FromResponse(uri, response);

#if NET5_0
            var meta = MetaData.FromEnumerables(response.Headers, response.Content.Headers, response.TrailingHeaders);
#else
            var meta = MetaData.FromEnumerables(response.Headers, response.Content.Headers);
#endif
            var tempFile = FilePair.GetTemp();
            try
            {
                using var httpStream = response.Content.ReadAsStream(token);
                using (var contentFileStream = FileEx.OpenWrite(tempFile.Content))
                    using (var metaFileStream = FileEx.OpenWrite(tempFile.Meta))
                        using (var writer = new Utf8JsonWriter(metaFileStream))
                        {
                            JsonSerializer.Serialize(writer, meta);
                            httpStream.CopyTo(contentFileStream);
                        }

                return(BuildResult(timestamp, tempFile));
            }
            finally
            {
                tempFile.Delete();
            }
        }
Beispiel #2
0
        async Task <Result> InnerAddItemAsync(
            CancellationToken token,
            Func <CancellationToken, Task <Stream> > httpContentFunc,
            MetaData meta,
            Timestamp timestamp)
        {
            var tempFile = FilePair.GetTemp();

            try
            {
#if NET5_0
                await using var httpStream = await httpContentFunc(token);

                await using (var contentFileStream = FileEx.OpenWrite(tempFile.Content))
                    await using (var metaFileStream = FileEx.OpenWrite(tempFile.Meta))
                    {
#else
                using var httpStream = await httpContentFunc(token);

                using (var contentFileStream = FileEx.OpenWrite(tempFile.Content))
                    using (var metaFileStream = FileEx.OpenWrite(tempFile.Meta))
                    {
#endif
                        await JsonSerializer.SerializeAsync(metaFileStream, meta, cancellationToken : token);

                        await httpStream.CopyToAsync(contentFileStream, token);
                    }

                return(BuildResult(timestamp, tempFile));
            }
            finally
            {
                tempFile.Delete();
            }
        }