Exemplo n.º 1
0
        public virtual ActionResult <Download> Post()
        {
            var httpPostedFile = Request.Form.Files.FirstOrDefault();

            if (httpPostedFile == null)
            {
                return(NotFound());
            }

            var fileBinary          = _downloadService.GetDownloadBits(httpPostedFile);
            var qqFileNameParameter = "qqfilename";
            var fileName            = httpPostedFile.FileName;

            if (string.IsNullOrEmpty(fileName) && Request.Form.ContainsKey(qqFileNameParameter))
            {
                fileName = Request.Form[qqFileNameParameter].ToString();
            }
            //remove path (passed in IE)
            fileName = _fileProvider.GetFileName(fileName);

            var contentType = httpPostedFile.ContentType;

            var fileExtension = _fileProvider.GetFileExtension(fileName);

            if (!string.IsNullOrEmpty(fileExtension))
            {
                fileExtension = fileExtension.ToLowerInvariant();
            }

            var download = new Download
            {
                DownloadGuid   = Guid.NewGuid(),
                UseDownloadUrl = false,
                DownloadUrl    = string.Empty,
                DownloadBinary = fileBinary,
                ContentType    = contentType,
                //we store filename without extension for downloads
                Filename  = _fileProvider.GetFileNameWithoutExtension(fileName),
                Extension = fileExtension,
                IsNew     = true
            };

            try
            {
                _downloadService.InsertDownload(download);

                //when returning JSON the mime-type must be set to text/plain
                //otherwise some browsers will pop-up a "Save As" dialog.
                return(download);
            }
            catch (Exception exc)
            {
                _logger.Error(exc.Message, exc);

                return(BadRequest());
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Get picture SEO friendly name
        /// </summary>
        /// <param name="name">Name</param>
        /// <returns>Result</returns>


        /// <summary>
        /// Gets the default picture URL
        /// </summary>
        /// <param name="targetSize">The target picture size (longest side)</param>
        /// <param name="defaultPictureType">Default picture type</param>
        /// <param name="storeLocation">Store location URL; null to use determine the current store location automatically</param>
        /// <returns>Picture URL</returns>
        public virtual string GetDefaultPictureUrl(int targetSize = 0,
                                                   PictureType defaultPictureType = PictureType.Entity,
                                                   string storeLocation           = null)
        {
            var defaultImageFileName = defaultPictureType switch
            {
                PictureType.Avatar => _settingService.GetSettingByKey("Media.Customer.DefaultAvatarImageName", MotelMediaDefaults.DefaultAvatarFileName),
                _ => _settingService.GetSettingByKey("Media.DefaultImageName", MotelMediaDefaults.DefaultImageFileName),
            };
            var filePath = GetPictureLocalPath(defaultImageFileName);

            if (!_fileProvider.FileExists(filePath))
            {
                return(string.Empty);
            }

            if (targetSize == 0)
            {
                var url = GetImagesPathUrl(storeLocation) + defaultImageFileName;

                return(url);
            }
            else
            {
                var fileExtension = _fileProvider.GetFileExtension(filePath);
                var thumbFileName = $"{_fileProvider.GetFileNameWithoutExtension(filePath)}_{targetSize}{fileExtension}";
                var thumbFilePath = GetThumbLocalPath(thumbFileName);
                if (!GeneratedThumbExists(thumbFilePath, thumbFileName))
                {
                    using var image = Image.Load <Rgba32>(filePath, out var imageFormat);
                    image.Mutate(imageProcess => imageProcess.Resize(new ResizeOptions
                    {
                        Mode = ResizeMode.Max,
                        Size = CalculateDimensions(image.Size(), targetSize)
                    }));
                    var pictureBinary = EncodeImage(image, imageFormat);
                    SaveThumb(thumbFilePath, thumbFileName, imageFormat.DefaultMimeType, pictureBinary);
                }

                var url = GetThumbUrl(thumbFileName, storeLocation);
                return(url);
            }
        }