import * as fs from "fs" ; import { v4 as uuidv4 } from "uuid" ; import { Controller , Post } from "../core/decorator" ; import { createStringDate } from "../common/utils" ; import { defaultStaticConfig } from "../config" ; import BaseController from "./BaseController" ; @ Controller ( ) export default class CommonController extends BaseController { private readonly staticPath = defaultStaticConfig . staticPath ; param file @ Post ( "/upload/image" ) async uploadImage ( ctx ) { const { req } = ctx ; const files = await req . saveRequestFiles ( ) ; const file = files [ 0 ] ; if ( ! file . mimetype || file . mimetype . indexOf ( "image/" ) === - 1 ) { return this . fail ( "只能上传图片格式文件" ) ; } const dataPath = `upload/${ createStringDate ( "YYYYMMDD" ) }` ; const extArr = file . filename . split ( "." ) ; const ext = extArr . length > 1 ? extArr [ extArr . length - 1 ] . toLowerCase ( ) : "" ; const fileName = uuidv4 ( ) ; const urlPath = `${ dataPath }/${ fileName }.${ ext }` ; const savePath = `${ this . staticPath }/${ urlPath }` ; fs . mkdirSync ( `${ this . staticPath }/${ dataPath }` , { recursive : true } ) ; fs . createReadStream ( file . filepath ) . pipe ( fs . createWriteStream ( savePath ) ) ; return this . success ( { path : `${ defaultStaticConfig . staticPrefix }${ urlPath }` , } ) ; } }