import { createElement, setTooltip, showAlert, createPicture, createAudio, createVideo, createFile } from "../../ui"; import { r } from "../../utility"; export function createBox(title, functions) { const container = createElement('div', 'comm'); const header = createElement('div', 'title-bar', title, createElement('div', 'title-functions', ...functions) ); container.appendChild(header); return container; }; export function appendMedia(container, mimeType, url) { switch (mimeType) { case 'application/pdf': container.appendChild(createFile(url, 'file-pdf')); break; case 'application/smil': // TODO: ignore smil files // container.appendChild(createFile(url, 'smile')); break; case 'audio/amr': case 'audio/mp3': case 'audio/mpeg': case 'audio/x-mpeg': case 'audio/aac': case 'audio/ogg': case 'audio/opus': case 'audio/wav': case 'audio/webm': container.appendChild(createAudio(mimeType, url)); break; case 'image/gif': case 'image/jpeg': case 'image/png': container.appendChild(createPicture(url)); break; case 'text/x-vcard': container.appendChild(createFile(url, 'id-card')); break; case 'video/mp4': case 'video/mpeg': case 'video/x-mpeg': case 'video/mp2t': case 'video/webm': case 'video/quicktime': container.appendChild(createVideo(url)); break; default: if (/^audio\//.test(mimeType)) { container.appendChild(createFile(url, 'music')); } else if (/^video\//.test(mimeType)) { container.appendChild(createFile(url, 'video')); } else { container.appendChild(createFile(url)); } break; } return container; }; const MaxAttachmentSize = { limit: 2 * 1024 * 1024, text: '2MB' }; export const fileSupported = [ 'application/pdf', 'audio/mpeg', 'audio/x-mpeg', 'audio/amr', '.amr', 'image/bmp', 'image/gif', 'image/jpeg', 'image/png', 'image/tiff', 'text/plain', 'text/x-vcard', 'video/3gpp', 'video/mp4', 'video/webm', 'video/quicktime' ]; export function insertFile(container, file) { const label = container.querySelector('.file-selector>.selector-name'); if (label != null && file != null) { let type = file.type; if (type == null || type.length === 0) { type = file.name; type = type.substring(type.lastIndexOf('.')); } if (fileSupported.indexOf(type) < 0) { showAlert(r('error', 'Error'), r('notSupported', 'File type "{type}" is now not supported.').replace('{type}', type)); return; } const isImage = /^image\//.test(type); if (!isImage && file.size > MaxAttachmentSize.limit) { showAlert(r('error', 'Error'), r('fileTooLarge', `File is too large. (> ${MaxAttachmentSize.text})`), 'warn'); return; } const fn = file.name; label.style.display = ''; label.innerText = fn; if (isImage) { const img = new Image(); const reader = new FileReader(); reader.onload = e => { img.src = e.target.result; setTooltip(label, img); }; reader.onerror = () => setTooltip(label, fn); reader.readAsDataURL(file); // img.src = URL.createObjectURL(file); // setTooltip(label, img); } else { setTooltip(label, fn); } return file; } };