Beispiel #1
0
        public ImageEditor(IPostedFile postedFile)
        {
            _Postedfile = postedFile ?? throw new ArgumentNullException($"{nameof(IPostedFile)} cannot be null.");

            _Image = Image.Load(postedFile.OpenReadStream()) as Image <Rgba32>;
            _Image.Mutate(i => i.AutoOrient());
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="args">(IPostedFile pf, string savepath)</param>
        /// <returns></returns>
        public ParameterResolverValue SaveFile(IDataLoaderContext ctx, ParameterResolverValue[] args)
        {
            if (args.Length != 2)
            {
                throw new ArgumentException("Save file takes two arguments: file, save_path");
            }
            IPostedFile file     = args[0].Value as IPostedFile;
            string      savepath = args[1].Value as string;

            if (savepath == null || file == null)
            {
                throw new ArgumentException("SaveFile: either file, path or both are null");
            }
            if (!Path.IsPathFullyQualified(savepath))
            {
                throw new ArgumentException("SaveFile the specified path is not fully qualified. Did you forget to combine paths?");
            }
            using (var stream = new FileStream(savepath, FileMode.Create))
            {
                file.OpenReadStream().CopyTo(stream);
            }
            var scope = Scope(ctx);

            scope.DeleteOnRollback(savepath);
            return(new ParameterResolverValue(true));
        }
 public void ValidateIconPostFile(Icon icon, IPostedFile file) {
     if (icon.IsNew && string.IsNullOrWhiteSpace(icon.Image))
         throw new PortalException(IconResult.FAIL_NO_IMAGE, "Image extension must have a value.");
     if (icon.IsNew && file == null)
         throw new PortalException(IconResult.FAIL_NO_FILE, "Image file must be uploaded.");
     if (file != null && file.ContentLength > MAX_ICON_MB * 1024 * 1024)
         throw new PortalException(IconResult.FAIL_FILE_TOO_LARGE, string.Format("Image file is too large (limit {0}MB)", MAX_ICON_MB));
 }
Beispiel #4
0
 private void SaveFile(IPostedFile file, Icon newIcon)
 {
     if (file != null)
     {
         string path = WebsiteState.GetPath(string.Format(
                                                SAVE_PATH_TEMPLATE, newIcon.Id, newIcon.Image));
         file.SaveAs(path);
     }
 }
Beispiel #5
0
        private Icon BuildIconFromMessage(IconPost iconPost, IPostedFile file)
        {
            Icon icon = new Icon()
            {
                Id   = iconPost.Id,
                Name = iconPost.Name,
                Link = iconPost.Link
            };

            // Force DB name to be correctly formatted
            icon.Name = PortalUtility.UnUrlFormat(PortalUtility.UrlFormat(icon.Name));

            if (file != null)
            {
                icon.Image = PortalUtility.GetImageExtension(file.ContentType);
            }
            return(icon);
        }
Beispiel #6
0
        public Icon Process(IconPost model)
        {
            this.NeedNotNull(model, "uploaded icon");
            IconValidatorService.ValidateIconPost(model);
            IPostedFile file = FileReceiver.GetPostedFiles().FirstOrDefault();
            Icon        icon = BuildIconFromMessage(model, file);

            IconValidatorService.ValidateIconPostFile(icon, file);

            Icon newIcon;

            using (IConnection connection = ConnectionFactory.Create()) {
                CheckAgainstExistingIcons(icon, connection);
                newIcon = UpdateDatabase(icon, connection);
                SaveFile(file, newIcon);
            }
            return(newIcon);
        }