refactor: externalize upload and conflict helpers
This commit is contained in:
@@ -146,6 +146,43 @@ async function fetchWithTimeout(input, init, timeoutMs, label) {
|
||||
}
|
||||
}
|
||||
|
||||
async function uploadLocalFolderAsset(file, plan, context) {
|
||||
var rootUri = String(context && context.rootUri || '').trim() || currentRootUri();
|
||||
var documentId = String(
|
||||
plan && plan.targetDocumentId
|
||||
|| context && context.documentId
|
||||
|| ''
|
||||
).trim();
|
||||
var hasFolderTarget = plan && Object.prototype.hasOwnProperty.call(plan, 'targetRelativePath');
|
||||
var uploadIntent = String(
|
||||
plan && plan.uploadIntent
|
||||
|| (hasFolderTarget ? 'filetree.folder.drop' : 'editor.markdown.attach')
|
||||
).trim();
|
||||
if (!rootUri || !uploadIntent) {
|
||||
throw new Error('本地 Markdown 上传缺少 rootUri 或 documentId');
|
||||
}
|
||||
var localForm = new FormData();
|
||||
localForm.append('file', file);
|
||||
localForm.append('rootUri', rootUri);
|
||||
localForm.append('uploadIntent', uploadIntent);
|
||||
if (documentId) localForm.append('documentId', documentId);
|
||||
if (hasFolderTarget) localForm.append('targetRelativePath', String(plan.targetRelativePath || ''));
|
||||
localForm.append('kind', file && String(file.type || '').indexOf('image/') === 0 ? 'image' : 'attachment');
|
||||
var localResponse = await fetchWithTimeout('/api/local-folder/assets/upload', {
|
||||
method: 'POST',
|
||||
credentials: 'include',
|
||||
body: localForm
|
||||
}, Number(context && context.timeoutMs) || 15000, '本地上传');
|
||||
var localPayload = await localResponse.json().catch(function() { return null; });
|
||||
if (!localResponse.ok || !localPayload || !localPayload.asset) {
|
||||
throw new Error(localPayload && localPayload.error ? localPayload.error : '上传失败');
|
||||
}
|
||||
return {
|
||||
asset: localPayload.asset,
|
||||
documentId: documentId
|
||||
};
|
||||
}
|
||||
|
||||
function localAssetOpenUrl(asset, download, context) {
|
||||
if (!isLocalUploadedAsset(asset)) return '';
|
||||
var rootUri = String(context && context.rootUri || asset && (asset.rootUri || asset.root_uri) || '').trim() || currentRootUri();
|
||||
@@ -275,6 +312,7 @@ window.__mnoteLocalUploadRuntime = {
|
||||
editorRootFromUploadOptions: editorRootFromUploadOptions,
|
||||
openEditorUploadFilePicker: openEditorUploadFilePicker,
|
||||
fetchWithTimeout: fetchWithTimeout,
|
||||
uploadLocalFolderAsset: uploadLocalFolderAsset,
|
||||
localAssetOpenUrl: localAssetOpenUrl,
|
||||
uploadedAssetType: uploadedAssetType,
|
||||
fileTreeIconKindForFileName: fileTreeIconKindForFileName,
|
||||
|
||||
Reference in New Issue
Block a user