Skip to content

Commit 8a0d051

Browse files
committed
fix: revert upload-img changes
1 parent 98e2db1 commit 8a0d051

File tree

4 files changed

+63
-22
lines changed

4 files changed

+63
-22
lines changed

.eslintrc.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
}
1818
],
1919
"@typescript-eslint/strict-boolean-expressions": [
20-
"error",
20+
"warn",
2121
{
2222
"allowString": false,
2323
"allowNumber": false,
+10-12
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
import { readableToBytes } from '@/infra/convert/readableToBuffer'
1+
import { Alert } from '@/infra/alert'
2+
import { imageService } from '@/service/upload-img/image.service'
23
import fs from 'fs'
3-
import { RsHttp } from '@/wasm'
4-
import { getAuthedImgReq } from '@/service/extract-img/get-replace-list'
54

6-
export async function uploadImgFromPath(path: string) {
7-
const readable = fs.createReadStream(path)
8-
9-
const bytes = await readableToBytes(readable)
10-
const req = await getAuthedImgReq()
11-
const mime = RsHttp.mimeInfer(path)
12-
if (mime === undefined) throw Error('未知的 MIME 类型')
13-
14-
return req.upload(bytes, mime)
5+
export function uploadImgFromPath(path: string) {
6+
try {
7+
const readStream = fs.createReadStream(path)
8+
return imageService.upload(readStream)
9+
} catch (e) {
10+
console.log(`上传图片失败: ${<string>e}`)
11+
void Alert.err(`上传图片失败: ${<string>e}`)
12+
}
1513
}

src/cmd/upload-img/upload-img.ts

+5-9
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,12 @@ export async function uploadImg() {
1717

1818
let imageUrl: string | undefined
1919

20-
try {
21-
if (selected === options[0]) imageUrl = await uploadFsImage()
22-
else if (selected === options[1]) imageUrl = await uploadClipboardImg()
20+
if (selected === options[0]) imageUrl = await uploadFsImage()
21+
else if (selected === options[1]) imageUrl = await uploadClipboardImg()
2322

24-
if (imageUrl === undefined) return
23+
if (imageUrl === undefined) return
2524

26-
await showUploadSuccessModel(imageUrl)
25+
await showUploadSuccessModel(imageUrl)
2726

28-
return imageUrl
29-
} catch (e) {
30-
void Alert.err(`上传图片失败: ${<string>e}`)
31-
}
27+
return imageUrl
3228
}
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { Readable } from 'stream'
2+
import { isString, merge, pick } from 'lodash-es'
3+
import path from 'path'
4+
import httpClient from '@/infra/http-client'
5+
import { ExtConst } from '@/ctx/ext-const'
6+
7+
class ImageService {
8+
async upload<T extends Readable & { name?: string; fileName?: string; filename?: string; path?: string | Buffer }>(
9+
file: T
10+
): Promise<string> {
11+
// eslint-disable-next-line @typescript-eslint/naming-convention
12+
const FormData = (await import('form-data')).default
13+
const form = new FormData()
14+
const { name, fileName, filename, path: _path } = file
15+
const finalName = path.basename(isString(_path) ? _path : fileName || filename || name || 'image.png')
16+
const ext = path.extname(finalName)
17+
const mime = await import('mime')
18+
const mimeType = mime.lookup(ext, 'image/png')
19+
form.append('image', file, { filename: finalName, contentType: mimeType })
20+
const response = await httpClient.post(`${ExtConst.ApiBase.BLOG_BACKEND}/posts/body/images`, {
21+
body: form,
22+
})
23+
24+
return response.body
25+
}
26+
27+
/**
28+
* Download the image from web
29+
* This will reject if failed to download
30+
* @param url The url of the web image
31+
* @param name The name that expected applied to the downloaded image
32+
* @returns The {@link Readable} stream
33+
*/
34+
async download(url: string, name?: string): Promise<Readable> {
35+
const response = await httpClient.get(url, { responseType: 'buffer' })
36+
const contentType = response.headers['content-type'] ?? 'image/png'
37+
name = name ? 'image' : name
38+
const mime = await import('mime')
39+
40+
return merge(Readable.from(response.body), {
41+
...pick(response, 'httpVersion', 'headers'),
42+
path: `${name}.${mime.extension(contentType) ?? 'png'}`,
43+
})
44+
}
45+
}
46+
47+
export const imageService = new ImageService()

0 commit comments

Comments
 (0)