noprefix now auto-adds prefix if file exists.
All checks were successful
continuous-integration/appveyor/branch AppVeyor build succeeded

This commit is contained in:
Jonatan Nilsson 2022-08-13 01:03:06 +00:00
parent 2b31fce045
commit 9a0fca4a22
3 changed files with 68 additions and 20 deletions

View file

@ -6,30 +6,35 @@ import config from '../config.mjs'
let lastDateString = '' let lastDateString = ''
let incrementor = 1 let incrementor = 1
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
}
export function uploadFile(ctx, siteName, noprefix = false) { export function uploadFile(ctx, siteName, noprefix = false) {
return new Promise((res, rej) => { return new Promise((res, rej) => {
const date = new Date()
let prefix = '' let prefix = ''
if (!noprefix) {
// 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
}
}
var form = new formidable.IncomingForm() var form = new formidable.IncomingForm()
form.uploadDir = `./public/${siteName}` form.uploadDir = `./public/${siteName}`
form.maxFileSize = config.get('fileSize') form.maxFileSize = config.get('fileSize')
@ -46,6 +51,10 @@ export function uploadFile(ctx, siteName, noprefix = false) {
}) })
ctx.req.body = fields ctx.req.body = fields
if (!noprefix || fs.existsSync(`./public/${siteName}/${prefix}${file.name}`)) {
prefix = getPrefix()
}
fs.rename(files.file.path, `./public/${siteName}/${prefix}${file.name}`, function(err) { fs.rename(files.file.path, `./public/${siteName}/${prefix}${file.name}`, function(err) {
if (err) return rej(err) if (err) return rej(err)
file.path = `./public/${siteName}/${prefix}${file.name}` file.path = `./public/${siteName}/${prefix}${file.name}`

View file

@ -1,6 +1,6 @@
{ {
"name": "storage-upload", "name": "storage-upload",
"version": "2.1.3", "version": "2.1.4",
"description": "", "description": "",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {

View file

@ -183,6 +183,45 @@ t.describe('Media (API)', () => {
assert.strictEqual(img.height, 700) assert.strictEqual(img.height, 700)
assert.strictEqual(img.format, 'png') assert.strictEqual(img.format, 'png')
}) })
t.test('should upload and create file with prefix if exists', async () => {
let token = encode(null, { iss: 'development' }, secret)
await fs.unlink(resolve(`../../public/development/test.png`)).catch(function() {})
let datas = []
datas.push(await client.upload(
`/media/noprefix?token=${token}`,
resolve('test.png')
))
datas.push(await client.upload(
`/media/noprefix?token=${token}`,
resolve('test.png')
))
testFiles.push(datas[0].path)
testFiles.push(datas[1].path)
let prefix = new Date().toISOString().replace(/-/g,'').split('T')[0]
let first = datas[0]
let second = datas[1]
assert.notStrictEqual(first.filename, second.filename)
assert.notOk(first.filename.startsWith(currYear))
assert.notOk(first.filename.startsWith(prefix))
assert.ok(second.filename.startsWith(currYear))
assert.ok(second.filename.startsWith(prefix))
let stats = await Promise.all([
fs.stat(resolve('test.png')),
fs.stat(resolve('../../public/development/test.png')),
fs.stat(resolve('../../public/development/' + second.filename)),
])
assert.strictEqual(stats[0].size, stats[1].size)
assert.strictEqual(stats[0].size, stats[2].size)
})
}) })
t.timeout(10000).describe('POST /media/resize', function temp() { t.timeout(10000).describe('POST /media/resize', function temp() {