示例#1
0
        /// <summary>
        /// Check whether there are any restrictions on handling the file
        /// </summary>
        /// <param name="path">Path to the file</param>
        /// <returns>True if the file can be handled; otherwise false</returns>
        protected virtual bool CanHandleFile(string path)
        {
            bool result = false;

            string fileExtension = _fileProvider.GetFileExtension(path).Replace(".", string.Empty).ToLower();

            string forbiddenUploads = GetSetting("FORBIDDEN_UPLOADS").Trim().ToLower();

            if (!string.IsNullOrEmpty(forbiddenUploads))
            {
                ArrayList forbiddenFileExtensions = new ArrayList(Regex.Split(forbiddenUploads, "\\s+"));
                result = !forbiddenFileExtensions.Contains(fileExtension);
            }

            string allowedUploads = GetSetting("ALLOWED_UPLOADS").Trim().ToLower();

            if (string.IsNullOrEmpty(allowedUploads))
            {
                return(result);
            }

            ArrayList allowedFileExtensions = new ArrayList(Regex.Split(allowedUploads, "\\s+"));

            result = allowedFileExtensions.Contains(fileExtension);

            return(result);
        }
示例#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.Customer.DefaultAvatarImageName", AgsMediaDefaults.DefaultAvatarFileName);
                break;

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

            string filePath = GetPictureLocalPath(defaultImageFileName);

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

            if (targetSize == 0)
            {
                string url = (!string.IsNullOrEmpty(storeLocation)
                                 ? storeLocation
                                 : _webHelper.GetStoreLocation())
                             + "images/" + defaultImageFileName;
                return(url);
            }
            else
            {
                string fileExtension = _fileProvider.GetFileExtension(filePath);
                string thumbFileName = $"{_fileProvider.GetFileNameWithoutExtension(filePath)}_{targetSize}{fileExtension}";
                string thumbFilePath = GetThumbLocalPath(thumbFileName);
                if (!GeneratedThumbExists(thumbFilePath, thumbFileName))
                {
                    using (Image <Rgba32> image = Image.Load(filePath, out IImageFormat imageFormat))
                    {
                        image.Mutate(imageProcess => imageProcess.Resize(new ResizeOptions
                        {
                            Mode = ResizeMode.Max,
                            Size = CalculateDimensions(image.Size(), targetSize)
                        }));
                        byte[] pictureBinary = EncodeImage(image, imageFormat);
                        SaveThumb(thumbFilePath, thumbFileName, imageFormat.DefaultMimeType, pictureBinary);
                    }
                }

                string url = GetThumbUrl(thumbFileName, storeLocation);
                return(url);
            }
        }
示例#3
0
        public virtual IActionResult Upload()
        {
            ViewData["resultCode"] = "failed";
            ViewData["result"]     = "No access to this functionality";
            return(View());


            if (Request.Form.Files.Count == 0)
            {
                throw new Exception("No file uploaded");
            }

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

            if (uploadFile == null)
            {
                ViewData["resultCode"] = "failed";
                ViewData["result"]     = "No file name provided";
                return(View());
            }

            var fileName = _fileProvider.GetFileName(uploadFile.FileName);

            if (string.IsNullOrEmpty(fileName))
            {
                ViewData["resultCode"] = "failed";
                ViewData["result"]     = "No file name provided";
                return(View());
            }

            var directory = "~/wwwroot/images/uploaded/";
            var filePath  = _fileProvider.Combine(_fileProvider.MapPath(directory), fileName);

            var fileExtension = _fileProvider.GetFileExtension(filePath);

            if (!GetAllowedFileTypes().Contains(fileExtension))
            {
                ViewData["resultCode"] = "failed";
                ViewData["result"]     = $"Files with {fileExtension} extension cannot be uploaded";
                return(View());
            }

            using (var fileStream = new FileStream(filePath, FileMode.Create))
            {
                uploadFile.CopyTo(fileStream);
            }

            ViewData["resultCode"] = "success";
            ViewData["result"]     = "success";
            ViewData["filename"]   = Url.Content($"{directory}{fileName}");
            return(View());
        }
示例#4
0
        //do not validate request token (XSRF)


        public virtual IActionResult AsyncUpload()
        {
            //if (!_permissionService.Authorize(StandardPermissionProvider.UploadPictures))
            //    return Json(new { success = false, error = "You do not have required permissions" }, "text/plain");


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

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

            byte[] fileBinary = _downloadService.GetDownloadBits(httpPostedFile);

            const string qqFileNameParameter = "qqfilename";
            string       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);

            string contentType = httpPostedFile.ContentType;

            string 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.Id, 100) }));
        }