ImportWatcher -> BackgroundTask -> _processImportables
Continuing the push to remove explicit events for db changes
This commit is contained in:
parent
6f103b92bc
commit
937713de53
@ -27,6 +27,7 @@ export const removed = new Event('Image.removed');
|
|||||||
|
|
||||||
// Watchers
|
// Watchers
|
||||||
export const watcher = Watcher(db, SELECTOR);
|
export const watcher = Watcher(db, SELECTOR);
|
||||||
|
export const importWatcher = Watcher(db, IMPORT_SELECTOR);
|
||||||
|
|
||||||
// Methods
|
// Methods
|
||||||
const getId = id => (id.startsWith(PREFIX) ? id : `${PREFIX}_${id}`);
|
const getId = id => (id.startsWith(PREFIX) ? id : `${PREFIX}_${id}`);
|
||||||
@ -61,9 +62,6 @@ export async function add(imageFileList) {
|
|||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
const results = await db.bulkDocs(docs);
|
const results = await db.bulkDocs(docs);
|
||||||
|
|
||||||
processImportables();
|
|
||||||
|
|
||||||
return docs.filter((d, i) => results[i].ok);
|
return docs.filter((d, i) => results[i].ok);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,77 +88,82 @@ export async function addAttachment(doc, key, blob) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Internal Functions
|
// Internal Functions
|
||||||
const processImportables = backgroundTask(async function _processImportables() {
|
importWatcher(
|
||||||
const result = await db.find({
|
backgroundTask(async function _processImportables(changeId, deleted) {
|
||||||
selector: IMPORT_SELECTOR,
|
if (deleted) {
|
||||||
limit: 1
|
return;
|
||||||
});
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
const selector = changeId ? { _id: changeId } : IMPORT_SELECTOR;
|
||||||
|
const result = await db.find({
|
||||||
|
selector,
|
||||||
|
limit: 1
|
||||||
|
});
|
||||||
|
|
||||||
await db.remove({ _id, _rev });
|
if (!result.docs.length) {
|
||||||
processImportables();
|
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 });
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user