Created
October 18, 2018 12:13
-
-
Save bjarnef/4a4a077b78a3125cd1ef61d068fcb86b to your computer and use it in GitHub Desktop.
Custom fallback image using LocalFileImageService
This file contains hidden or 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
public class CustomLocalFileImageService : LocalFileImageService | |
{ | |
/// <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 override async Task<byte[]> GetImage(object id) | |
{ | |
string path = id.ToString(); | |
byte[] buffer; | |
// Check to see if the file exists. | |
if (!File.Exists(path)) | |
{ | |
path = System.Web.Hosting.HostingEnvironment.MapPath(@"~/dist/img/no-product-image.png"); | |
if(!File.Exists(path)) | |
throw new HttpException((int)HttpStatusCode.NotFound, "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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment