diff --git a/api/media/formidable.mjs b/api/media/formidable.mjs new file mode 100644 index 0000000..856dc47 --- /dev/null +++ b/api/media/formidable.mjs @@ -0,0 +1,36 @@ +import fs from 'fs' +import { HttpError } from '../error.mjs' +import formidable from 'formidable' +import config from '../config.mjs' + +export function uploadFile(ctx, siteName) { + return new Promise((res, rej) => { + const date = new Date() + + // Generate 'YYYYMMDD_HHMMSS_' prefix + const prefix = date + .toISOString() + .replace(/-/g, '') + .replace('T', '_') + .replace(/:/g, '') + .replace(/\..+/, '_') + + var form = new formidable.IncomingForm() + form.uploadDir = `./public/${siteName}` + form.maxFileSize = config.get('fileSize') + + form.parse(ctx.req, function(err, fields, files) { + if (err) return rej(err) + if (!files || !files.file) return rej(new HttpError('File in body was missing', 422)) + let file = files.file + + fs.rename(files.file.path, `./public/${siteName}/${prefix}${file.name}`, function(err) { + if (err) return rej(err) + file.path = `./public/${siteName}/${prefix}${file.name}` + file.filename = `${prefix}${file.name}` + + return res(file) + }) + }) + }) +} diff --git a/api/media/multer.mjs b/api/media/multer.mjs deleted file mode 100644 index 1281d19..0000000 --- a/api/media/multer.mjs +++ /dev/null @@ -1,37 +0,0 @@ -import multer from 'multer' - -/*const storage = multer.diskStorage({ - destination: function (req, file, cb) { - cb(null, '/tmp/my-uploads') - }, - filename: function (req, file, cb) { - console.log(file) - cb(null, file.fieldname + '-' + Date.now()) - } -})*/ - -export function uploadFile(ctx, siteName) { - return new Promise((res, rej) => { - const date = new Date() - - // Generate 'YYYYMMDD_HHMMSS_' prefix - const prefix = date - .toISOString() - .replace(/-/g, '') - .replace('T', '_') - .replace(/:/g, '') - .replace(/\..+/, '_') - - const storage = multer.diskStorage({ - destination: `./public/${siteName}`, - filename: (req, file, cb) => - cb(null, `${prefix}${file.originalname}`), - }) - - multer({ storage: storage }) - .single('file')(ctx.req, ctx.res, (err, data) => { - if (err) return rej(err) - return res(ctx.req.file) - }) - }) -} diff --git a/api/media/routes.mjs b/api/media/routes.mjs index 70ccb24..26d387b 100644 --- a/api/media/routes.mjs +++ b/api/media/routes.mjs @@ -1,18 +1,18 @@ import * as security from './security.mjs' -import * as multer from './multer.mjs' +import * as formidable from './formidable.mjs' export default class MediaRoutes { constructor(opts = {}) { Object.assign(this, { security: opts.security || security, - multer: opts.multer || multer, + formidable: opts.formidable || formidable, }) } async upload(ctx) { let site = await this.security.verifyToken(ctx) - let result = await this.multer.uploadFile(ctx, site) + let result = await this.formidable.uploadFile(ctx, site) ctx.body = { filename: result.filename, diff --git a/package.json b/package.json index 5307c95..7afb128 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "dependencies": { "bunyan-lite": "^1.1.1", "flaska": "^0.9.5", - "multer": "^1.3.0", + "formidable": "^1.2.2", "nconf-lite": "^2.0.0" }, "devDependencies": { diff --git a/test/media/api.test.mjs b/test/media/api.test.mjs index 00d6a2a..0d6f901 100644 --- a/test/media/api.test.mjs +++ b/test/media/api.test.mjs @@ -1,5 +1,5 @@ import { Eltro as t, assert} from 'eltro' -import fs from 'fs' +import fs from 'fs/promises' import { fileURLToPath } from 'url' import path from 'path' @@ -19,11 +19,10 @@ t.describe('Media (API)', () => { const secret = 'asdf1234' let testFile - t.after(function(done) { + t.after(function() { if (testFile) { - return fs.unlink(resolve(`../../public/${testFile}`), done) + return fs.unlink(resolve(`../../public/${testFile}`)) } - done() }) t.timeout(10000).describe('POST /media', function temp() { @@ -89,6 +88,12 @@ t.describe('Media (API)', () => { assert.ok(data.path) testFile = data.path + + let stats = await Promise.all([ + fs.stat(resolve('test.png')), + fs.stat(resolve(`../../public/${testFile}`)), + ]) + assert.strictEqual(stats[0].size, stats[1].size) }) }) }) diff --git a/test/media/routes.test.mjs b/test/media/routes.test.mjs index f0c5349..39d39ec 100644 --- a/test/media/routes.test.mjs +++ b/test/media/routes.test.mjs @@ -9,7 +9,7 @@ t.describe('#upload', () => { const routes = new MediaRoutes({ security: { verifyToken: stubVerify }, - multer: { uploadFile: stubUpload }, + formidable: { uploadFile: stubUpload }, }) function reset() {