2021-10-11 00:56:22 +00:00
|
|
|
import fs from 'fs'
|
2022-08-13 21:52:45 +00:00
|
|
|
import { HttpError } from 'flaska'
|
2021-10-11 00:56:22 +00:00
|
|
|
import formidable from 'formidable'
|
|
|
|
import config from '../config.mjs'
|
|
|
|
|
2022-01-05 14:47:51 +00:00
|
|
|
let lastDateString = ''
|
|
|
|
let incrementor = 1
|
|
|
|
|
2022-08-13 01:03:06 +00:00
|
|
|
function getPrefix() {
|
|
|
|
const date = new Date()
|
|
|
|
let prefix = ''
|
|
|
|
|
|
|
|
// Generate 'YYYYMMDD_HHMMSS_' prefix
|
|
|
|
prefix = date
|
|
|
|
.toISOString()
|
|
|
|
.replace(/-/g, '')
|
|
|
|
.replace('T', '_')
|
|
|
|
.replace(/:/g, '')
|
|
|
|
.replace(/\..+/, '_')
|
|
|
|
|
|
|
|
// Append xx_ if same date is hit multiple times
|
|
|
|
if (prefix === lastDateString) {
|
|
|
|
prefix += incrementor.toString().padStart('2', '0') + '_'
|
|
|
|
incrementor++
|
|
|
|
} else {
|
|
|
|
lastDateString = prefix
|
|
|
|
incrementor = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return prefix
|
|
|
|
}
|
|
|
|
|
2022-01-06 09:01:10 +00:00
|
|
|
export function uploadFile(ctx, siteName, noprefix = false) {
|
2021-10-11 00:56:22 +00:00
|
|
|
return new Promise((res, rej) => {
|
|
|
|
|
2022-01-06 09:01:10 +00:00
|
|
|
let prefix = ''
|
|
|
|
|
2021-10-11 00:56:22 +00:00
|
|
|
var form = new formidable.IncomingForm()
|
2022-08-13 21:52:45 +00:00
|
|
|
form.uploadDir = `${config.get('uploadFolder')}/${siteName}`
|
2021-10-11 00:56:22 +00:00
|
|
|
form.maxFileSize = config.get('fileSize')
|
|
|
|
|
2023-11-29 21:57:14 +00:00
|
|
|
let siteSize = config.get(`sites:${siteName}:fileSize`)
|
|
|
|
|
|
|
|
if (siteSize && typeof(siteSize) === 'number') {
|
|
|
|
form.maxFileSize = siteSize
|
|
|
|
}
|
|
|
|
|
2021-10-11 00:56:22 +00:00
|
|
|
form.parse(ctx.req, function(err, fields, files) {
|
|
|
|
if (err) return rej(err)
|
2022-08-13 21:52:45 +00:00
|
|
|
if (!files || !files.file) return rej(new HttpError(422, 'File in body was missing'))
|
2021-10-11 00:56:22 +00:00
|
|
|
let file = files.file
|
2022-10-17 09:45:28 +00:00
|
|
|
let filename = file.name.replace(/ /g, '_')
|
|
|
|
.replace(/&/g, 'and')
|
|
|
|
.replace(/'/g, '')
|
|
|
|
.replace(/"/g, '')
|
2022-10-17 09:47:03 +00:00
|
|
|
.replace(/\?/g, '')
|
2021-10-11 00:56:22 +00:00
|
|
|
|
2022-01-05 14:47:51 +00:00
|
|
|
Object.keys(fields).forEach(function(key) {
|
|
|
|
try {
|
|
|
|
fields[key] = JSON.parse(fields[key])
|
|
|
|
} catch { }
|
|
|
|
})
|
|
|
|
ctx.req.body = fields
|
|
|
|
|
2022-10-17 09:45:28 +00:00
|
|
|
if (!noprefix || fs.existsSync(`${config.get('uploadFolder')}/${siteName}/${prefix}${filename}`)) {
|
2022-08-13 01:03:06 +00:00
|
|
|
prefix = getPrefix()
|
|
|
|
}
|
|
|
|
|
2022-10-17 09:45:28 +00:00
|
|
|
fs.rename(files.file.path, `${config.get('uploadFolder')}/${siteName}/${prefix}${filename}`, function(err) {
|
2021-10-11 00:56:22 +00:00
|
|
|
if (err) return rej(err)
|
2022-10-17 09:45:28 +00:00
|
|
|
file.path = `${config.get('uploadFolder')}/${siteName}/${prefix}${filename}`
|
|
|
|
file.filename = `${prefix}${filename}`
|
2021-10-11 00:56:22 +00:00
|
|
|
|
|
|
|
return res(file)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|