Beispiel #1
0
        public async Task WriteAsync(string key, ICachedImage cachedImage)
        {
            string[] parts = KeyToPath(key);

            //create folder structure
            DirectoryInfo sub = Directory.CreateDirectory(Path.Combine(parts.Take(parts.Length - 1).ToArray()));

            //write to file
            FileInfo fileInfoMetadata = new FileInfo(Path.Combine(sub.FullName, parts.Last() + ".meta"));

            //write metadata
            string json = JsonSerializer.Serialize(cachedImage.Metadata, new JsonSerializerOptions()
            {
                WriteIndented = true
            });
            Stream metadataStream = new MemoryStream(Encoding.UTF8.GetBytes(json));

            using (Stream fs = fileInfoMetadata.OpenWrite())
            {
                await metadataStream.CopyToAsync(fs);
            }

            //write data
            FileInfo fileInfoData = new FileInfo(Path.Combine(sub.FullName, parts.Last()));

            using (Stream fs = fileInfoData.OpenWrite())
                using (Stream cachedImageStream = await cachedImage.OpenReadAsync())
                {
                    await cachedImageStream.CopyToAsync(fs);
                }
        }
Beispiel #2
0
        public override async Task <OriginalData> GetAsync(string source, ICachedImage existingCachedImage)
        {
            GetObjectRequest request = new GetObjectRequest()
            {
                BucketName = Options.BucketName,
                Key        = source
            };

            if (existingCachedImage != null)
            {
                request.EtagToNotMatch = $"\"{existingCachedImage.Metadata.Cache.ETag}\"";
            }

            GetObjectResponse result = await Client.GetObjectAsync(request);

            if (result.HttpStatusCode == System.Net.HttpStatusCode.NotModified)
            {
                return(null);
            }

            return(new OriginalData(
                       result.Headers.ContentType,
                       await result.ResponseStream.ToByteArrayAsync(),
                       new CacheSettings()
            {
                ETag = result.ETag.GetTagUnquoted()
            }));
        }
Beispiel #3
0
        public override async Task <OriginalImage> GetAsync(string source, ICachedImage existingCachedImage)
        {
            string url = $"https://i.ytimg.com/vi/{source}/maxresdefault.jpg";

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, url);

            if (existingCachedImage != null)
            {
                if (existingCachedImage.Metadata.Cache.ETag != null)
                {
                    request.Headers.IfNoneMatch.Add(new EntityTagHeaderValue($"\"{existingCachedImage.Metadata.Cache.ETag}\""));
                }
            }

            HttpResponseMessage response = await Client.SendAsync(request);

            if (response.StatusCode == HttpStatusCode.NotModified)
            {
                return(null);
            }

            response.EnsureSuccessStatusCode();

            byte[] data = await response.Content.ReadAsByteArrayAsync();

            return(new OriginalImage(response.Content.Headers.ContentType.MediaType, data, new CacheSettings(response)));
        }
        public override async Task <OriginalImage> GetAsync(string source, ICachedImage existingCachedImage)
        {
            BlobClient blob = Client.GetBlobClient(source);

            BlobRequestConditions conditions = new BlobRequestConditions();

            if (existingCachedImage != null)
            {
                conditions.IfNoneMatch = new ETag(existingCachedImage.Metadata.Cache.ETag);
            }

            var response = await blob.DownloadAsync(conditions : conditions);

            if (response.GetRawResponse().Status == (int)System.Net.HttpStatusCode.NotModified)
            {
                return(null);
            }

            return(new OriginalImage(
                       response.Value.ContentType,
                       await response.Value.Content.ToByteArrayAsync(),
                       new CacheSettings()
            {
                ETag = response.Value.Details.ETag.ToString().GetTagUnquoted()
            }));
        }
Beispiel #5
0
        public override async Task <OriginalImage> GetAsync(string source, ICachedImage existingCachedImage)
        {
            IFileInfo fileInfo = FileProvider.GetFileInfo(source);

            if (fileInfo.Exists == false)
            {
                throw new Exception("file not found: " + source);
            }

            string etag = (fileInfo.Length ^ fileInfo.LastModified.UtcTicks).ToString();

            if (existingCachedImage != null)
            {
                if (existingCachedImage.Metadata.Cache.ETag == etag)
                {
                    return(null);
                }
            }

            MemoryStream mem = new MemoryStream((int)fileInfo.Length);

            using (Stream stream = fileInfo.CreateReadStream())
            {
                await stream.CopyToAsync(mem);
            }

            string mimeType = MimeTypeHelper.GetMimeTypeByExtension(fileInfo.Name);

            return(new OriginalImage(mimeType, mem.ToArray(), new CacheSettings()
            {
                ETag = etag
            }));
        }
Beispiel #6
0
        public async Task WriteAsync(string key, ICachedImage cachedImage)
        {
            string[] parts = SplitKey(key);

            //store transformed image
            DirectoryInfo sub = Directory.CreateDirectory(Path.Combine(new[] { HostingEnvironment.ContentRootPath }.Concat(new[] { Settings.Value.Folder }).Concat(parts.Take(parts.Length - 1)).ToArray()));

            //write to file
            FileInfo fileInfoMetadata = new FileInfo(Path.Combine(sub.FullName, parts.Last() + ".meta"));

            //write metadata
            string json = JsonSerializer.Serialize(cachedImage.Metadata, new JsonSerializerOptions()
            {
                WriteIndented = true
            });

            byte[] metadataBuffer = Encoding.UTF8.GetBytes(json);

            using (Stream fs = fileInfoMetadata.OpenWrite())
            {
                await fs.WriteAsync(metadataBuffer, 0, metadataBuffer.Length);
            }

            //write data
            FileInfo fileInfoData = new FileInfo(Path.Combine(sub.FullName, parts.Last()));

            using (Stream fs = fileInfoData.OpenWrite())
                using (Stream cachedImageStream = await cachedImage.OpenReadAsync())
                {
                    await cachedImageStream.CopyToAsync(fs);
                }
        }
Beispiel #7
0
        public async Task WriteAsync(string key, ICachedImage cachedImage)
        {
            ImageMetadataModel model = new ImageMetadataModel()
            {
                Created      = cachedImage.Metadata.Created,
                Cache        = cachedImage.Metadata.Cache,
                Hash         = cachedImage.Metadata.Hash,
                Key          = cachedImage.Metadata.Key,
                LoaderSource = cachedImage.Metadata.LoaderSource,
                Filters      = cachedImage.Metadata.Filters,
                LoaderType   = cachedImage.Metadata.LoaderType,
                MimeType     = cachedImage.Metadata.MimeType,
                Width        = cachedImage.Metadata.Width,
                Height       = cachedImage.Metadata.Height,
                DPR          = cachedImage.Metadata.DPR,
                FileLength   = cachedImage.Metadata.FileLength
            };

            //upload image metadata
            await ImageMetadatas.ReplaceOneAsync(Builders <ImageMetadataModel> .Filter.Where(x => x.Key == key), model, new ReplaceOptions()
            {
                IsUpsert = true
            });

            using (Stream cachedImageStream = await cachedImage.OpenReadAsync())
            {
                //upload transformed image
                await ImageBuffer.UploadFromStreamAsync(key, cachedImageStream);
            }
        }
 public void OnCachedImageCreated(ICachedImage cachedImage)
 {
     lock (_lock)
     {
         AnalyticsData.CreatedImages++;
         AnalyticsData.CreatedImagesInBytes += cachedImage.Metadata.FileLength;
     }
 }
 public void OnResponseCompleted(ICachedImage cachedImage)
 {
     lock (_lock)
     {
         AnalyticsData.TransferedImages++;
         AnalyticsData.TransferedImagesInBytes += cachedImage.Metadata.FileLength;
     }
 }
Beispiel #10
0
        public override async Task <OriginalImage> GetAsync(string source, ICachedImage existingCachedImage)
        {
            string url = $"https://www.gravatar.com/avatar/{source}?size=512";

            HttpResponseMessage response = await Client.GetAsync(url);

            byte[] data = await response.Content.ReadAsByteArrayAsync();

            return(new OriginalImage(response.Content.Headers.ContentType.MediaType, data, new CacheSettings(response)));
        }
Beispiel #11
0
        public async Task WriteAsync(string key, ICachedImage cachedImage)
        {
            string json = JsonSerializer.Serialize(cachedImage.Metadata);

            await Cache.SetStringAsync($"{KeyPrefix}{key}#meta", json);

            using (Stream cachedImageStream = await cachedImage.OpenReadAsync())
            {
                MemoryStream mem = new MemoryStream();
                await cachedImageStream.CopyToAsync(mem);

                await Cache.SetAsync($"{KeyPrefix}{key}", mem.ToArray());
            }
        }
Beispiel #12
0
        /// <summary>
        /// Download
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public override async Task <OriginalImage> GetAsync(string source, ICachedImage existingCachedImage)
        {
            //is relative url?
            if (Regex.Match(source, "^https?://", RegexOptions.Compiled).Success == false)
            {
                if (string.IsNullOrWhiteSpace(Options.DefaultBaseUrl) == false)
                {
                    source = $"{Options.DefaultBaseUrl.TrimEnd('/')}/{source}";
                }
                else
                {
                    //create absolute url
                    source = $"{HttpContextAccessor.HttpContext.Request.Scheme}://{HttpContextAccessor.HttpContext.Request.Host}/{source}";
                }
            }

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, new Uri(source));

            if (existingCachedImage != null)
            {
                if (existingCachedImage.Metadata.Cache.ETag != null)
                {
                    request.Headers.IfNoneMatch.Add(new EntityTagHeaderValue($"\"{existingCachedImage.Metadata.Cache.ETag}\""));
                }
            }

            HttpResponseMessage response = await HttpClient.SendAsync(request);

            if (response.StatusCode == HttpStatusCode.NotModified)
            {
                return(null);
            }

            response.EnsureSuccessStatusCode();

            byte[] data = await response.Content.ReadAsByteArrayAsync();

            string mimeType = response.Content.Headers.ContentType?.MediaType;

            if (mimeType == null)
            {
                throw new Exception("no content-type available");
            }

            return(new OriginalImage(mimeType, data, new CacheSettings(response)));
        }
Beispiel #13
0
 public abstract Task <OriginalImage> GetAsync(string source, ICachedImage existingCachedImage);
 public void OnResponseSending(HttpResponse response, ICachedImage cachedImage)
 {
 }
Beispiel #15
0
 public void Add(object id, ICachedImage image)
 {
     _cache[id] = image;
 }