fix: open local uploaded resources
This commit is contained in:
@@ -2137,7 +2137,7 @@ const SIDEBAR_TREE_JS: &str = r##"
|
||||
|
||||
function localFilePathFromAssetId(assetId) {
|
||||
var value = String(assetId || '').trim();
|
||||
return value.indexOf('local-file:') === 0 ? value.slice('local-file:'.length) : '';
|
||||
return value.indexOf('local-file:') === 0 ? value.slice('local-file:'.length) : value.indexOf('local:asset:') === 0 ? value.slice('local:asset:'.length) : '';
|
||||
}
|
||||
|
||||
function buildLocalFileOpenUrl(relativePath, download) {
|
||||
@@ -2350,6 +2350,16 @@ const SIDEBAR_TREE_JS: &str = r##"
|
||||
return String(asset && (asset.asset_type || asset.assetType || asset.mime_type || '') || '').trim();
|
||||
}
|
||||
|
||||
function fileTreeIconKindForFileName(fileName) {
|
||||
var name = String(fileName || '').trim().toLowerCase();
|
||||
var ext = name.indexOf('.') >= 0 ? name.split('.').pop() : '';
|
||||
if (['doc', 'docx', 'odt', 'rtf', 'ppt', 'pptx', 'odp', 'xls', 'xlsx', 'ods', 'csv'].indexOf(ext) >= 0) return 'office';
|
||||
if (ext === 'md' || ext === 'markdown') return 'markdown';
|
||||
if (ext === 'pdf') return 'pdf';
|
||||
if (['png', 'jpg', 'jpeg', 'gif', 'webp', 'svg', 'bmp'].indexOf(ext) >= 0) return 'image';
|
||||
return '';
|
||||
}
|
||||
|
||||
function isLocalUploadedAsset(asset) {
|
||||
var id = String(asset && asset.id || '').trim();
|
||||
return String(asset && asset.sourceKind || '').trim() === 'local_folder' || id.indexOf('local:asset:') === 0;
|
||||
@@ -2688,7 +2698,7 @@ const SIDEBAR_TREE_JS: &str = r##"
|
||||
};
|
||||
li.setAttribute('data-node-id', 'asset:' + assetId);
|
||||
var title = uploadedAssetTitle(asset);
|
||||
var iconKind = uploadedAssetType(asset) || 'file';
|
||||
var iconKind = fileTreeIconKindForFileName(title) || uploadedAssetType(asset) || 'file';
|
||||
if (objectKind === 'mindmap') iconKind = 'mindmap';
|
||||
li.innerHTML =
|
||||
'<div class="tree-row" role="treeitem" aria-level="' + (parentRow ? '2' : '1') + '" aria-expanded="false" data-rust-rendered-row="filetree" data-testid="filetree-asset-row" data-row-id="' + escapeHtml('asset:' + assetId) + '" data-row-kind="asset" data-node-id="' + escapeHtml('asset:' + assetId) + '" data-document-id="' + escapeHtml(targetDocumentId) + '" data-doc-id="' + escapeHtml(targetDocumentId) + '" data-asset-id="' + escapeHtml(assetId) + '" data-object-identity="' + escapeHtml(objectIdentityAttr(objectIdentity)) + '" data-object-kind="' + escapeHtml(objectKind || 'attachment') + '" data-shell-mode="filetree" data-selected="false" data-active="false" draggable="true">' +
|
||||
@@ -7221,6 +7231,19 @@ const SIDEBAR_TREE_JS: &str = r##"
|
||||
|
||||
async function resolveEditorAttachmentUrl(detail) {
|
||||
var assetId = String(detail && detail.assetId || '').trim();
|
||||
var localFilePath = localFilePathFromAssetId(assetId);
|
||||
if (localFilePath) {
|
||||
var localUrl = buildLocalFileOpenUrl(localFilePath, false);
|
||||
if (localUrl) {
|
||||
return {
|
||||
url: localUrl,
|
||||
asset: {
|
||||
file_name: String(detail && detail.fileName || '').trim(),
|
||||
fileSize: String(detail && detail.fileSize || '').trim()
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
if (assetId) {
|
||||
var response = await fetch('/api/media/sign?assetId=' + encodeURIComponent(assetId), {
|
||||
method: 'GET',
|
||||
@@ -7303,6 +7326,14 @@ const SIDEBAR_TREE_JS: &str = r##"
|
||||
|
||||
async function openEditorAttachmentDownload(detail) {
|
||||
if (!detail) return;
|
||||
var localFilePath = localFilePathFromAssetId(detail.assetId);
|
||||
if (localFilePath) {
|
||||
var localDownloadUrl = buildLocalFileOpenUrl(localFilePath, true);
|
||||
if (localDownloadUrl) {
|
||||
window.open(localDownloadUrl, '_blank', 'noopener,noreferrer');
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (detail.assetId) {
|
||||
try {
|
||||
var response = await fetch('/api/media/sign?assetId=' + encodeURIComponent(detail.assetId), {
|
||||
@@ -8740,6 +8771,8 @@ mod tests {
|
||||
assert!(SIDEBAR_TREE_JS.contains("function localFilePathFromAssetId"));
|
||||
assert!(SIDEBAR_TREE_JS.contains("/api/local-folder/files/open"));
|
||||
assert!(SIDEBAR_TREE_JS.contains("local-file:"));
|
||||
assert!(SIDEBAR_TREE_JS
|
||||
.contains("value.indexOf('local:asset:') === 0 ? value.slice('local:asset:'.length)"));
|
||||
assert!(SIDEBAR_TREE_JS.contains("fetchCurrentOnlyOfficeUserId"));
|
||||
assert!(SIDEBAR_TREE_JS.contains("/api/auth/whoami"));
|
||||
assert!(SIDEBAR_TREE_JS.contains("buildOnlyOfficeOpenUrl"));
|
||||
@@ -8908,6 +8941,7 @@ mod tests {
|
||||
assert!(SIDEBAR_TREE_JS.contains("isLocalUploadedAsset(asset)"));
|
||||
assert!(SIDEBAR_TREE_JS.contains("buildLocalOnlyOfficeOpenUrl"));
|
||||
assert!(SIDEBAR_TREE_JS.contains("localOfficeUrl"));
|
||||
assert!(SIDEBAR_TREE_JS.contains("fileTreeIconKindForFileName(title)"));
|
||||
assert!(SIDEBAR_TREE_JS.contains("refreshLocalFolderSidebarSnapshot"));
|
||||
assert!(SIDEBAR_TREE_JS.contains("wolai:local-assets-changed"));
|
||||
}
|
||||
@@ -9051,6 +9085,9 @@ mod tests {
|
||||
assert!(SIDEBAR_TREE_JS.contains("function openPdfEditorAttachment"));
|
||||
assert!(SIDEBAR_TREE_JS.contains("function openCodeEditorAttachment"));
|
||||
assert!(SIDEBAR_TREE_JS.contains("function resolveEditorAttachmentUrl"));
|
||||
assert!(SIDEBAR_TREE_JS.contains("var localFilePath = localFilePathFromAssetId(assetId)"));
|
||||
assert!(SIDEBAR_TREE_JS
|
||||
.contains("var localDownloadUrl = buildLocalFileOpenUrl(localFilePath, true)"));
|
||||
assert!(SIDEBAR_TREE_JS.contains("type: 'codeBlock'"));
|
||||
assert!(SIDEBAR_TREE_JS.contains("attrs: { language: language }"));
|
||||
assert!(SIDEBAR_TREE_JS.contains("inferCodeAttachmentLanguage"));
|
||||
|
||||
Reference in New Issue
Block a user