cordis/client/webapp/elements/overlay-upload-datatransfer.ts
2021-10-30 12:26:41 -05:00

34 lines
1.3 KiB
TypeScript

import { Channel } from '../data-types';
import ClientController from '../client-controller.js';
import BaseElements from './require/base-elements.js';
import IState from './require/elements-state.js';
import { $, $$, $$$, $$$$ } from './require/q-module';
class ShouldNeverHappenError extends Error {
constructor(...args: any[]) {
super(...args);
this.name = 'ShouldNeverHappenError';
}
}
export default function createUploadOverlayFromDataTransferItem(state: IState, server: ClientController, channel: Channel, dataTransferItem: DataTransferItem): HTMLElement {
const { document } = state;
$.setDocument(document);
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;
}