2022-04-12 01:09:13 +08:00
|
|
|
import logger from '/imports/startup/client/logger';
|
|
|
|
|
2023-04-25 00:56:54 +08:00
|
|
|
const MIME_TYPES_ALLOWED = ['image/png', 'image/jpeg', 'image/webp'];
|
2022-06-01 23:05:16 +08:00
|
|
|
const MAX_FILE_SIZE = 5000; // KBytes
|
|
|
|
|
2022-04-12 01:09:13 +08:00
|
|
|
const withObjectStore = ({
|
|
|
|
onError,
|
|
|
|
onSuccess,
|
|
|
|
}) => {
|
|
|
|
const request = window.indexedDB.open('BBB', 1);
|
|
|
|
|
|
|
|
request.onerror = onError;
|
|
|
|
|
|
|
|
request.onupgradeneeded = (e) => {
|
|
|
|
const db = e.target.result;
|
|
|
|
db.createObjectStore('CustomBackgrounds', { keyPath: 'uniqueId' });
|
|
|
|
};
|
|
|
|
|
|
|
|
request.onsuccess = (e) => {
|
|
|
|
const db = e.target.result;
|
|
|
|
const transaction = db.transaction(['CustomBackgrounds'], 'readwrite');
|
|
|
|
const objectStore = transaction.objectStore('CustomBackgrounds');
|
|
|
|
|
|
|
|
onSuccess(objectStore);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const genericErrorHandlerBuilder = (
|
|
|
|
code,
|
2022-07-06 21:38:40 +08:00
|
|
|
message,
|
2022-04-12 01:09:13 +08:00
|
|
|
callback,
|
|
|
|
) => (e) => {
|
|
|
|
logger.warn({
|
|
|
|
logCode: code,
|
|
|
|
extraInfo: {
|
|
|
|
errorName: e.name,
|
|
|
|
errorMessage: e.message,
|
|
|
|
},
|
2022-07-06 21:38:40 +08:00
|
|
|
}, `${message}: ${e.message}`);
|
2022-04-12 01:09:13 +08:00
|
|
|
|
|
|
|
if (callback) callback(e);
|
|
|
|
};
|
|
|
|
|
|
|
|
const load = (onError, onSuccess) => {
|
|
|
|
withObjectStore({
|
|
|
|
onError: genericErrorHandlerBuilder(
|
|
|
|
'IndexedDB_access',
|
2022-07-06 21:38:40 +08:00
|
|
|
'Error on load custom backgrounds from IndexedDB',
|
2022-04-12 01:09:13 +08:00
|
|
|
onError,
|
|
|
|
),
|
|
|
|
onSuccess: (objectStore) => {
|
|
|
|
const backgrounds = [];
|
|
|
|
|
|
|
|
objectStore.openCursor().onsuccess = (e) => {
|
|
|
|
const cursor = e.target.result;
|
|
|
|
|
|
|
|
if (cursor) {
|
|
|
|
backgrounds.push(cursor.value);
|
|
|
|
cursor.continue();
|
|
|
|
} else {
|
|
|
|
onSuccess(backgrounds);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-07-06 21:38:40 +08:00
|
|
|
const save = (background, onError) => {
|
2022-04-12 01:09:13 +08:00
|
|
|
withObjectStore({
|
|
|
|
onError: genericErrorHandlerBuilder(
|
|
|
|
'IndexedDB_access',
|
|
|
|
'Error on save custom background to IndexedDB',
|
2022-07-06 21:38:40 +08:00
|
|
|
onError,
|
2022-04-12 01:09:13 +08:00
|
|
|
),
|
|
|
|
onSuccess: (objectStore) => {
|
|
|
|
objectStore.add(background);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-07-06 21:38:40 +08:00
|
|
|
const del = (key, onError) => {
|
2022-04-12 01:09:13 +08:00
|
|
|
withObjectStore({
|
|
|
|
onError: genericErrorHandlerBuilder(
|
|
|
|
'IndexedDB_access',
|
2022-07-06 21:38:40 +08:00
|
|
|
'Error on delete custom background from IndexedDB',
|
|
|
|
onError,
|
2022-04-12 01:09:13 +08:00
|
|
|
),
|
|
|
|
onSuccess: (objectStore) => {
|
|
|
|
objectStore.delete(key);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-07-08 23:16:37 +08:00
|
|
|
const update = (background) => {
|
|
|
|
withObjectStore({
|
|
|
|
onError: genericErrorHandlerBuilder(
|
|
|
|
'IndexedDB_access',
|
|
|
|
'Error on update custom background in IndexedDB',
|
|
|
|
'Something wrong while updating custom background',
|
|
|
|
),
|
|
|
|
onSuccess: (objectStore) => {
|
|
|
|
objectStore.put(background);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2024-08-23 20:04:56 +08:00
|
|
|
// Function to convert image URL to a File object
|
|
|
|
async function getFileFromUrl(url) {
|
|
|
|
try {
|
|
|
|
const response = await fetch(url, {
|
|
|
|
credentials: 'omit',
|
|
|
|
mode: 'cors',
|
|
|
|
headers: {
|
|
|
|
Accept: 'image/*',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error(`HTTP error! status: ${response.status}`);
|
|
|
|
}
|
|
|
|
const blob = await response.blob();
|
|
|
|
const file = new File([blob], 'fetchedWebcamBackground', { type: blob.type });
|
|
|
|
return file;
|
|
|
|
} catch (error) {
|
|
|
|
logger.error({
|
|
|
|
logCode: 'image_fetch_error',
|
|
|
|
extraInfo: {
|
|
|
|
errorMessage: error?.message,
|
|
|
|
errorName: error?.name,
|
|
|
|
errorStack: error?.stack,
|
|
|
|
},
|
|
|
|
}, `Fetching image from ${url} failed.`);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-12 01:09:13 +08:00
|
|
|
export default {
|
|
|
|
load,
|
|
|
|
save,
|
|
|
|
del,
|
2022-07-08 23:16:37 +08:00
|
|
|
update,
|
2022-06-01 23:05:16 +08:00
|
|
|
MIME_TYPES_ALLOWED,
|
|
|
|
MAX_FILE_SIZE,
|
2024-08-23 20:04:56 +08:00
|
|
|
getFileFromUrl,
|
2022-04-12 01:09:13 +08:00
|
|
|
};
|