2022-04-12 01:09:13 +08:00
|
|
|
import logger from '/imports/startup/client/logger';
|
|
|
|
|
2022-06-01 23:05:16 +08:00
|
|
|
const MIME_TYPES_ALLOWED = ['image/png', 'image/jpeg'];
|
|
|
|
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);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
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,
|
2022-04-12 01:09:13 +08:00
|
|
|
};
|