diff --git a/packages/gallery/src/data/image.js b/packages/gallery/src/data/image.js index c9dad65..a77d5e0 100644 --- a/packages/gallery/src/data/image.js +++ b/packages/gallery/src/data/image.js @@ -27,6 +27,7 @@ export const removed = new Event('Image.removed'); // Watchers export const watcher = Watcher(db, SELECTOR); +export const importWatcher = Watcher(db, IMPORT_SELECTOR); // Methods const getId = id => (id.startsWith(PREFIX) ? id : `${PREFIX}_${id}`); @@ -61,9 +62,6 @@ export async function add(imageFileList) { } })); const results = await db.bulkDocs(docs); - - processImportables(); - return docs.filter((d, i) => results[i].ok); } @@ -90,77 +88,82 @@ export async function addAttachment(doc, key, blob) { } // Internal Functions -const processImportables = backgroundTask(async function _processImportables() { - const result = await db.find({ - selector: IMPORT_SELECTOR, - limit: 1 - }); - - if (!result.docs.length) { - return; - } - - const doc = result.docs[0]; - const { _id, _rev } = doc; - const imageData = await db.getAttachment(_id, 'image'); - - const ExifParser = await import('exif-parser'); - - const buffer = await blobToArrayBuffer(imageData); - const digest = await sha256(buffer); - - // Check if this image already exists - // TODO - Create an image.digest index - const digestQuery = await db.find({ - selector: { digest }, - fields: ['_id'], - limit: 1 - }); - - if (digestQuery.docs.length) { - imported.fire(digestQuery.docs[0]._id, _id, false); - } else { - const exifData = ExifParser.create(buffer).parse(); - const { tags, imageSize } = exifData; - const originalDate = new Date( - tags.DateTimeOriginal ? new Date(tags.DateTimeOriginal * 1000).toISOString() : doc.modifiedDate - ); - const id = `${PREFIX}_${originalDate.getTime().toString(36)}_${digest.substr(0, 6)}`; - - const newDoc = Object.assign( - {}, - doc, - { - _id: id, - originalDate: originalDate.toISOString(), - orientation: tags.Orientation, - digest, - make: tags.Make, - model: tags.Model, - flash: !!tags.Flash, - ISO: tags.ISO, - attachmentUrls: { - image: generateAttachmentUrl(db.name, id, 'image') - }, - gps: { - latitude: tags.GPSLatitude, - longitude: tags.GPSLongitude, - altitude: tags.GPSAltitude, - heading: tags.GPSImgDirection - } - }, - imageSize // width & height - ); - delete newDoc._rev; // assigned from doc but not desired. - - try { - await db.put(newDoc); - imported.fire(id, _id, true); - } catch (e) { - error(`Error processing Image ${id}`, e); +importWatcher( + backgroundTask(async function _processImportables(changeId, deleted) { + if (deleted) { + return; } - } + const selector = changeId ? { _id: changeId } : IMPORT_SELECTOR; + const result = await db.find({ + selector, + limit: 1 + }); - await db.remove({ _id, _rev }); - processImportables(); -}); + if (!result.docs.length) { + return; + } + + const doc = result.docs[0]; + const { _id, _rev } = doc; + const imageData = await db.getAttachment(_id, 'image'); + + const ExifParser = await import('exif-parser'); + + const buffer = await blobToArrayBuffer(imageData); + const digest = await sha256(buffer); + + // Check if this image already exists + // TODO - Create an image.digest index + const digestQuery = await db.find({ + selector: { digest }, + fields: ['_id'], + limit: 1 + }); + + if (digestQuery.docs.length) { + imported.fire(digestQuery.docs[0]._id, _id, false); + } else { + const exifData = ExifParser.create(buffer).parse(); + const { tags, imageSize } = exifData; + const originalDate = new Date( + tags.DateTimeOriginal ? new Date(tags.DateTimeOriginal * 1000).toISOString() : doc.modifiedDate + ); + const id = `${PREFIX}_${originalDate.getTime().toString(36)}_${digest.substr(0, 6)}`; + + const newDoc = Object.assign( + {}, + doc, + { + _id: id, + originalDate: originalDate.toISOString(), + orientation: tags.Orientation, + digest, + make: tags.Make, + model: tags.Model, + flash: !!tags.Flash, + ISO: tags.ISO, + attachmentUrls: { + image: generateAttachmentUrl(db.name, id, 'image') + }, + gps: { + latitude: tags.GPSLatitude, + longitude: tags.GPSLongitude, + altitude: tags.GPSAltitude, + heading: tags.GPSImgDirection + } + }, + imageSize // width & height + ); + delete newDoc._rev; // assigned from doc but not desired. + + try { + await db.put(newDoc); + imported.fire(id, _id, true); + } catch (e) { + error(`Error processing Image ${id}`, e); + } + } + + await db.remove({ _id, _rev }); + }) +);