mirror of https://github.com/SixLabors/ImageSharp
Browse Source
TODO: Remote setting plus further integration. Former-commit-id: 360cab7a4ce988c81d10657df549496990d059c7pull/17/head
7 changed files with 662 additions and 11 deletions
@ -0,0 +1,51 @@ |
|||
// --------------------------------------------------------------------------------------------------------------------
|
|||
// <copyright file="IImageService.cs" company="James South">
|
|||
// Copyright (c) James South.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
// <summary>
|
|||
// Defines properties and methods for allowing retrieval of image from different means.
|
|||
// </summary>
|
|||
// --------------------------------------------------------------------------------------------------------------------
|
|||
|
|||
namespace ImageProcessor.Web.Services |
|||
{ |
|||
using System.Collections.Generic; |
|||
using System.Threading.Tasks; |
|||
|
|||
/// <summary>
|
|||
/// Defines properties and methods for allowing retrieval of image from different means.
|
|||
/// </summary>
|
|||
public interface IImageService |
|||
{ |
|||
/// <summary>
|
|||
/// Gets the key for the given implementation.
|
|||
/// <remarks>
|
|||
/// This value is used as a prefix for any image requests that should use this service.
|
|||
/// </remarks>
|
|||
/// </summary>
|
|||
string Key { get; } |
|||
|
|||
/// <summary>
|
|||
/// Gets a value indicating whether the image service requests files from
|
|||
/// the locally based file system.
|
|||
/// </summary>
|
|||
bool IsFileLocalService { get; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets any additional settings required by the service.
|
|||
/// </summary>
|
|||
Dictionary<string, string> Settings { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets the image using the given identifier.
|
|||
/// </summary>
|
|||
/// <param name="id">
|
|||
/// The value identifying the image to fetch.
|
|||
/// </param>
|
|||
/// <returns>
|
|||
/// The <see cref="System.Byte"/> array containing the image data.
|
|||
/// </returns>
|
|||
Task<byte[]> GetImage(object id); |
|||
} |
|||
} |
|||
@ -0,0 +1,86 @@ |
|||
// --------------------------------------------------------------------------------------------------------------------
|
|||
// <copyright file="LocalFileImageService.cs" company="James South">
|
|||
// Copyright (c) James South.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
// <summary>
|
|||
// The local file image service for retrieving images from the file system.
|
|||
// </summary>
|
|||
// --------------------------------------------------------------------------------------------------------------------
|
|||
|
|||
namespace ImageProcessor.Web.Services |
|||
{ |
|||
using System.Collections.Generic; |
|||
using System.IO; |
|||
using System.Threading.Tasks; |
|||
using System.Web; |
|||
|
|||
/// <summary>
|
|||
/// The local file image service for retrieving images from the file system.
|
|||
/// </summary>
|
|||
public class LocalFileImageService : IImageService |
|||
{ |
|||
/// <summary>
|
|||
/// Gets the key for the given implementation.
|
|||
/// <remarks>
|
|||
/// This value is used as a prefix for any image requests that should use this service.
|
|||
/// </remarks>
|
|||
/// </summary>
|
|||
public string Key |
|||
{ |
|||
get |
|||
{ |
|||
return string.Empty; |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets a value indicating whether the image service requests files from
|
|||
/// the locally based file system.
|
|||
/// </summary>
|
|||
public bool IsFileLocalService |
|||
{ |
|||
get |
|||
{ |
|||
return true; |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets any additional settings required by the service.
|
|||
/// </summary>
|
|||
public Dictionary<string, string> Settings { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets the image using the given identifier.
|
|||
/// </summary>
|
|||
/// <param name="id">
|
|||
/// The value identifying the image to fetch.
|
|||
/// </param>
|
|||
/// <returns>
|
|||
/// The <see cref="System.Byte"/> array containing the image data.
|
|||
/// </returns>
|
|||
public async Task<byte[]> GetImage(object id) |
|||
{ |
|||
string path = id.ToString(); |
|||
byte[] buffer; |
|||
|
|||
// Check to see if the file exists.
|
|||
// ReSharper disable once AssignNullToNotNullAttribute
|
|||
FileInfo fileInfo = new FileInfo(path); |
|||
|
|||
if (!fileInfo.Exists) |
|||
{ |
|||
throw new HttpException(404, "No image exists at " + path); |
|||
} |
|||
|
|||
using (FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, true)) |
|||
{ |
|||
buffer = new byte[file.Length]; |
|||
await file.ReadAsync(buffer, 0, (int)file.Length); |
|||
} |
|||
|
|||
return buffer; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,105 @@ |
|||
// --------------------------------------------------------------------------------------------------------------------
|
|||
// <copyright file="RemoteImageService.cs" company="James South">
|
|||
// Copyright (c) James South.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
// <summary>
|
|||
// The remote image service.
|
|||
// </summary>
|
|||
// --------------------------------------------------------------------------------------------------------------------
|
|||
|
|||
namespace ImageProcessor.Web.Services |
|||
{ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.IO; |
|||
using System.Net; |
|||
using System.Threading.Tasks; |
|||
|
|||
using ImageProcessor.Web.Helpers; |
|||
|
|||
/// <summary>
|
|||
/// The remote image service.
|
|||
/// </summary>
|
|||
public class RemoteImageService : IImageService |
|||
{ |
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="RemoteImageService"/> class.
|
|||
/// </summary>
|
|||
public RemoteImageService() |
|||
{ |
|||
this.Settings = new Dictionary<string, string>(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets the key for the given implementation.
|
|||
/// <remarks>
|
|||
/// This value is used as a prefix for any image requests that should use this service.
|
|||
/// </remarks>
|
|||
/// </summary>
|
|||
public string Key |
|||
{ |
|||
get |
|||
{ |
|||
return "remote.axd"; |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets a value indicating whether the image service requests files from
|
|||
/// the locally based file system.
|
|||
/// </summary>
|
|||
public bool IsFileLocalService |
|||
{ |
|||
get |
|||
{ |
|||
return false; |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets any additional settings required by the service.
|
|||
/// </summary>
|
|||
public Dictionary<string, string> Settings { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets the image using the given identifier.
|
|||
/// </summary>
|
|||
/// <param name="id">
|
|||
/// The value identifying the image to fetch.
|
|||
/// </param>
|
|||
/// <returns>
|
|||
/// The <see cref="System.Byte"/> array containing the image data.
|
|||
/// </returns>
|
|||
public async Task<byte[]> GetImage(object id) |
|||
{ |
|||
Uri uri = new Uri(id.ToString()); |
|||
RemoteFile remoteFile = new RemoteFile(uri, false); |
|||
byte[] buffer = { }; |
|||
|
|||
// Prevent response blocking.
|
|||
WebResponse webResponse = await remoteFile.GetWebResponseAsync().ConfigureAwait(false); |
|||
|
|||
using (MemoryStream memoryStream = new MemoryStream()) |
|||
{ |
|||
using (WebResponse response = webResponse) |
|||
{ |
|||
using (Stream responseStream = response.GetResponseStream()) |
|||
{ |
|||
if (responseStream != null) |
|||
{ |
|||
responseStream.CopyTo(memoryStream); |
|||
|
|||
// Reset the position of the stream to ensure we're reading the correct part.
|
|||
memoryStream.Position = 0; |
|||
|
|||
buffer = memoryStream.ToArray(); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
return buffer; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue