2022-03-28 13:30:27 -07:00
|
|
|
const truncateFilename = (url: string, maxLength: number) => {
|
2020-09-06 14:44:19 -07:00
|
|
|
const filename = url.split('/').pop();
|
|
|
|
|
2022-03-28 13:30:27 -07:00
|
|
|
if (!filename) {
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
2020-09-06 14:44:19 -07:00
|
|
|
if (filename.length <= maxLength) return filename;
|
|
|
|
|
|
|
|
return [
|
2022-05-11 10:40:34 -07:00
|
|
|
filename.substr(0, maxLength / 2),
|
|
|
|
filename.substr(filename.length - maxLength / 2),
|
2020-09-06 14:44:19 -07:00
|
|
|
].join('…');
|
|
|
|
};
|
2022-03-28 13:30:27 -07:00
|
|
|
|
|
|
|
const formatBytes = (bytes: number, decimals: number = 2) => {
|
|
|
|
if (bytes === 0) return '0 Bytes';
|
|
|
|
|
|
|
|
const k = 1024;
|
|
|
|
const dm = decimals < 0 ? 0 : decimals;
|
|
|
|
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
|
|
|
|
|
|
|
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
|
|
|
|
|
|
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
|
|
|
|
};
|
|
|
|
|
2022-06-22 12:40:26 -07:00
|
|
|
const getVideoDuration = (file: File): Promise<number> => {
|
|
|
|
const video = document.createElement('video');
|
|
|
|
|
|
|
|
const promise = new Promise<number>((resolve, reject) => {
|
|
|
|
video.addEventListener('loadedmetadata', () => {
|
|
|
|
// Chrome bug: https://bugs.chromium.org/p/chromium/issues/detail?id=642012
|
|
|
|
if (video.duration === Infinity) {
|
|
|
|
video.currentTime = Number.MAX_SAFE_INTEGER;
|
|
|
|
video.ontimeupdate = () => {
|
|
|
|
video.ontimeupdate = null;
|
|
|
|
resolve(video.duration);
|
|
|
|
video.currentTime = 0;
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
resolve(video.duration);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
video.onerror = (event: any) => reject(event.target.error);
|
|
|
|
});
|
|
|
|
|
|
|
|
video.src = window.URL.createObjectURL(file);
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
};
|
|
|
|
|
2022-11-11 06:57:39 -08:00
|
|
|
const domParser = new DOMParser();
|
|
|
|
|
2022-11-14 08:50:21 -08:00
|
|
|
enum VideoProviders {
|
|
|
|
RUMBLE = 'rumble.com'
|
|
|
|
}
|
|
|
|
|
2022-11-11 06:57:39 -08:00
|
|
|
const addAutoPlay = (html: string): string => {
|
|
|
|
const document = domParser.parseFromString(html, 'text/html').documentElement;
|
|
|
|
const iframe = document.querySelector('iframe');
|
|
|
|
|
|
|
|
if (iframe) {
|
2022-11-14 08:50:21 -08:00
|
|
|
const url = new URL(iframe.src);
|
|
|
|
const provider = new URL(iframe.src).host;
|
2022-11-11 06:57:39 -08:00
|
|
|
|
2022-11-14 08:50:21 -08:00
|
|
|
if (provider === VideoProviders.RUMBLE) {
|
|
|
|
url.searchParams.append('pub', '7a20');
|
|
|
|
url.searchParams.append('autoplay', '2');
|
2022-11-11 06:57:39 -08:00
|
|
|
} else {
|
2022-11-14 08:50:21 -08:00
|
|
|
url.searchParams.append('autoplay', '1');
|
|
|
|
url.searchParams.append('auto_play', '1');
|
2022-11-11 06:57:39 -08:00
|
|
|
iframe.allow = 'autoplay';
|
|
|
|
}
|
|
|
|
|
2022-11-14 08:50:21 -08:00
|
|
|
iframe.src = url.toString();
|
|
|
|
|
2022-11-11 06:57:39 -08:00
|
|
|
// DOM parser creates html/body elements around original HTML fragment,
|
|
|
|
// so we need to get innerHTML out of the body and not the entire document
|
|
|
|
return (document.querySelector('body') as HTMLBodyElement).innerHTML;
|
|
|
|
}
|
|
|
|
|
|
|
|
return html;
|
|
|
|
};
|
|
|
|
|
|
|
|
export { getVideoDuration, formatBytes, truncateFilename, addAutoPlay };
|