Created
May 18, 2015 12:01
-
-
Save anonymous/bf6abd6d60b4abdc2d75 to your computer and use it in GitHub Desktop.
ImageProcessor service for Umbraco.Storage.S3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace ImageProcessor.Web.Services | |
{ | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Net; | |
using System.Threading.Tasks; | |
using System.Web; | |
using ImageProcessor.Web.Helpers; | |
/// <summary> | |
/// The remote image service. | |
/// </summary> | |
public class UmbracoStorageS3Service : IImageService | |
{ | |
/// <summary> | |
/// The prefix for the given implementation. | |
/// </summary> | |
private string prefix = ""; | |
/// <summary> | |
/// Initializes a new instance of the <see cref="UmbracoStorageS3Service"/> class. | |
/// </summary> | |
public UmbracoStorageS3Service() | |
{ | |
this.Settings = new Dictionary<string, string> | |
{ | |
{ "MaxBytes", "4194304" }, | |
{ "Timeout", "30000" }, | |
{ "BucketPath", "http://bucket.s3.amazonaws.com/" } | |
}; | |
this.WhiteList = new Uri[] { }; | |
} | |
/// <summary> | |
/// Gets or sets the prefix 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 Prefix | |
{ | |
get | |
{ | |
return this.prefix; | |
} | |
set | |
{ | |
this.prefix = value; | |
} | |
} | |
/// <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 or sets the white list of <see cref="System.Uri"/>. | |
/// </summary> | |
public Uri[] WhiteList { get; set; } | |
public bool IsValidRequest(string path) | |
{ | |
return ImageHelpers.IsValidImageExtension(path); | |
} | |
/// <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 bucketPath = this.Settings["BucketPath"].EndsWith("/") ? this.Settings["BucketPath"] : this.Settings["BucketPath"] + "/"; | |
string relativePath = id.ToString().Substring(id.ToString().IndexOf("media")).Replace("\\", "/"); | |
Uri uri = new Uri(bucketPath + relativePath); | |
RemoteFile remoteFile = new RemoteFile(uri) | |
{ | |
MaxDownloadSize = int.Parse(this.Settings["MaxBytes"]), | |
TimeoutLength = int.Parse(this.Settings["Timeout"]) | |
}; | |
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(); | |
} | |
else | |
{ | |
throw new HttpException(404, "No image exists at " + uri); | |
} | |
} | |
} | |
} | |
return buffer; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment