Replace multer with formidable
This commit is contained in:
parent
0961d4b2dd
commit
bf508fc5b8
6 changed files with 50 additions and 46 deletions
36
api/media/formidable.mjs
Normal file
36
api/media/formidable.mjs
Normal file
|
@ -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)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
|
@ -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)
|
||||
})
|
||||
})
|
||||
}
|
|
@ -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,
|
||||
|
|
|
@ -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": {
|
||||
|
|
|
@ -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)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
|
@ -9,7 +9,7 @@ t.describe('#upload', () => {
|
|||
|
||||
const routes = new MediaRoutes({
|
||||
security: { verifyToken: stubVerify },
|
||||
multer: { uploadFile: stubUpload },
|
||||
formidable: { uploadFile: stubUpload },
|
||||
})
|
||||
|
||||
function reset() {
|
||||
|
|
Loading…
Reference in a new issue