mirror of
https://github.com/pieartsy/PluralFlux.git
synced 2026-04-17 04:05:29 +10:00
Tests are mostly complete (though some are failing -- to be fixed in the async/await refactor. Also refactored, fixed bugs found while testing, and added ability to type `pf;m` instead of `pf;member`. --------- Co-authored-by: Aster Fialla <asterfialla@gmail.com>
30 lines
956 B
JavaScript
30 lines
956 B
JavaScript
import {enums} from '../enums.js'
|
|
|
|
const u = {};
|
|
|
|
u.debounce = function(func, delay) {
|
|
let timeout = null;
|
|
return function (...args) {
|
|
clearTimeout(timeout);
|
|
timeout = setTimeout(() => func(...args), delay);
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Checks if an uploaded picture is in the right format.
|
|
*
|
|
* @async
|
|
* @param {string} imageUrl - The url of the image
|
|
* @throws {Error} When loading the profile picture from a URL doesn't work, or it fails requirements.
|
|
*/
|
|
u.checkImageFormatValidity = async function (imageUrl) {
|
|
const acceptableImages = ['image/png', 'image/jpg', 'image/jpeg', 'image/webp'];
|
|
await fetch(imageUrl).then(r => r.blob()).then(blobFile => {
|
|
if (blobFile.size > 1000000 || !acceptableImages.includes(blobFile.type)) throw new Error(enums.err.PROPIC_FAILS_REQUIREMENTS);
|
|
}).catch((error) => {
|
|
throw new Error(`${enums.err.PROPIC_CANNOT_LOAD}: ${error.message}`);
|
|
});
|
|
}
|
|
|
|
export const utils = u;
|