//[OutputCache(Duration = 60 * 60 * 24 * 30, Location = OutputCacheLocation.Downstream)] public async Task <ActionResult> Image(int assetId, string fileName, string extension, int?cropSizeId) { var settings = ImageResizeSettings.ParseFromQueryString(Request.Query); var getImageQuery = new GetImageAssetRenderDetailsByIdQuery(assetId); var asset = await _queryExecutor.ExecuteAsync(getImageQuery); if (asset == null) { return(FileAssetNotFound("Image could not be found")); } if (SlugFormatter.ToSlug(asset.FileName) != fileName) { var url = _imageAssetRouteLibrary.ImageAsset(asset, settings); return(RedirectPermanent(url)); } var lastModified = DateTime.SpecifyKind(asset.UpdateDate, DateTimeKind.Utc); // Round the ticks down (see http://stackoverflow.com/a/1005222/486434), because http headers are only accurate to seconds, so get rounded down lastModified = lastModified.AddTicks(-(lastModified.Ticks % TimeSpan.TicksPerSecond)); if (!string.IsNullOrEmpty(Request.Headers["If-Modified-Since"])) { DateTime ifModifiedSince; if (DateTime.TryParse(Request.Headers["If-Modified-Since"], out ifModifiedSince) && lastModified <= ifModifiedSince.ToUniversalTime()) { return(StatusCode(304)); } } Stream stream = null; try { stream = await _resizedImageAssetFileService.GetAsync(asset, settings); } catch (FileNotFoundException ex) { // If the file exists but the file has gone missing, log and return a 404 _logger.LogError(0, ex, "Image Asset exists, but has no file: {0}", assetId); return(FileAssetNotFound("File not found")); } // Expire the image, so browsers always check with the server, but also send a last modified date so we can check for If-Modified-Since on the next request and return a 304 Not Modified. var headers = Response.GetTypedHeaders(); headers.Expires = DateTime.UtcNow.AddMonths(-1); headers.LastModified = lastModified; var contentType = _mimeTypeService.MapFromFileName("." + asset.Extension); return(new FileStreamResult(stream, contentType)); }
//[OutputCache(Duration = 60 * 60 * 24 * 30, Location = OutputCacheLocation.Downstream)] public ActionResult Image(int assetId, string fileName, string extension, int?cropSizeId) { var qs = Request.Url.Query; var settings = ImageResizeSettings.ParseFromQueryString(qs); var asset = _queryExecutor.GetById <ImageAssetRenderDetails>(assetId); if (asset == null) { return(FileAssetNotFound("Image could not be found")); } if (SlugFormatter.ToSlug(asset.FileName) != fileName) { var url = _imageAssetRouteLibrary.ImageAsset(asset, settings); return(RedirectPermanent(url)); } DateTime lastModified = DateTime.SpecifyKind(asset.UpdateDate, DateTimeKind.Utc); // Round the ticks down (see http://stackoverflow.com/a/1005222/486434), because http headers are only accurate to seconds, so get rounded down lastModified = lastModified.AddTicks(-(lastModified.Ticks % TimeSpan.TicksPerSecond)); if (!string.IsNullOrEmpty(Request.Headers["If-Modified-Since"])) { DateTime ifModifiedSince; if (DateTime.TryParse(Request.Headers["If-Modified-Since"], out ifModifiedSince) && lastModified <= ifModifiedSince.ToUniversalTime()) { return(new HttpStatusCodeResult(304, "Not Modified")); } } Stream stream = null; try { stream = _resizedImageAssetFileService.Get(asset, settings); } catch (FileNotFoundException ex) { // If the file exists but the file has gone missing, log and return a 404 _errorLoggingService.Log(ex); return(FileAssetNotFound("File not found")); } // Expire the image, so browsers always check with the server, but also send a last modified date so we can check for If-Modified-Since on the next request and return a 304 Not Modified. Response.Cache.SetExpires(DateTime.UtcNow.AddMonths(-1)); Response.Cache.SetLastModified(lastModified); var contentType = MimeMapping.GetMimeMapping(asset.Extension); return(new FileStreamResult(stream, contentType)); }
public async Task <ActionResult> Image_OldPath(int assetId, string fileName, string extension, int?cropSizeId) { var settings = ImageResizeSettings.ParseFromQueryString(Request.Query); var getImageQuery = new GetImageAssetRenderDetailsByIdQuery(assetId); var asset = await _queryExecutor.ExecuteAsync(getImageQuery); if (asset == null) { return(FileAssetNotFound("Image could not be found")); } var url = _imageAssetRouteLibrary.ImageAsset(asset, settings); return(RedirectPermanent(url)); }
public async Task <ActionResult> Image(int imageAssetId, long fileStamp, string verificationToken, string fileName, string extension) { var settings = ImageResizeSettings.ParseFromQueryString(Request.Query); var getImageQuery = new GetImageAssetRenderDetailsByIdQuery(imageAssetId); var asset = await _queryExecutor.ExecuteAsync(getImageQuery); // additionally check that filestamp is not after the curent update date if (asset == null || !IsFileStampValid(fileStamp, asset.FileUpdateDate) || asset.VerificationToken != verificationToken) { return(FileAssetNotFound("Image could not be found")); } // if the title or filestamp is different, redirect to the correct url var sluggedFileName = SlugFormatter.ToSlug(asset.FileName); if (sluggedFileName != fileName || fileStamp.ToString() != asset.FileStamp) { var url = _imageAssetRouteLibrary.ImageAsset(asset, settings); return(RedirectPermanent(url)); } Stream stream = null; try { stream = await _resizedImageAssetFileService.GetAsync(asset, settings); } catch (FileNotFoundException ex) { // If the file exists but the file has gone missing, log and return a 404 _logger.LogError(0, ex, "Image Asset exists, but has no file: {0}", imageAssetId); return(FileAssetNotFound("File not found")); } var contentType = _mimeTypeService.MapFromFileName("." + asset.FileExtension); SetCacheHeader(_imageAssetsSettings.CacheMaxAge); return(new FileStreamResult(stream, contentType)); }