2019-09-14 19:03:38 +00:00
|
|
|
import config from '../config.mjs'
|
|
|
|
import Media from './model.mjs'
|
|
|
|
import * as multer from '../multer.mjs'
|
|
|
|
import Resizer from './resize.mjs'
|
|
|
|
import { uploadFile } from './upload.mjs'
|
|
|
|
import Jwt from '../jwt.mjs'
|
2019-02-20 16:10:37 +00:00
|
|
|
|
|
|
|
export default class MediaRoutes {
|
|
|
|
constructor(opts = {}) {
|
|
|
|
Object.assign(this, {
|
|
|
|
Media: opts.Media || Media,
|
|
|
|
multer: opts.multer || multer,
|
|
|
|
resize: opts.resize || new Resizer(),
|
|
|
|
jwt: opts.jwt || new Jwt(),
|
|
|
|
uploadFile: opts.uploadFile || uploadFile,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async upload(ctx) {
|
|
|
|
let result = await this.multer.processBody(ctx)
|
|
|
|
|
2019-10-01 17:18:20 +00:00
|
|
|
let height = null
|
|
|
|
if (ctx.query.height) {
|
|
|
|
height = Number(ctx.query.height)
|
|
|
|
}
|
|
|
|
|
2019-02-20 16:10:37 +00:00
|
|
|
let smallPath = await this.resize.createSmall(result.path)
|
2019-10-01 17:18:20 +00:00
|
|
|
let mediumPath = await this.resize.createMedium(result.path, height)
|
2019-09-23 18:37:02 +00:00
|
|
|
let largePath = await this.resize.createLarge(result.path)
|
2019-02-20 16:10:37 +00:00
|
|
|
|
|
|
|
let token = this.jwt.signDirect({ site: config.get('upload:name') }, config.get('upload:secret'))
|
|
|
|
|
2019-09-23 18:37:02 +00:00
|
|
|
let [org, small, medium, large] = await Promise.all([
|
2019-02-20 16:10:37 +00:00
|
|
|
this.uploadFile(token, result.path),
|
|
|
|
this.uploadFile(token, smallPath),
|
|
|
|
this.uploadFile(token, mediumPath),
|
2019-09-23 18:37:02 +00:00
|
|
|
this.uploadFile(token, largePath),
|
2019-02-20 16:10:37 +00:00
|
|
|
])
|
|
|
|
|
|
|
|
ctx.body = await this.Media.create({
|
|
|
|
filename: result.originalname,
|
|
|
|
filetype: result.mimetype,
|
|
|
|
small_image: small.path,
|
|
|
|
medium_image: medium.path,
|
|
|
|
large_image: large.path,
|
2019-09-23 18:37:02 +00:00
|
|
|
org_image: org.path,
|
2019-02-20 16:10:37 +00:00
|
|
|
size: result.size,
|
|
|
|
staff_id: ctx.state.user.id,
|
|
|
|
})
|
|
|
|
}
|
2019-09-13 13:33:10 +00:00
|
|
|
|
|
|
|
async getAllMedia(ctx) {
|
|
|
|
ctx.body = await this.Media.getAll(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
async removeMedia(ctx) {
|
|
|
|
let media = await this.Media.getSingle(ctx.params.id)
|
|
|
|
|
|
|
|
media.set({
|
|
|
|
is_deleted: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
await media.save()
|
|
|
|
|
|
|
|
ctx.status = 200
|
|
|
|
}
|
2019-02-20 16:10:37 +00:00
|
|
|
}
|