2022-01-05 14:47:51 +00:00
|
|
|
import path from 'path'
|
2022-04-07 17:15:27 +00:00
|
|
|
import sharp from 'sharp'
|
2022-01-05 14:47:51 +00:00
|
|
|
import fs from 'fs/promises'
|
2022-08-13 21:52:45 +00:00
|
|
|
import config from '../config.mjs'
|
2023-11-15 08:51:48 +00:00
|
|
|
import { HttpError, CorsHandler } from 'flaska'
|
2021-10-11 00:21:57 +00:00
|
|
|
import * as security from './security.mjs'
|
2021-10-11 00:56:22 +00:00
|
|
|
import * as formidable from './formidable.mjs'
|
2017-12-10 09:45:38 +00:00
|
|
|
|
2021-10-11 00:21:57 +00:00
|
|
|
export default class MediaRoutes {
|
|
|
|
constructor(opts = {}) {
|
|
|
|
Object.assign(this, {
|
|
|
|
security: opts.security || security,
|
2021-10-11 00:56:22 +00:00
|
|
|
formidable: opts.formidable || formidable,
|
2022-01-05 14:47:51 +00:00
|
|
|
sharp: opts.sharp || sharp,
|
|
|
|
fs: opts.fs || fs,
|
2021-10-11 00:21:57 +00:00
|
|
|
})
|
2022-01-05 14:47:51 +00:00
|
|
|
this.siteCache = new Map()
|
|
|
|
this.collator = new Intl.Collator('is-IS', { numeric: false, sensitivity: 'accent' })
|
2023-11-15 13:21:22 +00:00
|
|
|
this.cors = CorsHandler({ allowedOrigins: ['*'] })
|
2022-01-05 14:47:51 +00:00
|
|
|
}
|
|
|
|
|
2022-08-13 21:52:45 +00:00
|
|
|
register(server) {
|
2022-08-15 19:51:13 +00:00
|
|
|
this.init(server).then(function() {}, function(err) {
|
2022-08-13 21:52:45 +00:00
|
|
|
server.core.log.error(err, 'Error initing media')
|
|
|
|
})
|
|
|
|
|
|
|
|
server.flaska.get('/media', [server.queryHandler()], this.listFiles.bind(this))
|
|
|
|
server.flaska.get('/media/:site', this.listPublicFiles.bind(this))
|
|
|
|
server.flaska.post('/media', [server.queryHandler()], this.upload.bind(this))
|
|
|
|
server.flaska.post('/media/noprefix', [server.queryHandler()], this.uploadNoPrefix.bind(this))
|
|
|
|
server.flaska.post('/media/resize', [server.queryHandler()], this.resize.bind(this))
|
|
|
|
server.flaska.post('/media/resize/:filename', [server.queryHandler(), server.jsonHandler()], this.resizeExisting.bind(this))
|
|
|
|
server.flaska.delete('/media/:filename', [server.queryHandler()], this.remove.bind(this))
|
2023-11-15 13:21:22 +00:00
|
|
|
server.flaska.options('/::path', [server.queryHandler(), this.security.verifyCorsEnabled], this.cors)
|
2022-08-13 21:52:45 +00:00
|
|
|
}
|
|
|
|
|
2022-08-15 19:51:13 +00:00
|
|
|
init(server) {
|
|
|
|
let folders = Object.keys(config.get('sites'))
|
|
|
|
|
|
|
|
return Promise.all(folders.map(folder => {
|
|
|
|
return fs.readdir(config.get('uploadFolder') + '/' + folder)
|
|
|
|
.then(files => {
|
|
|
|
return files.map(file => {
|
|
|
|
return { filename: file, size: null }
|
2022-01-05 14:47:51 +00:00
|
|
|
})
|
2022-08-15 19:51:13 +00:00
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
server.core.log.error(err, `Error reading folder "${config.get('uploadFolder')}/${folder}"`)
|
|
|
|
return []
|
|
|
|
})
|
|
|
|
.then(files => {
|
|
|
|
this.siteCache.set(folder, files)
|
|
|
|
})
|
|
|
|
}))
|
2022-01-05 14:47:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
filesCacheGet(site) {
|
|
|
|
let files = this.siteCache.get(site) || []
|
|
|
|
return files.sort((a, b) => {
|
2022-01-06 09:01:10 +00:00
|
|
|
return this.collator.compare(a.filename, b.filename)
|
2022-01-05 14:47:51 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
filesCacheAdd(site, filename, size) {
|
|
|
|
let arr = this.siteCache.get(site)
|
|
|
|
if (!arr) {
|
|
|
|
this.siteCache.set(site, arr = [])
|
|
|
|
}
|
2022-01-06 09:01:10 +00:00
|
|
|
let found = false
|
|
|
|
for (let file of arr) {
|
|
|
|
if (file.filename === filename) {
|
|
|
|
found = true
|
|
|
|
file.size = size
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
|
|
|
arr.push({ filename: filename, size: size })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
filesCacheRemove(site, filename) {
|
|
|
|
let arr = this.siteCache.get(site)
|
|
|
|
if (!arr) return
|
|
|
|
|
|
|
|
for (let i = 0; i < arr.length; i++) {
|
|
|
|
if (arr[i].filename === filename) {
|
|
|
|
arr.splice(i, 1)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2022-01-05 14:47:51 +00:00
|
|
|
}
|
|
|
|
|
2023-11-15 13:21:22 +00:00
|
|
|
getSite(ctx) {
|
|
|
|
let site = this.security.verifyToken(ctx)
|
|
|
|
if (this.security.hasCors(site)) {
|
|
|
|
this.cors(ctx)
|
|
|
|
}
|
|
|
|
return site
|
|
|
|
}
|
|
|
|
|
2022-01-05 14:47:51 +00:00
|
|
|
async listFiles(ctx) {
|
2023-11-15 13:21:22 +00:00
|
|
|
let site = this.getSite(ctx)
|
2022-01-05 14:47:51 +00:00
|
|
|
|
|
|
|
ctx.body = this.filesCacheGet(site)
|
|
|
|
}
|
|
|
|
|
|
|
|
async listPublicFiles(ctx) {
|
|
|
|
this.security.throwIfNotPublic(ctx.params.site)
|
2023-11-15 13:21:22 +00:00
|
|
|
|
|
|
|
if (this.security.hasCors(ctx.params.site)) {
|
|
|
|
this.cors(ctx)
|
|
|
|
}
|
2022-01-05 14:47:51 +00:00
|
|
|
|
|
|
|
ctx.body = this.filesCacheGet(ctx.params.site)
|
2021-10-11 00:21:57 +00:00
|
|
|
}
|
|
|
|
|
2022-01-06 09:01:10 +00:00
|
|
|
async upload(ctx, noprefix = false) {
|
2023-11-15 13:21:22 +00:00
|
|
|
let site = this.getSite(ctx)
|
2022-01-05 14:47:51 +00:00
|
|
|
ctx.state.site = site
|
2017-12-10 09:45:38 +00:00
|
|
|
|
2022-01-06 09:01:10 +00:00
|
|
|
let result = await this.formidable.uploadFile(ctx, ctx.state.site, noprefix)
|
2017-12-10 09:45:38 +00:00
|
|
|
|
2021-10-11 03:39:01 +00:00
|
|
|
ctx.log.info(`Uploaded ${result.filename}`)
|
|
|
|
|
2022-08-13 21:52:45 +00:00
|
|
|
let stat = await this.fs.stat(`${config.get('uploadFolder')}/${ctx.state.site}/${result.filename}`)
|
2022-01-05 14:47:51 +00:00
|
|
|
this.filesCacheAdd(ctx.state.site, result.filename, stat.size)
|
|
|
|
|
2021-10-11 00:21:57 +00:00
|
|
|
ctx.body = {
|
|
|
|
filename: result.filename,
|
2022-01-05 14:47:51 +00:00
|
|
|
path: `/${ctx.state.site}/${result.filename}`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-06 09:01:10 +00:00
|
|
|
uploadNoPrefix(ctx) {
|
|
|
|
return this.upload(ctx, true)
|
|
|
|
}
|
|
|
|
|
2022-04-07 11:35:29 +00:00
|
|
|
async resizeFile(ctx, sourceFile) {
|
2022-01-05 14:47:51 +00:00
|
|
|
this.security.verifyBody(ctx)
|
|
|
|
|
|
|
|
let keys = Object.keys(ctx.req.body)
|
|
|
|
|
2022-01-06 09:51:43 +00:00
|
|
|
let allowedOperations = [
|
|
|
|
'trim',
|
|
|
|
'flatten',
|
|
|
|
'resize',
|
|
|
|
'blur',
|
|
|
|
'extend',
|
|
|
|
]
|
|
|
|
|
2022-10-17 09:45:28 +00:00
|
|
|
await Promise.all(keys.filter(key => ctx.req.body[key]).map(key => {
|
2022-01-05 14:47:51 +00:00
|
|
|
return Promise.resolve()
|
|
|
|
.then(async () => {
|
|
|
|
let item = ctx.req.body[key]
|
2022-08-13 21:52:45 +00:00
|
|
|
let sharp = this.sharp(`${config.get('uploadFolder')}/${ctx.state.site}/${sourceFile}`)
|
2022-01-05 14:47:51 +00:00
|
|
|
.rotate()
|
|
|
|
|
2022-01-06 09:51:43 +00:00
|
|
|
for (let operation of allowedOperations) {
|
|
|
|
if (item[operation] != null) {
|
|
|
|
sharp = sharp[operation](item[operation])
|
|
|
|
}
|
2022-01-05 14:47:51 +00:00
|
|
|
}
|
|
|
|
sharp = sharp[item.format](item[item.format])
|
|
|
|
|
2022-04-07 11:35:29 +00:00
|
|
|
let target = sourceFile
|
2022-01-05 14:47:51 +00:00
|
|
|
if (path.extname(target).length > 0) {
|
|
|
|
target = target.slice(0, -path.extname(target).length)
|
|
|
|
}
|
|
|
|
target += `_${key}.${item.format.replace('jpeg', 'jpg')}`
|
|
|
|
|
|
|
|
if (item.out === 'base64') {
|
|
|
|
let buffer = await sharp.toBuffer()
|
2022-01-06 09:01:10 +00:00
|
|
|
ctx.body[key] = {
|
2022-01-05 14:47:51 +00:00
|
|
|
base64: `data:image/${item.format};base64,` + buffer.toString('base64'),
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2022-08-13 21:52:45 +00:00
|
|
|
await sharp.toFile(`${config.get('uploadFolder')}/${ctx.state.site}/${target}`)
|
2022-01-05 14:47:51 +00:00
|
|
|
|
2022-08-13 21:52:45 +00:00
|
|
|
let stat = await this.fs.stat(`${config.get('uploadFolder')}/${ctx.state.site}/${target}`)
|
2022-01-05 14:47:51 +00:00
|
|
|
this.filesCacheAdd(ctx.state.site, target, stat.size)
|
|
|
|
|
2022-01-06 09:01:10 +00:00
|
|
|
ctx.body[key] = {
|
2022-01-05 14:47:51 +00:00
|
|
|
filename: target,
|
|
|
|
path: `/${ctx.state.site}/${target}`,
|
|
|
|
}
|
|
|
|
}).then(
|
|
|
|
function() {},
|
|
|
|
function(err) {
|
2022-08-13 21:52:45 +00:00
|
|
|
throw new HttpError(422, `Error processing ${key}: ${err.message}`)
|
2022-01-05 14:47:51 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}))
|
2022-01-06 09:01:10 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 11:35:29 +00:00
|
|
|
async resizeExisting(ctx) {
|
2023-11-15 13:21:22 +00:00
|
|
|
let site = this.getSite(ctx)
|
2022-04-07 11:35:29 +00:00
|
|
|
ctx.state.site = site
|
|
|
|
|
|
|
|
ctx.body = {}
|
|
|
|
await this.resizeFile(ctx, ctx.params.filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
async resize(ctx) {
|
|
|
|
await this.upload(ctx)
|
|
|
|
|
|
|
|
await this.resizeFile(ctx, ctx.body.filename)
|
|
|
|
}
|
|
|
|
|
2022-01-06 09:01:10 +00:00
|
|
|
async remove(ctx) {
|
2023-11-15 13:21:22 +00:00
|
|
|
let site = this.getSite(ctx)
|
2022-01-06 09:01:10 +00:00
|
|
|
|
|
|
|
this.filesCacheRemove(site, ctx.params.filename)
|
|
|
|
|
2022-08-16 08:36:08 +00:00
|
|
|
let root = path.join(config.get('uploadFolder'), site)
|
2022-08-16 08:30:27 +00:00
|
|
|
var unlinkPath = path.join(root, decodeURIComponent(ctx.params.filename))
|
|
|
|
|
|
|
|
if (unlinkPath.indexOf(root) !== 0) {
|
|
|
|
throw new HttpError(403, `Error removing ${unlinkPath}: Traversing folder is not allowed`)
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.fs.unlink(unlinkPath)
|
2022-01-06 09:01:10 +00:00
|
|
|
.catch(function(err) {
|
2022-08-16 08:30:27 +00:00
|
|
|
throw new HttpError(422, `Error removing ${unlinkPath}: ${err.message}`)
|
2022-01-06 09:01:10 +00:00
|
|
|
})
|
2022-01-05 14:47:51 +00:00
|
|
|
|
2022-01-06 09:01:10 +00:00
|
|
|
ctx.status = 204
|
2017-12-10 09:45:38 +00:00
|
|
|
}
|
|
|
|
}
|