예제 #1
0
        //do not validate request token (XSRF)
        // [AdminAntiForgery(true)]
        public virtual IActionResult AsyncUpload()
        {
            var httpPostedFile = Request.Form.Files.FirstOrDefault();

            if (httpPostedFile == null)
            {
                return(Json(new
                {
                    success = false,
                    message = "No file uploaded",
                    downloadGuid = Guid.Empty
                }));
            }

            var fileBinary = httpPostedFile.GetDownloadBits();

            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
            };

            _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(Json(new { success = true,
                              downloadId = download.Id,
                              downloadUrl = Url.Action("DownloadFile", new { downloadGuid = download.DownloadGuid }) }));
        }
예제 #2
0
        /// <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)
        {
            string defaultImageFileName;

            switch (defaultPictureType)
            {
            case PictureType.Avatar:
                defaultImageFileName = _settingService.GetSettingByKey("Media.User.DefaultAvatarImageName", ResearchMediaDefaults.DefaultAvatarFileName);
                break;

            case PictureType.Pdf:
                defaultImageFileName = ResearchMediaDefaults.DefaultPdfFileName;
                break;

            case PictureType.Entity:
            default:
                defaultImageFileName = _settingService.GetSettingByKey("Media.DefaultImageName", ResearchMediaDefaults.DefaultImageFileName);
                break;
            }
            var filePath = GetPictureLocalPath(defaultImageFileName);

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


            if (targetSize == 0)
            {
                //var url = (!string.IsNullOrEmpty(storeLocation)
                //                 ? storeLocation
                //                 : _webHelper.GetStoreLocation())
                //                 +
                //                 "images/" + defaultImageFileName;
                var url = "images/" + 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(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);
            }
        }
예제 #3
0
        //do not validate request token (XSRF)
        // [AdminAntiForgery(true)]
        public virtual IActionResult AsyncUpload()
        {
            //if (!_permissionService.Authorize(StandardPermissionProvider.UploadPictures))
            //    return Json(new { success = false, error = "You do not have required permissions" }, "text/plain");

            var httpPostedFile = Request.Form.Files.FirstOrDefault();

            if (httpPostedFile == null)
            {
                return(Json(new
                {
                    success = false,
                    message = "No file uploaded",
                    downloadGuid = Guid.Empty
                }));
            }

            var fileBinary = httpPostedFile.GetDownloadBits();

            const string 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();
            }

            //contentType is not always available
            //that's why we manually update it here
            //http://www.sfsu.edu/training/mimetype.htm
            if (string.IsNullOrEmpty(contentType))
            {
                switch (fileExtension)
                {
                case ".bmp":
                    contentType = MimeTypes.ImageBmp;
                    break;

                case ".gif":
                    contentType = MimeTypes.ImageGif;
                    break;

                case ".jpeg":
                case ".jpg":
                case ".jpe":
                case ".jfif":
                case ".pjpeg":
                case ".pjp":
                    contentType = MimeTypes.ImageJpeg;
                    break;

                case ".png":
                    contentType = MimeTypes.ImagePng;
                    break;

                case ".tiff":
                case ".tif":
                    contentType = MimeTypes.ImageTiff;
                    break;

                default:
                    break;
                }
            }

            var picture = _pictureService.InsertPicture(fileBinary, contentType, null);

            //when returning JSON the mime-type must be set to text/plain
            //otherwise some browsers will pop-up a "Save As" dialog.
            return(Json(new { success = true, pictureId = picture.Id, imageUrl = _pictureService.GetPictureUrl(picture, 100) }));
        }