Browse Source

修改图片上传

zs 1 year ago
parent
commit
f79075ecc2

+ 0 - 2
src/config/config.default.ts

@@ -32,8 +32,6 @@ export default {
     cleanTimeout: 5 * 60 * 1000,
     // base64: boolean,设置原始body是否是base64格式,默认为false,一般用于腾讯云的兼容
     base64: false,
-    // 仅在匹配路径到 /api/upload 的时候去解析 body 中的文件信息
-    match: /\/api\/upload/,
     // 仅允许下面这些文件类型可以上传
     mimeTypeWhiteList: {
       '.jpg': 'image/jpeg',

+ 0 - 3
src/config/config.local.ts

@@ -5,9 +5,6 @@ const mongodb = 'file-upload_v1';
 export default {
   // use for cookie sign key, should change to your own and keep security
   keys: '1672292154640_488',
-  koa: {
-    globalPrefix: `/${project}`,
-  },
   swagger: {
     swaggerPath: `/dev/${project}/v1/api/doc`,
   },

+ 0 - 3
src/config/config.prod.ts

@@ -5,9 +5,6 @@ const mongodb = 'file-upload_v1';
 export default {
   // use for cookie sign key, should change to your own and keep security
   keys: '1672292154640_488',
-  koa: {
-    globalPrefix: `/${project}/`,
-  },
   swagger: {
     swaggerPath: `/${project}/v1/api/doc`,
   },

+ 6 - 4
src/controller/file.controller.ts

@@ -2,7 +2,7 @@ import { Controller, Inject, Post, Files, Fields } from '@midwayjs/core';
 import { FileService } from '../service/file.service';
 import { Context } from '@midwayjs/koa';
 
-@Controller('/api')
+@Controller('/file')
 export class FileController {
   @Inject()
   service: FileService;
@@ -10,10 +10,12 @@ export class FileController {
   @Inject()
   ctx: Context;
 
-  @Post('/upload')
+  @Post('/:appid/upload')
+  @Post('/:appid/:catalog/upload')
+  @Post('/:appid/:catalog/:item/upload')
   async upload(@Files() files, @Fields() fields) {
-    const route = this.ctx.query.route;
-    const result = await this.service.upload(files, fields, route);
+    const params = this.ctx.params;
+    const result = await this.service.upload(files, fields, params);
     return result;
   }
 }

+ 11 - 7
src/service/file.service.ts

@@ -6,15 +6,18 @@ import * as Path from 'node:path';
 const moment = require('moment');
 const sharp = require('sharp');
 const sep = Path.sep;
+const assert = require('assert');
 @Provide()
 export class FileService {
   @Config('export.root_path')
   root_path;
 
-  async upload(files, fields, route) {
-    const dirs = [];
-    if (route && route !== '_') {
-      const subs = route.split('_');
+  async upload(files, fields, params) {
+    const { appid, catalog, item } = params;
+    assert(appid);
+    const dirs = [appid];
+    if (catalog && catalog !== '_') {
+      const subs = catalog.split('_');
       dirs.push(...subs);
     }
     const rootPath = `${this.root_path}${sep}`;
@@ -37,10 +40,11 @@ export class FileService {
     }
     // TODO: 指定文件名或者按时间生成文件名
     // const name = moment().format('YYYYMMDDHHmmss');
-    const path = `${rootPath}${sep}${dirs.join(sep)}${sep}`;
-    const filePath = `${path}${sep}`;
+    const path = `${rootPath}${dirs.join(sep)}${sep}`;
+    const filePath = `${path}`;
     for (const val of files) {
-      const filename = moment().format('YYYYMMDDHHmmss');
+      let filename = item;
+      if (!item) filename = moment().format('YYYYMMDDHHmmss');
       await sharp(val.data)
         .toFormat('jpg', { mozjpeg: true })
         .toFile(`${filePath}${filename}.jpg`);