コード例 #1
0
        /// <summary>
        /// Gets a resized image for the image at the given path
        /// </summary>
        /// <param name="imagePath"></param>
        /// <param name="width"></param>
        /// <returns></returns>
        /// <remarks>
        /// If there is no media, image property or image file is found then this will return not found.
        /// </remarks>
        public IActionResult GetResized(string imagePath, int width)
        {
            var ext = Path.GetExtension(imagePath);

            // we need to check if it is an image by extension
            if (_imageUrlGenerator.IsSupportedImageFormat(ext) == false)
            {
                return(NotFound());
            }

            //redirect to ImageProcessor thumbnail with rnd generated from last modified time of original media file
            DateTimeOffset?imageLastModified = null;

            try
            {
                imageLastModified = _mediaFileManager.FileSystem.GetLastModified(imagePath);
            }
            catch (Exception)
            {
                // if we get an exception here it's probably because the image path being requested is an image that doesn't exist
                // in the local media file system. This can happen if someone is storing an absolute path to an image online, which
                // is perfectly legal but in that case the media file system isn't going to resolve it.
                // so ignore and we won't set a last modified date.
            }

            var rnd      = imageLastModified.HasValue ? $"&rnd={imageLastModified:yyyyMMddHHmmss}" : null;
            var imageUrl = _imageUrlGenerator.GetImageUrl(new ImageUrlGenerationOptions(imagePath)
            {
                Width            = width,
                ImageCropMode    = ImageCropMode.Max,
                CacheBusterValue = rnd
            });

            return(new RedirectResult(imageUrl, false));
        }
コード例 #2
0
        private void SetProperties(IContentBase content, ImagingAutoFillUploadField autoFillConfig, string filepath, Stream?filestream, string?culture, string?segment)
        {
            var extension = (Path.GetExtension(filepath) ?? string.Empty).TrimStart(Constants.CharArrays.Period);

            var size = _imageUrlGenerator.IsSupportedImageFormat(extension)
                ? _imageDimensionExtractor.GetDimensions(filestream) ?? (Size?)new Size(Constants.Conventions.Media.DefaultSize, Constants.Conventions.Media.DefaultSize)
                : null;

            SetProperties(content, autoFillConfig, size, filestream?.Length, extension, culture, segment);
        }
コード例 #3
0
        /// <summary>
        /// Gets a resized image for the image at the given path
        /// </summary>
        /// <param name="imagePath"></param>
        /// <param name="width"></param>
        /// <returns></returns>
        /// <remarks>
        /// If there is no media, image property or image file is found then this will return not found.
        /// </remarks>
        public IActionResult GetResized(string imagePath, int width)
        {
            // We have to use HttpUtility to encode the path here, for non-ASCII characters
            // We cannot use the WebUtility, as we only want to encode the path, and not the entire string
            var encodedImagePath = HttpUtility.UrlPathEncode(imagePath);


            var ext = Path.GetExtension(encodedImagePath);

            // check if imagePath is local to prevent open redirect
            if (!Uri.IsWellFormedUriString(encodedImagePath, UriKind.Relative))
            {
                return(Unauthorized());
            }

            // we need to check if it is an image by extension
            if (_imageUrlGenerator.IsSupportedImageFormat(ext) == false)
            {
                return(NotFound());
            }

            // redirect to ImageProcessor thumbnail with rnd generated from last modified time of original media file
            DateTimeOffset?imageLastModified = null;

            try
            {
                imageLastModified = _mediaFileManager.FileSystem.GetLastModified(imagePath);
            }
            catch (Exception)
            {
                // if we get an exception here it's probably because the image path being requested is an image that doesn't exist
                // in the local media file system. This can happen if someone is storing an absolute path to an image online, which
                // is perfectly legal but in that case the media file system isn't going to resolve it.
                // so ignore and we won't set a last modified date.
            }

            var rnd      = imageLastModified.HasValue ? $"&rnd={imageLastModified:yyyyMMddHHmmss}" : null;
            var imageUrl = _imageUrlGenerator.GetImageUrl(new ImageUrlGenerationOptions(encodedImagePath)
            {
                Width            = width,
                ImageCropMode    = ImageCropMode.Max,
                CacheBusterValue = rnd
            });

            if (Url.IsLocalUrl(imageUrl))
            {
                return(new LocalRedirectResult(imageUrl, false));
            }
            else
            {
                return(Unauthorized());
            }
        }
コード例 #4
0
    public async Task <IActionResult> UploadImage(List <IFormFile> file)
    {
        // Create an unique folder path to help with concurrent users to avoid filename clash
        var imageTempPath =
            _hostingEnvironment.MapPathWebRoot(Constants.SystemDirectories.TempImageUploads + "/" + Guid.NewGuid());

        // Ensure image temp path exists
        if (Directory.Exists(imageTempPath) == false)
        {
            Directory.CreateDirectory(imageTempPath);
        }

        // Must have a file
        if (file.Count == 0)
        {
            return(NotFound());
        }

        // Should only have one file
        if (file.Count > 1)
        {
            return(new UmbracoProblemResult("Only one file can be uploaded at a time", HttpStatusCode.BadRequest));
        }

        IFormFile formFile = file.First();

        // Really we should only have one file per request to this endpoint
        //  var file = result.FileData[0];
        var fileName     = formFile.FileName.Trim(new[] { '\"' }).TrimEnd();
        var safeFileName = fileName.ToSafeFileName(_shortStringHelper);
        var ext          = safeFileName.Substring(safeFileName.LastIndexOf('.') + 1).ToLowerInvariant();

        if (_contentSettings.IsFileAllowedForUpload(ext) == false ||
            _imageUrlGenerator.IsSupportedImageFormat(ext) == false)
        {
            // Throw some error - to say can't upload this IMG type
            return(new UmbracoProblemResult("This is not an image filetype extension that is approved", HttpStatusCode.BadRequest));
        }

        var newFilePath         = imageTempPath + Path.DirectorySeparatorChar + safeFileName;
        var relativeNewFilePath = _ioHelper.GetRelativePath(newFilePath);

        await using (FileStream stream = System.IO.File.Create(newFilePath))
        {
            await formFile.CopyToAsync(stream);
        }

        return(Ok(new { tmpLocation = relativeNewFilePath }));
    }