From 8a0d051195b5f590baebcb708ff550e6ea17efe9 Mon Sep 17 00:00:00 2001 From: dudu Date: Sat, 5 Oct 2024 16:57:34 +0800 Subject: [PATCH] fix: revert upload-img changes --- .eslintrc.json | 2 +- src/cmd/upload-img/upload-img-from-path.ts | 22 +++++----- src/cmd/upload-img/upload-img.ts | 14 +++---- src/service/upload-img/image.service.ts | 47 ++++++++++++++++++++++ 4 files changed, 63 insertions(+), 22 deletions(-) create mode 100644 src/service/upload-img/image.service.ts diff --git a/.eslintrc.json b/.eslintrc.json index 6e160168..a8ca8520 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -17,7 +17,7 @@ } ], "@typescript-eslint/strict-boolean-expressions": [ - "error", + "warn", { "allowString": false, "allowNumber": false, diff --git a/src/cmd/upload-img/upload-img-from-path.ts b/src/cmd/upload-img/upload-img-from-path.ts index ceb1dc4b..4c8309f5 100644 --- a/src/cmd/upload-img/upload-img-from-path.ts +++ b/src/cmd/upload-img/upload-img-from-path.ts @@ -1,15 +1,13 @@ -import { readableToBytes } from '@/infra/convert/readableToBuffer' +import { Alert } from '@/infra/alert' +import { imageService } from '@/service/upload-img/image.service' import fs from 'fs' -import { RsHttp } from '@/wasm' -import { getAuthedImgReq } from '@/service/extract-img/get-replace-list' -export async function uploadImgFromPath(path: string) { - const readable = fs.createReadStream(path) - - const bytes = await readableToBytes(readable) - const req = await getAuthedImgReq() - const mime = RsHttp.mimeInfer(path) - if (mime === undefined) throw Error('未知的 MIME 类型') - - return req.upload(bytes, mime) +export function uploadImgFromPath(path: string) { + try { + const readStream = fs.createReadStream(path) + return imageService.upload(readStream) + } catch (e) { + console.log(`上传图片失败: ${e}`) + void Alert.err(`上传图片失败: ${e}`) + } } diff --git a/src/cmd/upload-img/upload-img.ts b/src/cmd/upload-img/upload-img.ts index 7dab189b..93d6a7ff 100644 --- a/src/cmd/upload-img/upload-img.ts +++ b/src/cmd/upload-img/upload-img.ts @@ -17,16 +17,12 @@ export async function uploadImg() { let imageUrl: string | undefined - try { - if (selected === options[0]) imageUrl = await uploadFsImage() - else if (selected === options[1]) imageUrl = await uploadClipboardImg() + if (selected === options[0]) imageUrl = await uploadFsImage() + else if (selected === options[1]) imageUrl = await uploadClipboardImg() - if (imageUrl === undefined) return + if (imageUrl === undefined) return - await showUploadSuccessModel(imageUrl) + await showUploadSuccessModel(imageUrl) - return imageUrl - } catch (e) { - void Alert.err(`上传图片失败: ${e}`) - } + return imageUrl } diff --git a/src/service/upload-img/image.service.ts b/src/service/upload-img/image.service.ts new file mode 100644 index 00000000..1b9655ec --- /dev/null +++ b/src/service/upload-img/image.service.ts @@ -0,0 +1,47 @@ +import { Readable } from 'stream' +import { isString, merge, pick } from 'lodash-es' +import path from 'path' +import httpClient from '@/infra/http-client' +import { ExtConst } from '@/ctx/ext-const' + +class ImageService { + async upload( + file: T + ): Promise { + // eslint-disable-next-line @typescript-eslint/naming-convention + const FormData = (await import('form-data')).default + const form = new FormData() + const { name, fileName, filename, path: _path } = file + const finalName = path.basename(isString(_path) ? _path : fileName || filename || name || 'image.png') + const ext = path.extname(finalName) + const mime = await import('mime') + const mimeType = mime.lookup(ext, 'image/png') + form.append('image', file, { filename: finalName, contentType: mimeType }) + const response = await httpClient.post(`${ExtConst.ApiBase.BLOG_BACKEND}/posts/body/images`, { + body: form, + }) + + return response.body + } + + /** + * Download the image from web + * This will reject if failed to download + * @param url The url of the web image + * @param name The name that expected applied to the downloaded image + * @returns The {@link Readable} stream + */ + async download(url: string, name?: string): Promise { + const response = await httpClient.get(url, { responseType: 'buffer' }) + const contentType = response.headers['content-type'] ?? 'image/png' + name = name ? 'image' : name + const mime = await import('mime') + + return merge(Readable.from(response.body), { + ...pick(response, 'httpVersion', 'headers'), + path: `${name}.${mime.extension(contentType) ?? 'png'}`, + }) + } +} + +export const imageService = new ImageService()