-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #306 from cnblogs/revert-upload-image-changes
fix: revert upload-img changes
- Loading branch information
Showing
4 changed files
with
63 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / node-lint
Check warning on line 15 in src/service/upload-img/image.service.ts GitHub Actions / node-lint
|
||
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 | ||
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() |