Skip to content

Commit 0c25b9c

Browse files
committed
🐛 Fix encrypted asset external operations #18034
1 parent e34062b commit 0c25b9c

17 files changed

Lines changed: 303 additions & 43 deletions

File tree

app/src/asset/renderAssets.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import {Constants} from "../constants";
22
/// #if !MOBILE
33
import {getAllModels} from "../layout/getAll";
44
/// #endif
5-
import {pathPosix} from "../util/pathName";
5+
import {getAssetExtension} from "../util/pathName";
66
import * as dayjs from "dayjs";
77

88
export const renderAssetsPreview = (pathString: string) => {
99
if (!pathString) {
1010
return "";
1111
}
12-
const type = pathPosix().extname(pathString).toLowerCase();
12+
const type = getAssetExtension(pathString).toLowerCase();
1313
if (Constants.SIYUAN_ASSETS_IMAGE.includes(type)) {
1414
return `<img style="max-height: 100%" src="${pathString}">`;
1515
} else if (Constants.SIYUAN_ASSETS_AUDIO.includes(type)) {

app/src/editor/openLink.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {isLocalPath, pathPosix} from "../util/pathName";
1+
import {getAssetExtension, isLocalPath} from "../util/pathName";
22
/// #if !BROWSER
33
import {shell} from "electron";
44
/// #endif
@@ -32,11 +32,12 @@ export const openLink = (app: App, aLink: string, event?: MouseEvent, ctrlIsPres
3232
openByMobile(linkAddress);
3333
/// #else
3434
if (isLocalPath(linkAddress)) {
35-
if (Constants.SIYUAN_ASSETS_EXTS.includes(pathPosix().extname(linkAddress)) &&
35+
const extension = getAssetExtension(linkAddress);
36+
if (Constants.SIYUAN_ASSETS_EXTS.includes(extension) &&
3637
(
37-
!linkAddress.endsWith(".pdf") ||
38+
extension !== ".pdf" ||
3839
// 本地 pdf 仅 assets/ 开头的才使用 siyuan 打开
39-
(linkAddress.endsWith(".pdf") && linkAddress.startsWith("assets/"))
40+
(extension === ".pdf" && linkAddress.startsWith("assets/"))
4041
)
4142
) {
4243
if (event && event.altKey) {

app/src/editor/util.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {getInstanceById, getWndByLayout, newModelByInitData, pdfIsLoading, setPa
55
import {getDockByType} from "../layout/tabUtil";
66
import {getAllModels, getAllTabs} from "../layout/getAll";
77
import {highlightById, scrollCenter} from "../util/highlightById";
8-
import {getDisplayName, getDocDisplayName, isEncryptedBox, pathPosix, useShell} from "../util/pathName";
8+
import {getAssetExtension, getDisplayName, getDocDisplayName, isEncryptedBox, useShell} from "../util/pathName";
99
import {Constants} from "../constants";
1010
import {Files} from "../layout/dock/Files";
1111
import {fetchPost, fetchSyncPost} from "../util/fetch";
@@ -90,7 +90,7 @@ export const openFileById = async (options: {
9090
};
9191

9292
export const openAsset = (app: App, assetPath: string, page: number | string, position?: string) => {
93-
const suffix = pathPosix().extname(assetPath).split("?")[0];
93+
const suffix = getAssetExtension(assetPath);
9494
if (!Constants.SIYUAN_ASSETS_EXTS.includes(suffix)) {
9595
return;
9696
}
@@ -491,7 +491,7 @@ const switchEditor = (editor: Editor, options: IOpenFileOptions, allModels: IMod
491491
const newTab = (options: IOpenFileOptions) => {
492492
let tab: Tab;
493493
if (options.assetPath) {
494-
const suffix = pathPosix().extname(options.assetPath).split("?")[0];
494+
const suffix = getAssetExtension(options.assetPath);
495495
if (Constants.SIYUAN_ASSETS_EXTS.includes(suffix)) {
496496
let icon = "iconPDF";
497497
if (Constants.SIYUAN_ASSETS_IMAGE.includes(suffix)) {

app/src/menus/commonMenuItem.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {shell} from "electron";
33
/// #endif
44
import {confirmDialog} from "../dialog/confirmDialog";
55
import {getSearch, isMobile, isValidCustomAttrName} from "../util/functions";
6-
import {isEncryptedBox, isLocalPath, movePathTo, moveToPath, pathPosix} from "../util/pathName";
6+
import {getAssetExtension, isEncryptedBox, isLocalPath, movePathTo, moveToPath, pathPosix} from "../util/pathName";
77
import {MenuItem} from "./Menu";
88
import {onExport, saveExport} from "../protyle/export";
99
import {exportMarkdownZip} from "../protyle/export/exportMd";
@@ -853,9 +853,10 @@ export const openMenu = (app: App, src: string, onlyMenu: boolean, showAccelerat
853853
});
854854
/// #else
855855
if (isLocalPath(src)) {
856-
if (Constants.SIYUAN_ASSETS_EXTS.includes(pathPosix().extname(src).split("?")[0]) &&
857-
(!src.endsWith(".pdf") ||
858-
(src.endsWith(".pdf") && !src.startsWith("file://")))
856+
const extension = getAssetExtension(src);
857+
if (Constants.SIYUAN_ASSETS_EXTS.includes(extension) &&
858+
(extension !== ".pdf" ||
859+
(extension === ".pdf" && !src.startsWith("file://")))
859860
) {
860861
submenu.push({
861862
id: "insertRight",

app/src/menus/util.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {ipcRenderer} from "electron";
33
import * as path from "path";
44
/// #endif
55
import {fetchPost} from "../util/fetch";
6-
import {getAssetName, pathPosix, useShell} from "../util/pathName";
6+
import {getAssetExtension, getAssetName, useShell} from "../util/pathName";
77
import {openFileById} from "../editor/util";
88
import {Constants} from "../constants";
99
import {openNewWindowById} from "../window/openNewWindow";
@@ -26,7 +26,7 @@ export const exportAsset = (src: string) => {
2626
/// #else
2727
const result = await ipcRenderer.invoke(Constants.SIYUAN_GET, {
2828
cmd: "showSaveDialog",
29-
defaultPath: getAssetName(src) + pathPosix().extname(src),
29+
defaultPath: getAssetName(src) + getAssetExtension(src),
3030
properties: ["showOverwriteConfirmation"],
3131
});
3232
if (!result.canceled) {

app/src/protyle/hint/extend.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {hasClosestBlock, hasClosestByClassName} from "../util/hasClosest";
1111
import {getContenteditableElement, getTopAloneElement} from "../wysiwyg/getBlock";
1212
import {replaceFileName} from "../../editor/rename";
1313
import {transaction} from "../wysiwyg/transaction";
14-
import {getAssetName, getDisplayName, isEncryptedBox, pathPosix} from "../../util/pathName";
14+
import {getAssetExtension, getAssetName, getDisplayName, isEncryptedBox} from "../../util/pathName";
1515
import {genEmptyElement} from "../../block/util";
1616
import {updateListOrder} from "../wysiwyg/list";
1717
import {escapeHtml} from "../../util/escape";
@@ -604,7 +604,7 @@ export const hintRenderWidget = (value: string, protyle: IProtyle) => {
604604

605605
export const hintRenderAssets = (value: string, protyle: IProtyle) => {
606606
focusByRange(protyle.toolbar.range);
607-
const type = pathPosix().extname(value).toLowerCase();
607+
const type = getAssetExtension(value).toLowerCase();
608608
const filename = value.startsWith("assets/") ? getAssetName(value) : value;
609609
insertHTML(genAssetHTML(type, value, filename, value.startsWith("assets/") ? filename + type : value), protyle);
610610
hideElements(["util"], protyle);

app/src/protyle/render/av/asset.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {updateAttrViewCellAnimation} from "./action";
44
import {isMobile} from "../../../util/functions";
55
import {Constants} from "../../../constants";
66
import {uploadFiles} from "../../upload";
7-
import {pathPosix} from "../../../util/pathName";
7+
import {getAssetExtension, pathPosix} from "../../../util/pathName";
88
import {openMenu} from "../../../menus/commonMenuItem";
99
import {MenuItem} from "../../../menus/Menu";
1010
import {copyPNGByLink, exportAsset, writeAssetToClipboard} from "../../../menus/util";
@@ -46,7 +46,7 @@ export const bindAssetEvent = (options: {
4646
value.push({
4747
name: key,
4848
content: resData.data.succMap[key],
49-
type: Constants.SIYUAN_ASSETS_IMAGE.includes(pathPosix().extname(resData.data.succMap[key]).toLowerCase()) ? "image" : "file"
49+
type: Constants.SIYUAN_ASSETS_IMAGE.includes(getAssetExtension(resData.data.succMap[key]).toLowerCase()) ? "image" : "file"
5050
});
5151
});
5252
updateAssetCell({

app/src/protyle/render/av/cell.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {getColIconByType, getColId} from "./col";
1212
import {genAVValueHTML, getAVTemplateHTML} from "./attributeValue";
1313
import {Constants} from "../../../constants";
1414
import {hintRef} from "../../hint/extend";
15-
import {getAssetName, pathPosix} from "../../../util/pathName";
15+
import {getAssetExtension, getAssetName} from "../../../util/pathName";
1616
import {mergeAddOption} from "./select";
1717
import {escapeAriaLabel, escapeAttr, escapeHtml} from "../../../util/escape";
1818
import {electronUndo} from "../../undo";
@@ -230,7 +230,7 @@ const transformCellValue = (colType: TAVCol, value: IAVCellValue): IAVCellValue
230230
} else if (colType === "mAsset") {
231231
const content = getCellValueContent(value).toString();
232232
newValue.mAsset = [{
233-
type: Constants.SIYUAN_ASSETS_IMAGE.includes(pathPosix().extname(content).toLowerCase()) ? "image" : "file",
233+
type: Constants.SIYUAN_ASSETS_IMAGE.includes(getAssetExtension(content).toLowerCase()) ? "image" : "file",
234234
content,
235235
name: "",
236236
}];
@@ -302,7 +302,7 @@ export const genCellValue = (colType: TAVCol, value: string | any, dateFormat: T
302302
relation: {blockIDs: [value], contents: []}
303303
};
304304
} else if (colType === "mAsset") {
305-
const type = pathPosix().extname(value).toLowerCase();
305+
const type = getAssetExtension(value).toLowerCase();
306306
cellValue = {
307307
type: colType,
308308
mAsset: [{
@@ -842,7 +842,7 @@ export const updateCellsValue = async (protyle: IProtyle, nodeElement: HTMLEleme
842842
// https://github.com/siyuan-note/siyuan/issues/13892
843843
if (!link && value.startsWith("assets/")) {
844844
link = value;
845-
name = getAssetName(value) + pathPosix().extname(value);
845+
name = getAssetName(value) + getAssetExtension(value);
846846
}
847847
// https://github.com/siyuan-note/siyuan/issues/12308
848848
if (html) {

app/src/protyle/render/av/openMenuPanel.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import {updateAttrViewCellAnimation} from "./action";
3838
import {addAssetLink, bindAssetEvent, editAssetItem, getAssetHTML, updateAssetCell} from "./asset";
3939
import {Constants} from "../../../constants";
4040
import {hideElements} from "../../ui/hideElements";
41-
import {pathPosix} from "../../../util/pathName";
41+
import {getAssetExtension} from "../../../util/pathName";
4242
import {openEmojiPanel, unicode2Emoji} from "../../../emoji";
4343
import {isMobile} from "../../../util/functions";
4444
import {openLink} from "../../../editor/openLink";
@@ -1751,7 +1751,7 @@ export const openMenuPanel = (options: {
17511751
h: rect.height
17521752
}, (url, name) => {
17531753
let value: IAVCellAssetValue;
1754-
if (Constants.SIYUAN_ASSETS_IMAGE.includes(pathPosix().extname(url).toLowerCase())) {
1754+
if (Constants.SIYUAN_ASSETS_IMAGE.includes(getAssetExtension(url).toLowerCase())) {
17551755
value = {
17561756
type: "image",
17571757
content: url,

app/src/util/pathName.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,17 @@ export const getDocDisplayName = (name: string, titleEmpty?: boolean, escape?: b
154154
return displayName;
155155
};
156156

157+
export const getAssetPathWithoutQuery = (assetPath: string) => {
158+
return assetPath.split("?", 1)[0];
159+
};
160+
161+
export const getAssetExtension = (assetPath: string) => {
162+
return pathPosix().extname(getAssetPathWithoutQuery(assetPath));
163+
};
164+
157165
export const getAssetName = (assetPath: string) => {
158-
return pathPosix().basename(assetPath, pathPosix().extname(assetPath)).replace(/-\d{14}-\w{7}/, "");
166+
const pathWithoutQuery = getAssetPathWithoutQuery(assetPath);
167+
return pathPosix().basename(pathWithoutQuery, getAssetExtension(pathWithoutQuery)).replace(/-\d{14}-\w{7}/, "");
159168
};
160169

161170
export const isLocalPath = (link: string) => {

0 commit comments

Comments
 (0)