public static async Task <ResultT> LoadAsync <ResultT>(
            this ICacheSourceLayer layer,
            IDeserializer <ResultT> deserializer,
            ContentReference fileRef,
            IProgress prog = null)
        {
            if (layer is null)
            {
                throw new ArgumentNullException(nameof(layer));
            }

            if (deserializer is null)
            {
                throw new ArgumentNullException(nameof(deserializer));
            }

            if (fileRef is null)
            {
                throw new ArgumentNullException(nameof(fileRef));
            }

            var progs = prog.Split("Read", "Decode");

            using var stream = await layer
                               .GetStreamAsync(fileRef, progs[0])
                               .ConfigureAwait(false);

            using var progStream = new ProgressStream(stream, stream.Length, progs[1], false);
            return(deserializer.Deserialize(progStream));
        }
        public static async Task SendContentAsync(
            this HttpListenerResponse response,
            ICacheSourceLayer layer,
            ContentReference fileRef)
        {
            if (layer is null)
            {
                throw new ArgumentNullException(nameof(layer));
            }

            if (fileRef is null)
            {
                throw new ArgumentNullException(nameof(fileRef));
            }

            response.SetFileName(fileRef.ContentType, fileRef.FileName);

            using var stream = await layer
                               .GetStreamAsync(fileRef, null)
                               .ConfigureAwait(false);

            await response
            .SendStreamAsync(fileRef.ContentType, stream)
            .ConfigureAwait(false);
        }
        public static Task <Stream> OpenAsync(
            this ICacheSourceLayer layer,
            ContentReference fileRef)
        {
            if (layer is null)
            {
                throw new ArgumentNullException(nameof(layer));
            }

            if (fileRef is null)
            {
                throw new ArgumentNullException(nameof(fileRef));
            }

            return(layer.GetStreamAsync(fileRef, null));
        }
Exemple #4
0
        public static async Task CopyToAsync(
            this ICacheSourceLayer fromLayer,
            ContentReference fromRef,
            ICacheDestinationLayer toLayer,
            ContentReference toRef,
            IProgress prog = null)
        {
            if (fromLayer is null)
            {
                throw new ArgumentNullException(nameof(fromLayer));
            }

            if (fromRef is null)
            {
                throw new ArgumentNullException(nameof(fromRef));
            }

            if (toLayer is null)
            {
                throw new ArgumentNullException(nameof(toLayer));
            }

            if (toRef is null)
            {
                throw new ArgumentNullException(nameof(toRef));
            }

            if (fromLayer.IsCached(fromRef))
            {
                using var inStream = await fromLayer
                                     .GetStreamAsync(fromRef, prog)
                                     .ConfigureAwait(false);

                using var outStream = toLayer.Create(toRef);
                await inStream
                .CopyToAsync(outStream)
                .ConfigureAwait(false);
            }
        }