-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(`上传图片失败: ${<string>e}`) | ||
void Alert.err(`上传图片失败: ${<string>e}`) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<T extends Readable & { name?: string; fileName?: string; filename?: string; path?: string | Buffer }>( | ||
file: T | ||
): Promise<string> { | ||
// 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') | ||
Check warning on line 15 in src/service/upload-img/image.service.ts
|
||
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<Readable> { | ||
const response = await httpClient.get(url, { responseType: 'buffer' }) | ||
const contentType = response.headers['content-type'] ?? 'image/png' | ||
name = name ? 'image' : name | ||
Check warning on line 37 in src/service/upload-img/image.service.ts
|
||
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() |