Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Harden MP3 recording upload recovery https://github.com/siyuan-note/siyuan/issues/11727

Daniel2b39c5025ae7
diff --git a/app/THIRD_PARTY_NOTICES.md b/app/THIRD_PARTY_NOTICES.md
new file mode 100644
index 0000000000..015d0d7e6b
--- /dev/null
+++ b/app/THIRD_PARTY_NOTICES.md
@@ -0,0 +1,11 @@
+# Third-party notices
+
+## @breezystack/lamejs
+
+SiYuan uses `@breezystack/lamejs` to encode microphone recordings as MP3. The library is distributed under the GNU Lesser General Public License version 3.0.
+
+Source code: https://github.com/shijinyu/lamejs
+
+License: https://www.gnu.org/licenses/lgpl-3.0.html
+
+SiYuan does not modify the library source code.
diff --git a/app/src/protyle/breadcrumb/index.ts b/app/src/protyle/breadcrumb/index.ts
index 61debc6dc3..57cb5e75b0 100644
--- a/app/src/protyle/breadcrumb/index.ts
+++ b/app/src/protyle/breadcrumb/index.ts
@@ -36,6 +36,7 @@ import {improveBreadcrumbAppearance} from "../wysiwyg/renderBacklink";
import {getCloudURL} from "../../config/util/about";
import {escapeAriaLabel} from "../../util/escape";
import {refreshUndoButtons} from "../undo/globalUndo";
+import {getAllEditor} from "../../layout/getAll";
export class Breadcrumb {
public element: HTMLElement;
@@ -44,6 +45,7 @@ export class Breadcrumb {
private messageId: string;
private recordUploadMessageIds = new Map<File, string>();
private pendingRecordFiles = new Set<File>();
+ private uploadingRecordFiles = new Set<File>();
private startingRecord = false;
private stoppingRecord = false;
private previousFocusElement: HTMLElement;
@@ -493,7 +495,7 @@ ${padHTML}
const blob = await recorder.stopRecording();
const file = new File([blob], `record${Date.now()}.mp3`, {type: "audio/mpeg"});
this.pendingRecordFiles.add(file);
- this.uploadRecord(protyle, file);
+ this.uploadRecord(protyle, file, protyle.block?.rootID);
} catch (error) {
showMessage(window.siyuan.languages["record-tip"]);
} finally {
@@ -505,27 +507,57 @@ ${padHTML}
}
}
- private uploadRecord(protyle: IProtyle, file: File) {
- if (!this.pendingRecordFiles.has(file)) {
+ private uploadRecord(protyle: IProtyle, file: File, rootID: string) {
+ if (!this.pendingRecordFiles.has(file) || this.uploadingRecordFiles.has(file)) {
return;
}
hideMessage(this.recordUploadMessageIds.get(file));
this.recordUploadMessageIds.delete(file);
- uploadFiles(protyle, [file], undefined, undefined, (succeeded) => {
- if (!this.pendingRecordFiles.has(file)) {
- return;
- }
- if (succeeded) {
- this.pendingRecordFiles.delete(file);
- return;
- }
- const messageId = showMessage(`<div class="fn__flex fn__flex-wrap">
+ const uploadProtyle = this.findRecordUploadProtyle(protyle, rootID);
+ if (!uploadProtyle) {
+ this.showRecordUploadRetry(protyle, file, rootID);
+ return;
+ }
+
+ this.uploadingRecordFiles.add(file);
+ try {
+ uploadFiles(uploadProtyle, [file], undefined, undefined, (succeeded) => {
+ this.uploadingRecordFiles.delete(file);
+ if (!this.pendingRecordFiles.has(file)) {
+ return;
+ }
+ if (succeeded) {
+ this.pendingRecordFiles.delete(file);
+ return;
+ }
+ this.showRecordUploadRetry(uploadProtyle, file, rootID);
+ });
+ } catch (error) {
+ this.uploadingRecordFiles.delete(file);
+ this.showRecordUploadRetry(uploadProtyle, file, rootID);
+ }
+ }
+
+ private findRecordUploadProtyle(protyle: IProtyle, rootID: string) {
+ if (document.body.contains(protyle.element) && (!rootID || protyle.block?.rootID === rootID)) {
+ return protyle;
+ }
+ return getAllEditor().find((editor) => {
+ return document.body.contains(editor.protyle.element) &&
+ (!rootID || editor.protyle.block?.rootID === rootID);
+ })?.protyle;
+ }
+
+ private showRecordUploadRetry(protyle: IProtyle, file: File, rootID: string) {
+ if (!this.pendingRecordFiles.has(file)) {
+ return;
+ }
+ const messageId = showMessage(`<div class="fn__flex fn__flex-wrap">
<span class="fn__flex-center">${window.siyuan.languages.uploadError}</span><span class="fn__space"></span>
<button class="b3-button b3-button--white">${window.siyuan.languages.retry}</button></div>`, -1);
- this.recordUploadMessageIds.set(file, messageId);
- document.querySelector(`#message [data-id="${messageId}"] button`)?.addEventListener("click", () => {
- this.uploadRecord(protyle, file);
- });
+ this.recordUploadMessageIds.set(file, messageId);
+ document.querySelector(`#message [data-id="${messageId}"] button`)?.addEventListener("click", () => {
+ this.uploadRecord(protyle, file, rootID);
});
}
diff --git a/app/src/protyle/upload/index.ts b/app/src/protyle/upload/index.ts
index c74d82a450..1b63978fbb 100644
--- a/app/src/protyle/upload/index.ts
+++ b/app/src/protyle/upload/index.ts
@@ -344,6 +344,7 @@ export const uploadFiles = (protyle: IProtyle, files: FileList | DataTransferIte
const isValidate = protyle.options.upload.validate(fileList);
if (typeof isValidate === "string") {
showMessage(isValidate);
+ completeCB?.(false);
return;
}
}
diff --git a/app/webpack.config.js b/app/webpack.config.js
index d8211c8091..bd0653ecef 100644
--- a/app/webpack.config.js
+++ b/app/webpack.config.js
@@ -38,6 +38,14 @@ module.exports = (env, argv) => {
chunks: "all",
minSize: 20000,
cacheGroups: {
+ // MP3 编码器仅由录音 Worker 使用,单独分包可避免主界面提前加载。
+ recordMediaEncoder: {
+ test: /[\\/]node_modules[\\/]@breezystack[\\/]lamejs[\\/]/,
+ name: "record-media-encoder",
+ chunks: "all",
+ priority: 20,
+ enforce: true,
+ },
// 第三方依赖统一进 vendors chunk(dayjs、iconv-lite、@tiptap/* 等)
vendors: {
test: /[\\/]node_modules[\\/]/,