private void HandleImageInfo(ApiRequest request)
{
try
{
var fileName = request.RequiredFileNameOrPath("image");
Guard.AgainstNull(_bookSelection.CurrentSelection, "CurrentBook");
var plainfilename = fileName.NotEncoded;
// The fileName might be URL encoded. See https://silbloom.myjetbrains.com/youtrack/issue/BL-3901.
var path = UrlPathString.GetFullyDecodedPath(_bookSelection.CurrentSelection.FolderPath, ref plainfilename);
RequireThat.File(path).Exists();
var fileInfo = new FileInfo(path);
dynamic result = new ExpandoObject();
result.name = plainfilename;
result.bytes = fileInfo.Length;
// Using a stream this way, according to one source,
// http://stackoverflow.com/questions/552467/how-do-i-reliably-get-an-image-dimensions-in-net-without-loading-the-image,
// supposedly avoids loading the image into memory when we only want its dimensions
using(var stream = RobustFile.OpenRead(path))
using(var img = Image.FromStream(stream, false, false))
{
result.width = img.Width;
result.height = img.Height;
switch(img.PixelFormat)
{
case PixelFormat.Format32bppArgb:
case PixelFormat.Format32bppRgb:
case PixelFormat.Format32bppPArgb:
result.bitDepth = "32";
break;
case PixelFormat.Format24bppRgb:
result.bitDepth = "24";
break;
case PixelFormat.Format16bppArgb1555:
case PixelFormat.Format16bppGrayScale:
result.bitDepth = "16";
break;
case PixelFormat.Format8bppIndexed:
result.bitDepth = "8";
break;
case PixelFormat.Format1bppIndexed:
result.bitDepth = "1";
break;
default:
result.bitDepth = "unknown";
break;
}
}
request.ReplyWithJson((object) result);
}
catch(Exception e)
{
Logger.WriteEvent("Error in server imageInfo/: url was " + request.LocalPath());
Logger.WriteEvent("Error in server imageInfo/: exception is " + e.Message);
request.Failed(e.Message);
NonFatalProblem.Report(ModalIf.None, PassiveIf.Alpha, "Request Error", request.LocalPath(), e);
}
}