Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload dir #853

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions examples/with-http.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,23 @@ const server = http.createServer((req, res) => {
if (req.url === '/api/upload' && req.method.toLowerCase() === 'post') {
// parse a file upload
const form = formidable({
// uploadDir: `uploads`,
defaultInvalidName: 'invalid',
uploadDir: `uploads`,
keepExtensions: true,
// filename(/*name, ext, part, form*/) {
// /* name basename of the http originalFilename
// ext with the dot ".txt" only if keepExtensions is true
// */
// // slugify to avoid invalid filenames
// // substr to define a maximum length
// // return `${slugify(name)}.${slugify(ext, {separator: ''})}`.substr(0, 100);
// return 'yo.txt'; // or completly different name
// // return 'z/yo.txt'; // subdirectory
// },
// filter: function ({name, originalFilename, mimetype}) {
// // keep only images
// return mimetype && mimetype.includes("image");
// }
createDirsFromUploads: true,
allowEmptyFiles: true,
minFileSize: 0,
filename(name, ext, part, form) {
// todo use only valid chars and check length
// originalFilename will have slashes with relative path if a
// directory was uploaded
return part.originalFilename;
},
filter: function ({name, originalFilename, mimetype}) {
return Boolean(originalFilename);
// keep only images
// return mimetype?.includes("image");
}
// maxTotalFileSize: 4000,
// maxFileSize: 1000,

Expand All @@ -53,7 +54,8 @@ const server = http.createServer((req, res) => {
<h2>With Node.js <code>"http"</code> module</h2>
<form action="/api/upload" enctype="multipart/form-data" method="post">
<div>Text field title: <input type="text" name="title" /></div>
<div>File: <input type="file" name="multipleFiles" multiple="multiple" /></div>
<div>File: <input type="file" name="multipleFiles" multiple /></div>
<div>Folders: <input type="file" name="folders" webkitdirectory directory multiple /></div>
<input type="submit" value="Upload" />
</form>

Expand Down
47 changes: 31 additions & 16 deletions src/Formidable.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import os from 'node:os';
import path from 'node:path';
import fs from 'node:fs';
import { EventEmitter } from 'node:events';
import { StringDecoder } from 'node:string_decoder';
import hexoid from 'hexoid';
Expand All @@ -25,6 +26,7 @@ const DEFAULT_OPTIONS = {
maxTotalFileSize: undefined,
minFileSize: 1,
allowEmptyFiles: false,
createDirsFromUploads: false,
keepExtensions: false,
encoding: 'utf-8',
hashAlgorithm: false,
Expand All @@ -42,6 +44,15 @@ function hasOwnProp(obj, key) {
return Object.prototype.hasOwnProperty.call(obj, key);
}


GrosSacASac marked this conversation as resolved.
Show resolved Hide resolved
const createNecessaryDirectoriesSync = function (filePath) {
const directoryname = path.dirname(filePath);
if (fs.existsSync(directoryname)) {
return;
}
fs.mkdirSync(directoryname, { recursive: true });
};

class IncomingForm extends EventEmitter {
constructor(options = {}) {
super();
Expand Down Expand Up @@ -462,22 +473,26 @@ class IncomingForm extends EventEmitter {
}

_newFile({ filepath, originalFilename, mimetype, newFilename }) {
return this.options.fileWriteStreamHandler
? new VolatileFile({
newFilename,
filepath,
originalFilename,
mimetype,
createFileWriteStream: this.options.fileWriteStreamHandler,
hashAlgorithm: this.options.hashAlgorithm,
})
: new PersistentFile({
newFilename,
filepath,
originalFilename,
mimetype,
hashAlgorithm: this.options.hashAlgorithm,
});
if (this.options.fileWriteStreamHandler) {
return new VolatileFile({
newFilename,
filepath,
originalFilename,
mimetype,
createFileWriteStream: this.options.fileWriteStreamHandler,
hashAlgorithm: this.options.hashAlgorithm,
});
}
if (this.options.createDirsFromUploads) {
createNecessaryDirectoriesSync(filepath);
}
return new PersistentFile({
newFilename,
filepath,
originalFilename,
mimetype,
hashAlgorithm: this.options.hashAlgorithm,
});
}

_getFileName(headerValue) {
Expand Down