MCPcopy Create free account
hub / github.com/coderwhy/coderhub / FileController

Class FileController

src/controller/file.controller.js:6–37  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4const { APP_HOST, APP_PORT } = require('../app/config');
5
6class FileController {
7 async saveAvatarInfo(ctx, next) {
8 // 1.获取图像相关的信息
9 const { filename, mimetype, size } = ctx.req.file;
10 const { id } = ctx.user;
11
12 // 2.将图像信息数据保存到数据库中
13 const result = await fileService.createAvatar(filename, mimetype, size, id);
14
15 // 3.将图片地址保存到user表中
16 const avatarUrl = `${APP_HOST}:${APP_PORT}/users/${id}/avatar`;
17 await userService.updateAvatarUrlById(avatarUrl, id);
18
19 // 4.返回结果
20 ctx.body = '上传头像成功~';
21 }
22
23 async savePictureInfo(ctx, next) {
24 // 1.获取图像信息
25 const files = ctx.req.files;
26 const { id } = ctx.user;
27 const { momentId } = ctx.query;
28
29 // 2.将所有的文件信息保存到数据库中
30 for (let file of files) {
31 const { filename, mimetype, size } = file;
32 await fileService.createFile(filename, mimetype, size, id, momentId);
33 }
34
35 ctx.body = '动态配图上传完成~'
36 }
37}
38
39module.exports = new FileController();

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected