import { Service } from 'egg' ; import { EggFile } from 'egg-multipart' ; import { formatTimeByFormater , now } from '../utils/date' ; import path = require ( 'path' ) ; import fs = require ( 'fs' ) ; import { getImageInfo } from '../utils/image' ; interface UploadImageProps { url : string ; width : number ; height : number ; } class UploadService extends Service { public async uploadFilesToQiniu ( files ) { const result = [ ] ; for ( const file of files ) { const name = this . relativeFilePath ( file ) ; const imageInfo = await getImageInfo ( file ) ; const data = await this . service . qiniu . upload ( name , file . filepath ) ; result . push ( { url : data . url , width : imageInfo . width , height : imageInfo . height , } ) ; } return result ; } public async uploadFilesToLocalServer ( files ) { const { ctx , config : { oss } , } = this ; if ( ! oss . local ) { throw new Error ( 'oss local config not found' ) ; } const origin = oss . local && oss . local . host ? oss . local . host : ctx . request . protocol + '://' + ctx . request . host ; const result = [ ] ; const staticDir = path . resolve ( __dirname , '../../' + oss . local . dir ) ; try { const stats = await this . readDir ( staticDir ) ; if ( ! stats . isDirectory ( ) ) { throw new Error ( 'oss local dir is not dir' ) ; } } catch ( error ) { throw new Error ( 'oss local dir is not found' ) ; } const now = formatTimeByFormater ( new Date ( ) , 'YYYY-MM-DD' ) ; const dayDir = staticDir + '/' + now ; try { await this . readDir ( dayDir ) ; } catch ( error ) { await this . mkdir ( dayDir ) ; } for ( const file of files ) { const name = this . relativeFilePath ( file ) ; const filePath = staticDir + '/' + name ; const imageInfo = await getImageInfo ( file ) ; await this . rename ( file . filepath , filePath ) ; result . push ( { url : origin + ( oss . local . prefix ? oss . local . prefix : '' ) + '/' + name , width : imageInfo . width , height : imageInfo . height , } ) ; } return result ; } private getFileType ( file ) { return file . mime . split ( '/' ) . pop ( ) ; } private relativeFilePath ( file ) { return ( formatTimeByFormater ( new Date ( ) , 'YYYY-MM-DD' ) + '/' + this . ctx . user . id + now ( ) + '.' + this . getFileType ( file ) ) ; } private readDir ( path ) { return new Promise ( ( resolve , reject ) => { fs . stat ( path , ( err , stats ) => { if ( err ) { reject ( err ) ; } resolve ( stats ) ; } ) ; } ) ; } private mkdir ( path ) { return new Promise ( ( resolve , reject ) => { fs . mkdir ( path , err => { if ( err ) { reject ( err ) ; } resolve ( ) ; } ) ; } ) ; } private rename ( oldPath , path ) { return new Promise ( ( resolve , reject ) => { fs . rename ( oldPath , path , err => { if ( err ) { reject ( err ) ; } resolve ( ) ; } ) ; } ) ; } } export default UploadService ;