Advertisement
Guest User

image_idb.js

a guest
Sep 18th, 2022
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 3.53 KB | Source Code | 0 0
  1. // image_idb.js
  2. import { openDB/*, deleteDB, wrap, unwrap*/ } from 'idb@7.min';
  3. export const dbName = 'YourIDBName';
  4. export const dbVersion = 1;
  5. export const storeName = {images: 'images'};
  6.  
  7. export const localImageCheck = (
  8.   dbName,
  9.   storeName,
  10.   id,
  11.   el,
  12.   onNoIDB = (el, url) => {},
  13.   onUndefined = (request) => {},
  14.   onOutdated = (request) => {},
  15.   onDefined = (request) => {}
  16. ) => {
  17.   let url = $(el).data('image-src');
  18.   //console.log(url);
  19.  
  20.   // Check for image blob in a local indexedDB
  21.   if(url !== undefined) {
  22.     if(!('indexedDB' in window)) {
  23.       // If browser doesn't support indexedDB. Needs additional logic added to alert end user of
  24.       // data consuption and alternative methods.
  25.       onNoIDB(el, url);
  26.     }
  27.     else {
  28.       //console.log({dbName: dbName, dbVersion: dbVersion});
  29.       openDB(dbName, dbVersion, {
  30.         upgrade(db) {
  31.           db.createObjectStore(storeName, {keyPath: 'id'});
  32.         },
  33.       }).then(
  34.         (db) => {
  35.           //console.log(db);
  36.           let transaction = db.transaction(storeName);
  37.           //console.log(transaction);
  38.           transaction.store.get(id).then(
  39.             (result) => {
  40.               //console.log(result);
  41.               if(result == undefined) onUndefined(el, result);
  42.               else if(result.url != url) onOutdated(el, result);
  43.               else onDefined(el, result.blob);
  44.             },
  45.             (error) => {
  46.               console.error('IDB.store.get():', error);
  47.             }
  48.           );
  49.         },
  50.         (error) => {
  51.           console.error('IDB.openDB():', error);
  52.           onNoIDB(el, url);
  53.         }
  54.       )
  55.     }
  56.   }
  57.   else onUndefined();
  58. }
  59.  
  60. export const localImageStore = (dbName, storeName, id, el, method, localImagePut = (el, blob, pulled) => {}) => {
  61.   let url = $(el).data('image-src');
  62.   //console.log(url);
  63.  
  64.   if(url !== undefined) {
  65.     let xhr = new XMLHttpRequest();
  66.     xhr.responseType = 'blob';
  67.     xhr.open('GET', url, true);
  68.  
  69.     xhr.addEventListener("load", () => {
  70.       if (xhr.readyState == 4 && xhr.status === 200) {
  71.         //console.log(xhr);
  72.         // Put/get image blobs in a local indexedDB
  73.         openDB(dbName, 1, {
  74.           upgrade(db) {
  75.             db.createObjectStore(storeName, {keyPath: 'id'});
  76.           },
  77.         }).then(
  78.           (db) => {
  79.             let transaction = db.transaction(storeName, 'readwrite');
  80.  
  81.             let image = {
  82.               id: id,
  83.               url: url,
  84.               blob: xhr.response
  85.             }
  86.  
  87.             if(method == 'ADD') {
  88.               //console.log(`ADD: ${image}`);
  89.  
  90.               let objStore = transaction.store.add(image).then(
  91.                 (key) => {
  92.                   localImagePut(el, xhr.response, true);
  93.                 },
  94.                 (error) => {
  95.                   console.error('IDB.store.add():', error);
  96.                 }
  97.               );
  98.             }
  99.             else if(method == 'PUT') {
  100.               //console.log(`PUT: ${image}`);
  101.  
  102.               let objStore = transaction.store.put(image).then(
  103.                 (key) => {
  104.                   localImagePut(el, xhr.response, true);
  105.                 },
  106.                 (error) => {
  107.                   console.error('IDB.store.put():', error);
  108.                 }
  109.               );
  110.             }
  111.           },
  112.           (error) => {
  113.             console.error('IDB.openDB():', error);
  114.           }
  115.         );
  116.       }
  117.       else {
  118.         console.log("Error", xmlhttp.statusText);
  119.      }
  120.     }, false);
  121.  
  122.     xhr.send();
  123.   }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement