Skip to content

Commit 4186033

Browse files
committed
refactor(publish): enhance image and video processing with improved error handling
- Ensure imgElements, images, and videos are validated as arrays before processing - Add warnings for missing audio and video data - Streamline the image upload process and improve debug logging for better traceability
1 parent 4aa6694 commit 4186033

File tree

1 file changed

+66
-41
lines changed

1 file changed

+66
-41
lines changed

src/tabs/publish.tsx

Lines changed: 66 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -70,40 +70,42 @@ export default function Publish() {
7070
let processedCoverImage: FileData | null = null;
7171

7272
// 处理所有图片
73-
for (const img of imgElements) {
74-
try {
75-
const originalUrl = img.src;
76-
// 跳过已经是 blob URL 的图片
77-
if (originalUrl.startsWith('blob:')) continue;
78-
79-
// 下载图片并创建 blob URL
80-
const response = await fetch(originalUrl);
81-
const blob = await response.blob();
82-
const blobUrl = URL.createObjectURL(blob);
83-
84-
// 替换 HTML 中的图片 URL
85-
img.src = blobUrl;
86-
blobUrls.push(blobUrl);
87-
88-
processedImages.push({
89-
name: images?.find((image) => image.url === originalUrl)?.name || originalUrl.split('/').pop() || blobUrl,
90-
url: blobUrl,
91-
type: blob.type,
92-
size: blob.size,
93-
});
94-
95-
// 替换 markdown 中的图片 URL
96-
// 使用正则表达式匹配 markdown 中的图片语法
97-
const escapedUrl = originalUrl.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
98-
const imgRegex = new RegExp(`!\\[.*?\\]\\(${escapedUrl}\\)`, 'g');
99-
processedMarkdownContent = processedMarkdownContent.replace(imgRegex, (match) => {
100-
return match.replace(originalUrl, blobUrl);
101-
});
102-
} catch (error) {
103-
console.error('处理图片时出错:', error);
104-
// 继续处理下一张图片
105-
setNotice(chrome.i18n.getMessage('errorProcessImage', [img.src]));
106-
setErrors((prev) => [...prev, chrome.i18n.getMessage('errorProcessImage', [img.src])]);
73+
if (Array.isArray(imgElements) && imgElements.length > 0) {
74+
for (const img of imgElements) {
75+
try {
76+
const originalUrl = img.src;
77+
// 跳过已经是 blob URL 的图片
78+
if (originalUrl.startsWith('blob:')) continue;
79+
80+
// 下载图片并创建 blob URL
81+
const response = await fetch(originalUrl);
82+
const blob = await response.blob();
83+
const blobUrl = URL.createObjectURL(blob);
84+
85+
// 替换 HTML 中的图片 URL
86+
img.src = blobUrl;
87+
blobUrls.push(blobUrl);
88+
89+
processedImages.push({
90+
name: images?.find((image) => image.url === originalUrl)?.name || originalUrl.split('/').pop() || blobUrl,
91+
url: blobUrl,
92+
type: blob.type,
93+
size: blob.size,
94+
});
95+
96+
// 替换 markdown 中的图片 URL
97+
// 使用正则表达式匹配 markdown 中的图片语法
98+
const escapedUrl = originalUrl.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
99+
const imgRegex = new RegExp(`!\\[.*?\\]\\(${escapedUrl}\\)`, 'g');
100+
processedMarkdownContent = processedMarkdownContent.replace(imgRegex, (match) => {
101+
return match.replace(originalUrl, blobUrl);
102+
});
103+
} catch (error) {
104+
console.error('处理图片时出错:', error);
105+
// 继续处理下一张图片
106+
setNotice(chrome.i18n.getMessage('errorProcessImage', [img.src]));
107+
setErrors((prev) => [...prev, chrome.i18n.getMessage('errorProcessImage', [img.src])]);
108+
}
107109
}
108110
}
109111

@@ -143,19 +145,29 @@ export default function Publish() {
143145

144146
const processDynamic = async (data: SyncData) => {
145147
setNotice(chrome.i18n.getMessage('processingContent'));
146-
const { images, videos } = data.data as DynamicData;
148+
const { images = [], videos = [] } = data.data as DynamicData;
147149

148150
const processedImages: FileData[] = [];
149151
const processedVideos: FileData[] = [];
150152

151-
for (const image of images) {
152-
setNotice(chrome.i18n.getMessage('errorProcessImage', [image.name]));
153-
processedImages.push(await processFile(image));
153+
// 确保 images 是可迭代的数组
154+
if (Array.isArray(images) && images.length > 0) {
155+
for (const image of images) {
156+
setNotice(chrome.i18n.getMessage('errorProcessImage', [image.name]));
157+
processedImages.push(await processFile(image));
158+
}
159+
} else {
160+
console.warn('images 不是一个数组或可迭代对象', images);
154161
}
155162

156-
for (const video of videos) {
157-
setNotice(chrome.i18n.getMessage('errorProcessFile', [video.name]));
158-
processedVideos.push(await processFile(video));
163+
// 确保 videos 是可迭代的数组
164+
if (Array.isArray(videos) && videos.length > 0) {
165+
for (const video of videos) {
166+
setNotice(chrome.i18n.getMessage('errorProcessFile', [video.name]));
167+
processedVideos.push(await processFile(video));
168+
}
169+
} else {
170+
console.warn('videos 不是一个数组或可迭代对象', videos);
159171
}
160172

161173
return {
@@ -171,6 +183,12 @@ export default function Publish() {
171183
const processPodcast = async (data: SyncData) => {
172184
setNotice(chrome.i18n.getMessage('processingContent'));
173185
const { audio } = data.data as PodcastData;
186+
187+
if (!audio) {
188+
console.warn('音频数据不存在');
189+
return data;
190+
}
191+
174192
const processedAudio = await processFile(audio);
175193
return {
176194
...data,
@@ -184,6 +202,12 @@ export default function Publish() {
184202
const processVideo = async (data: SyncData) => {
185203
setNotice(chrome.i18n.getMessage('processingContent'));
186204
const { video } = data.data as VideoData;
205+
206+
if (!video) {
207+
console.warn('视频数据不存在');
208+
return data;
209+
}
210+
187211
const processedVideo = await processFile(video);
188212
return {
189213
...data,
@@ -284,6 +308,7 @@ export default function Publish() {
284308
chrome.tabs.onUpdated.addListener(handleTabUpdated);
285309
chrome.tabs.onRemoved.addListener(handleTabRemoved);
286310
chrome.runtime.sendMessage({ action: 'MUTLIPOST_EXTENSION_PUBLISH_REQUEST_SYNC_DATA' }, async (response) => {
311+
console.log(response);
287312
const data = response.syncData as SyncData;
288313
if (!data) return setNotice(chrome.i18n.getMessage('errorGetSyncData'));
289314
setTitle(getTitleFromData(data));

0 commit comments

Comments
 (0)