Skip to content

Commit 1b9b54e

Browse files
committed
fix: upload images error
1 parent 8f427cb commit 1b9b54e

File tree

4 files changed

+46
-43
lines changed

4 files changed

+46
-43
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
"qiniu": "^7.8.0",
7777
"reflect-metadata": "^0.1.13",
7878
"rxjs": "^7.2.0",
79+
"url": "^0.11.0",
7980
"wildcard-match": "^5.1.2",
8081
"zx-cjs": "7.0.7-0"
8182
},

pnpm-lock.yaml

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/modules/upload/upload.controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ export class UploadController {
1717
@Auth() // 权限校验
1818
@UseInterceptors(FileInterceptor('file')) // 处理文件中间件 file 与上传的字段对应
1919
upload(@UploadedFile() file: Express.Multer.File) {
20-
return this.UploadService.upload(file)
20+
return this.UploadService.uploadPhoto(file)
2121
}
2222
}

src/modules/upload/upload.service.ts

Lines changed: 25 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,52 @@
11
import qiniu from 'qiniu'
2+
import * as url from 'url'
23

3-
import {
4-
BadRequestException,
5-
Injectable,
6-
InternalServerErrorException,
7-
} from '@nestjs/common'
4+
import { Injectable, InternalServerErrorException } from '@nestjs/common'
85

96
import { QINIU_SECRET } from '~/app.config'
107

11-
const mac = new qiniu.auth.digest.Mac(QINIU_SECRET.qn_ak, QINIU_SECRET.qn_sk)
12-
const putPolicy = new qiniu.rs.PutPolicy({
13-
scope: QINIU_SECRET.qn_scope,
14-
})
15-
const uploadToken = putPolicy.uploadToken(mac)
16-
17-
const config = new qiniu.conf.Config()
18-
const bucketManager = new qiniu.rs.BucketManager(mac, config)
19-
208
@Injectable()
219
export class UploadService {
22-
upload(file: Express.Multer.File): Promise<string> {
23-
if (!file) {
24-
throw new BadRequestException('请传入图片')
25-
}
26-
const filename = `${Date.now()}-${file.originalname}`
10+
async uploadPhoto(file: Express.Multer.File) {
11+
const mac = new qiniu.auth.digest.Mac(
12+
QINIU_SECRET.qn_ak,
13+
QINIU_SECRET.qn_sk,
14+
)
15+
const putPolicy = new qiniu.rs.PutPolicy({
16+
scope: QINIU_SECRET.qn_scope,
17+
})
18+
const uploadToken = putPolicy.uploadToken(mac)
19+
20+
// upload
2721
const formUploader = new qiniu.form_up.FormUploader(
2822
new qiniu.conf.Config({
2923
zone: qiniu.zone.Zone_z2,
3024
}),
3125
)
32-
const putExtra = new qiniu.form_up.PutExtra()
33-
34-
return new Promise((resolve) => {
26+
const img = (await new Promise((_res, _rej) => {
3527
formUploader.put(
3628
uploadToken,
37-
filename,
29+
`${Date.now()}-${file.originalname}`,
3830
file.buffer,
39-
putExtra,
31+
new qiniu.form_up.PutExtra(),
4032
(respErr, respBody, respInfo) => {
4133
if (respErr) {
34+
console.error(respErr)
4235
throw new InternalServerErrorException(respErr.message)
4336
}
37+
4438
if (respInfo.statusCode == 200) {
45-
resolve(`${QINIU_SECRET.qn_host}/${respBody.key}`)
39+
_res({
40+
url: new url.URL(respBody.key, QINIU_SECRET.qn_host).href,
41+
})
4642
} else {
47-
throw new InternalServerErrorException(respErr.message)
43+
console.error(respInfo.statusCode, respBody)
44+
throw new InternalServerErrorException(respInfo)
4845
}
4946
},
5047
)
51-
})
52-
}
48+
})) as { url: string }
5349

54-
remove(fileName: string) {
55-
return new Promise((resolve) => {
56-
bucketManager.delete(
57-
QINIU_SECRET.qn_scope,
58-
fileName,
59-
(err, respBody, respInfo) => {
60-
if (err) {
61-
throw new InternalServerErrorException(err.message)
62-
} else {
63-
resolve('删除成功')
64-
}
65-
},
66-
)
67-
})
50+
return img.url
6851
}
6952
}

0 commit comments

Comments
 (0)