Beispiel #1
0
            // Upload file
            public async Task <IActionResult> Upload()
            {
                if (Files.Count > 0)                   // DN
                {
                    var lang = new Lang();
                    var form = new HttpForm();
                    var name = form.GetValue("id");
                    UploadTable = form.GetValue("table");
                    var folder  = UploadTempPath(name, UploadTable);
                    var token   = form.GetValue(Config.TokenName);
                    var exts    = form.GetValue("exts");
                    var extList = exts.Split(',');
                    if (!Empty(Config.UploadAllowedFileExtensions))
                    {
                        var allowedExtList = new List <string>(Config.UploadAllowedFileExtensions.Split(','));
                        exts = String.Join(",", extList.Where(ext => allowedExtList.Contains(ext, StringComparer.OrdinalIgnoreCase)));                         // Make sure exts is a subset of Config.UploadAllowedFileExtensions
                        if (Empty(exts))
                        {
                            exts = Config.UploadAllowedFileExtensions;
                        }
                    }
                    if (Empty(exts))
                    {
                        exts = @"\w+";
                    }
                    var filetypes    = @"\.(" + exts.Replace(",", "|") + ")$";
                    var maxsize      = form.GetInt("maxsize");
                    var maxfilecount = form.GetInt("maxfilecount");
                    var filename     = form.GetUploadFileName(name);

                    // Skip if no file uploaded
                    if (Empty(filename))
                    {
                        return(Controller.BadRequest("Missing file name"));
                    }
                    if (Config.UploadConvertAccentedChars)
                    {
                        filename = HtmlEncode(filename);
                        filename = Regex.Replace(filename, @"&([a-zA-Z])(uml|acute|grave|circ|tilde|cedil);", "$1");
                        filename = HtmlDecode(filename);
                    }
                    var filetype = form.GetUploadFileContentType(name);
                    var filesize = form.GetUploadFileSize(name);
                    var value    = await form.GetUploadFileData(name);

                    // Check file types
                    if (!Regex.IsMatch(filename, filetypes, RegexOptions.IgnoreCase))
                    {
                        var fileerror = lang.Phrase("UploadErrMsgAcceptFileTypes");
                        return(OutputJson("files", new List <object[]> {
                            new object[] { name, filename, filetype, filesize, token, fileerror }
                        }));
                    }

                    // Check file size
                    if (maxsize < filesize)
                    {
                        var fileerror = lang.Phrase("UploadErrMsgMaxFileSize");
                        return(OutputJson("files", new List <object[]> {
                            new object[] { name, filename, filetype, filesize, token, fileerror }
                        }));
                    }

                    // Check max file count
                    var filecount = FolderFileCount(folder);
                    if (maxfilecount > 0 && maxfilecount <= filecount)
                    {
                        var fileerror = lang.Phrase("UploadErrMsgMaxNumberOfFiles");
                        return(OutputJson("files", new List <object[]> {
                            new object[] { name, filename, filetype, filesize, token, fileerror }
                        }));
                    }

                    // Delete all files in directory if replace
                    var version = Config.UploadThumbnailFolder;
                    if (form.GetBool("replace"))
                    {
                        CleanPath(folder, false);
                    }
                    await SaveFile(folder, filename, value);

                    folder = PathCombine(folder, version, true);
                    var w = Config.UploadThumbnailWidth;
                    var h = Config.UploadThumbnailHeight;
                    ResizeBinary(ref value, ref w, ref h);
                    await SaveFile(folder, filename, value);

                    return(OutputJson("files", new List <object[]> {
                        new object[] { name, filename, filetype, filesize, token }
                    }));
                }
                return(new EmptyResult());
            }