// --------------------------------------------------------------------------------------------------------------------
//
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
//
//
// The local file image service for retrieving images from the file system.
//
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Web.Services
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using System.Web;
///
/// The local file image service for retrieving images from the file system.
///
public class LocalFileImageService : IImageService
{
///
/// The prefix for the given implementation.
///
private string prefix = string.Empty;
///
/// Gets or sets the prefix for the given implementation.
///
/// This value is used as a prefix for any image requests that should use this service.
///
///
public string Prefix
{
get
{
return this.prefix;
}
set
{
this.prefix = value;
}
}
///
/// Gets a value indicating whether the image service requests files from
/// the locally based file system.
///
public bool IsFileLocalService
{
get
{
return true;
}
}
///
/// Gets or sets any additional settings required by the service.
///
public Dictionary Settings { get; set; }
///
/// Gets or sets the white list of .
///
public Uri[] WhiteList { get; set; }
///
/// Gets the image using the given identifier.
///
///
/// The value identifying the image to fetch.
///
///
/// The array containing the image data.
///
public async Task 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;
}
}
}