diff --git a/plugins/attachment-resources/src/components/AddAttachment.svelte b/plugins/attachment-resources/src/components/AddAttachment.svelte index 979b1e70fa..34495a9adf 100644 --- a/plugins/attachment-resources/src/components/AddAttachment.svelte +++ b/plugins/attachment-resources/src/components/AddAttachment.svelte @@ -2,7 +2,7 @@ import { Class, Doc, Ref, Space } from '@anticrm/core' import { getClient } from '@anticrm/presentation' import { CircleButton, IconAdd } from '@anticrm/ui' - import { createAttachment } from '../utils' + import { createAttachments } from '../utils' export let loading: number = 0 export let inputFile: HTMLInputElement @@ -10,16 +10,18 @@ export let objectClass: Ref> export let objectId: Ref export let space: Ref - const client = getClient() - function fileSelected() { + async function fileSelected() { const list = inputFile.files if (list === null || list.length === 0) return - for (let index = 0; index < list.length; index++) { - const file = list.item(index) - if (file !== null) createAttachment(client, file, loading, { objectClass, objectId, space }) + + loading++ + try { + await createAttachments(client, list, {objectClass, objectId, space}) + } finally { + loading-- } } @@ -31,13 +33,9 @@
{#if $$slots.control} - + {:else} - + {/if} > export let objectId: Ref export let space: Ref export let canDrop: ((e: DragEvent) => boolean) | undefined = undefined - + export let dragover = false const client = getClient() - - function fileDrop(e: DragEvent) { + + async function fileDrop(e: DragEvent) { dragover = false const list = e.dataTransfer?.files if (list === undefined || list.length === 0) return - for (let index = 0; index < list.length; index++) { - const file = list.item(index) - if (file !== null) createAttachment(client, file, loading, { objectClass, objectId, space }) + + loading++ + try { + await createAttachments(client, list, {objectClass, objectId, space}) + } finally { + loading-- } } diff --git a/plugins/attachment-resources/src/utils.ts b/plugins/attachment-resources/src/utils.ts index 5019e1f8e7..bcc700d847 100644 --- a/plugins/attachment-resources/src/utils.ts +++ b/plugins/attachment-resources/src/utils.ts @@ -14,13 +14,13 @@ // limitations under the License. // -import type { Class, Doc, Ref, Space, TxOperations as Client } from '@anticrm/core' +import type { Class, Doc, Ref, Space, TxOperations as Client } from '@anticrm/core' import login from '@anticrm/login' import { getMetadata, setPlatformStatus, unknownError } from '@anticrm/platform' import attachment from './plugin' -export async function uploadFile (file: File, opts?: { space: Ref, attachedTo: Ref }): Promise { +export async function uploadFile(file: File, opts?: { space: Ref; attachedTo: Ref }): Promise { const uploadUrl = getMetadata(login.metadata.UploadUrl) if (uploadUrl === undefined) { @@ -30,12 +30,16 @@ export async function uploadFile (file: File, opts?: { space: Ref, attach const data = new FormData() data.append('file', file) - const params = opts !== undefined - ? [['space', opts.space], ['attachedTo', opts.attachedTo]] - .filter((x): x is [string, Ref] => x[1] !== undefined) - .map(([name, value]) => `${name}=${value}`) - .join('&') - : '' + const params = + opts !== undefined + ? [ + ['space', opts.space], + ['attachedTo', opts.attachedTo] + ] + .filter((x): x is [string, Ref] => x[1] !== undefined) + .map(([name, value]) => `${name}=${value}`) + .join('&') + : '' const suffix = params === '' ? params : `?${params}` const url = `${uploadUrl}${suffix}` @@ -54,7 +58,7 @@ export async function uploadFile (file: File, opts?: { space: Ref, attach return await resp.text() } -export async function deleteFile (id: string): Promise { +export async function deleteFile(id: string): Promise { const uploadUrl = getMetadata(login.metadata.UploadUrl) const url = `${uploadUrl as string}?file=${id}` @@ -70,27 +74,33 @@ export async function deleteFile (id: string): Promise { } } -export async function createAttachment (client: Client, file: File, loading: number, attachTo: {objectClass: Ref>, space: Ref, objectId: Ref}) { - loading++ - const {objectClass, objectId, space} = attachTo +export async function createAttachments( + client: Client, + list: FileList, + attachTo: { objectClass: Ref>; space: Ref; objectId: Ref } +) { + const { objectClass, objectId, space } = attachTo try { - const uuid = await uploadFile(file, { space, attachedTo: objectId }) - console.log('uploaded file uuid', uuid) - client.addCollection(attachment.class.Attachment, space, objectId, objectClass, 'attachments', { - name: file.name, - file: uuid, - type: file.type, - size: file.size, - lastModified: file.lastModified - }) + for (let index = 0; index < list.length; index++) { + const file = list.item(index) + if (file !== null) { + const uuid = await uploadFile(file, { space, attachedTo: objectId }) + console.log('uploaded file uuid', uuid) + client.addCollection(attachment.class.Attachment, space, objectId, objectClass, 'attachments', { + name: file.name, + file: uuid, + type: file.type, + size: file.size, + lastModified: file.lastModified + }) + } + } } catch (err: any) { setPlatformStatus(unknownError(err)) - } finally { - loading-- } } -export function getType (type: string): 'image' | 'video' | 'audio' | 'pdf' | 'other' { +export function getType(type: string): 'image' | 'video' | 'audio' | 'pdf' | 'other' { if (type.startsWith('image/')) { return 'image' }