Tidy up image resizing example

This commit is contained in:
SteveSandersonMS
2016-06-07 17:46:42 +01:00
parent e58a4e4015
commit 7af64a8944
2 changed files with 18 additions and 15 deletions

View File

@@ -11,6 +11,7 @@ namespace NodeServicesExamples.Controllers
public class ResizeImageController : Controller public class ResizeImageController : Controller
{ {
private const int MaxDimension = 1000; private const int MaxDimension = 1000;
private static string[] AllowedMimeTypes = new[] { "image/jpeg", "image/png", "image/gif" };
private IHostingEnvironment _environment; private IHostingEnvironment _environment;
private INodeServices _nodeServices; private INodeServices _nodeServices;
@@ -25,11 +26,18 @@ namespace NodeServicesExamples.Controllers
public async Task<IActionResult> Index(string imagePath, int maxWidth, int maxHeight) public async Task<IActionResult> Index(string imagePath, int maxWidth, int maxHeight)
{ {
// Validate incoming params // Validate incoming params
if (maxWidth > MaxDimension || maxHeight > MaxDimension || (maxHeight <= 0 && maxWidth <= 0)) if (maxWidth < 0 || maxHeight < 0 || maxWidth > MaxDimension || maxHeight > MaxDimension
|| (maxWidth + maxHeight) == 0)
{ {
return BadRequest("Invalid dimensions"); return BadRequest("Invalid dimensions");
} }
var mimeType = GetContentType(imagePath);
if (Array.IndexOf(AllowedMimeTypes, mimeType) < 0)
{
return BadRequest("Disallowed image format");
}
// Locate source image on disk // Locate source image on disk
var fileInfo = _environment.WebRootFileProvider.GetFileInfo(imagePath); var fileInfo = _environment.WebRootFileProvider.GetFileInfo(imagePath);
if (!fileInfo.Exists) if (!fileInfo.Exists)
@@ -38,25 +46,19 @@ namespace NodeServicesExamples.Controllers
} }
// Invoke Node and pipe the result to the response // Invoke Node and pipe the result to the response
var mimeType = GetContentType(imagePath); var imageStream = await _nodeServices.Invoke<Stream>(
var imageStream = await _nodeServices.Invoke<Stream>("./Node/resizeImage", fileInfo.PhysicalPath, mimeType, maxWidth, maxHeight); "./Node/resizeImage",
fileInfo.PhysicalPath,
mimeType,
maxWidth,
maxHeight);
return File(imageStream, mimeType); return File(imageStream, mimeType);
} }
private string GetContentType(string path) private string GetContentType(string path)
{ {
string result; string result;
if (!new FileExtensionContentTypeProvider().TryGetContentType(path, out result)) return new FileExtensionContentTypeProvider().TryGetContentType(path, out result) ? result : null;
{
result = "application/octet-stream";
}
return result;
}
private class ResizeImageResult
{
public string Base64 { get; set; }
} }
} }
} }

View File

@@ -1,7 +1,8 @@
var sharp = require('sharp'); var sharp = require('sharp');
module.exports = function(result, physicalPath, mimeType, maxWidth, maxHeight) { module.exports = function(result, physicalPath, mimeType, maxWidth, maxHeight) {
// Invoke the 'sharp' NPM module, and have it pipe the resulting image data back to .NET
sharp(physicalPath) sharp(physicalPath)
.resize(maxWidth > 0 ? maxWidth : null, maxHeight > 0 ? maxHeight : null) .resize(maxWidth || null, maxHeight || null)
.pipe(result.stream); .pipe(result.stream);
} }