cordis/client/webapp/elements/overlay-upload-datatransfer.ts

22 lines
983 B
TypeScript
Raw Normal View History

2021-10-30 17:26:41 +00:00
import BaseElements from './require/base-elements.js';
import { Channel, ShouldNeverHappenError } from '../data-types';
import ClientController from '../client-controller.js';
2021-10-30 17:26:41 +00:00
export default function createUploadOverlayFromDataTransferItem(document: Document, server: ClientController, channel: Channel, dataTransferItem: DataTransferItem): HTMLElement {
let file = dataTransferItem.getAsFile();
if (file === null) throw new ShouldNeverHappenError('no file in the data transfer item');
let element = BaseElements.createUploadOverlay(document, {
server: server, channel: channel, resourceName: file.name,
resourceBuffFunc: async () => {
if (file === null) throw new ShouldNeverHappenError('no file in the data transfer item');
return Buffer.from(await file.arrayBuffer());
},
resourceSizeFunc: () => {
if (file === null) throw new ShouldNeverHappenError('no file in the data transfer item');
return file.size;
}
});
return element;
2021-10-30 17:26:41 +00:00
}