From 17ca5de28f7e1598a24ae07c7233e0d54d6c173a Mon Sep 17 00:00:00 2001 From: purocean Date: Fri, 8 Nov 2024 17:15:15 +0800 Subject: [PATCH 1/2] feat: enhance image upload handling by validating content type and improving file name extraction --- src/renderer/plugins/image-localization.ts | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/renderer/plugins/image-localization.ts b/src/renderer/plugins/image-localization.ts index 010e129c..bebe6bc6 100644 --- a/src/renderer/plugins/image-localization.ts +++ b/src/renderer/plugins/image-localization.ts @@ -1,6 +1,6 @@ import mime from 'mime' import * as api from '@fe/support/api' -import { encodeMarkdownLink } from '@fe/utils' +import { encodeMarkdownLink, removeQuery } from '@fe/utils' import { useToast } from '@fe/support/ui/toast' import store from '@fe/support/store' import { replaceValue } from '@fe/services/editor' @@ -42,8 +42,20 @@ async function transformImgOutLink (img: HTMLImageElement) { const headers = JSON.parse(img.getAttribute('headers') || '{}') const res = await api.proxyFetch(img.src, { headers }) const blob = await res.blob() - const imgFile = new File([blob!], 'file.' + mime.getExtension(res.headers.get('content-type')!)) - const assetPath = await upload(imgFile, currentFile) + const contentType = res.headers.get('content-type') || '' + + if (!contentType.startsWith('image/')) { + throw new Error('Not an image') + } + + const ext = mime.getExtension(contentType) || '' + const imgFile = new File([blob!], 'file.' + ext) + const name = removeQuery(img.src).split('/').pop() // get file name from url + const assetPath = await upload( + imgFile, + currentFile, + ext === name?.split('.').pop() ? name : undefined // if ext is not same as file name, use file name + ) replacedLink = assetPath } From 30d5ab77dd7f13155e6957c5e5dfb68b6eec937a Mon Sep 17 00:00:00 2001 From: purocean Date: Fri, 8 Nov 2024 17:26:45 +0800 Subject: [PATCH 2/2] fix: handle errors in file stat retrieval to prevent crashes --- src/main/server/file.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/server/file.ts b/src/main/server/file.ts index fcb56d78..5622ff49 100644 --- a/src/main/server/file.ts +++ b/src/main/server/file.ts @@ -296,7 +296,14 @@ async function travels ( await Promise.all(list.map(async name => { const p = path.join(location, name) - const stat = await fs.stat(p) + const stat = await fs.stat(p).catch(e => { + console.error('travels', p, e) + return null + }) + + if (!stat) { + return + } if (stat.isFile()) { if (excludeRegex.test(name)) {