Beispiel #1
0
        /// <summary>
        /// Uploads a file to predefined directory.
        /// </summary>
        /// <param name="execContext">Execution context</param>
        /// <param name="settings">Custom defined settings in conficuration file.</param>
        /// <returns>Dictionary of string objects containing the information of the uploaded files.</returns>
        private object UploadFiles(IDataLoaderWriteContext execContext, WriteCustomSettings settings)
        {
            CheckMaxFileSpacePerDashboard(execContext, settings.FileRestrictions);
            CreateDirectoryIfSuchDoesNotExist(settings.UploadFolder);

            List <Dictionary <string, object> > result = new List <Dictionary <string, object> >();

            double height = settings.PreviewHeight ?? 60D;
            double width  = settings.PreviewWidth ?? 120D;
            string fileName;
            string previewName;

            HtmlEncoder encoder = HtmlEncoder.Default;

            foreach (object item in execContext.ProcessingContext.InputModel.Data.Values)
            {
                if (item is IPostedFile postedFile)
                {
                    fileName = RenameUploadedFileIfNameExists(postedFile.FileName, settings.UploadFolder);

                    if (postedFile.ContentType.IndexOf("image", StringComparison.InvariantCultureIgnoreCase) > -1 && _ImageValidator.GetValidMimeTypes.Any(s => postedFile.ContentType.IndexOf(s, StringComparison.InvariantCultureIgnoreCase) > -1 && _ImageValidator.ValidateIfStreamIsImage(postedFile.OpenReadStream(), s)))
                    {
                        using (IImageEditor imageEditor = new ImageEditor(postedFile))
                        {
                            try
                            {
                                imageEditor.Save(Path.Combine(settings.UploadFolder, fileName));

                                previewName = GeneratePreviewName(fileName);

                                imageEditor.Resize(width, height);
                                imageEditor.Save(Path.Combine(settings.UploadFolder, previewName));
                            }
                            catch (Exception ex)
                            {
                                throw ex;
                            }
                        }
                    }
                    else
                    {
                        using (var stream = new FileStream(Path.Combine(settings.UploadFolder, fileName), FileMode.Create))
                        {
                            postedFile.OpenReadStream().CopyTo(stream);
                        }
                    }

                    result.Add(new Dictionary <string, object>()
                    {
                        { NAME, string.IsNullOrEmpty(postedFile.Name) ? postedFile.FileName : encoder.Encode(postedFile.Name) },//user input please escape
                        { FILENAME, fileName },
                        { CONTENTTYPE, postedFile.ContentType },
                        { ORIGINALFILENAME, postedFile.FileName },
                        { LENGTH, postedFile.Length }
                    });
                }
            }

            return(result);
        }
Beispiel #2
0
        /// <summary>
        /// Uploads or deletes new file to predefined directory
        /// </summary>
        /// <param name="execContext">Execution context</param>
        /// <returns>null</returns>
        protected override object Write(IDataLoaderWriteContext execContext)
        {
            //Get the configured directory
            WriteCustomSettings settings = new WriteCustomSettings(execContext, execContext.PluginServiceManager.GetService <KraftGlobalConfigurationSettings>(typeof(KraftGlobalConfigurationSettings)));

            if (execContext.Operation == OPERATION_DELETE)
            {
                string fileName = execContext.Evaluate("name").Value.ToString();

                if (string.IsNullOrWhiteSpace(fileName))
                {
                    throw new Exception("File name cannot be null, empty or white spaced.");
                }

                return(DeleteFile(settings.UploadFolder, fileName));
            }
            else if (execContext.Operation == OPERATION_INSERT)
            {
                return(UploadFiles(execContext, settings));
            }

            return(null);
        }