Use MangoQueries for Image import
This commit is contained in:
parent
e5f8335bf4
commit
99b8b9a636
@ -75,43 +75,47 @@ export async function addAttachment(doc, key, blob) {
|
|||||||
|
|
||||||
// Internal Functions
|
// Internal Functions
|
||||||
const processImportables = backgroundTask(async function _processImportables() {
|
const processImportables = backgroundTask(async function _processImportables() {
|
||||||
const result = await db.allDocs({
|
const result = await db.find({
|
||||||
startkey: `${PROCESS_PREFIX}_`,
|
selector: {
|
||||||
endkey: `${PROCESS_PREFIX}_\ufff0`,
|
_id: {
|
||||||
include_docs: true,
|
$gt: `${PROCESS_PREFIX}_`,
|
||||||
attachments: true,
|
$lt: `${PROCESS_PREFIX}_\ufff0`
|
||||||
binary: true,
|
}
|
||||||
|
},
|
||||||
limit: 1
|
limit: 1
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!result.rows.length) {
|
if (!result.docs.length) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const doc = result.docs[0];
|
||||||
|
const { _id, _rev } = doc;
|
||||||
|
const imageData = await db.getAttachment(_id, 'image');
|
||||||
|
|
||||||
const ExifParser = await import('exif-parser');
|
const ExifParser = await import('exif-parser');
|
||||||
|
|
||||||
const doc = result.rows[0].doc;
|
const buffer = await blobToArrayBuffer(imageData);
|
||||||
const buffer = await blobToArrayBuffer(doc._attachments.image.data);
|
|
||||||
const digest = await sha256(buffer);
|
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 exifData = ExifParser.create(buffer).parse();
|
||||||
const { tags, imageSize } = exifData;
|
const { tags, imageSize } = exifData;
|
||||||
const originalDate = new Date(
|
const originalDate = new Date(
|
||||||
tags.DateTimeOriginal ? new Date(tags.DateTimeOriginal * 1000).toISOString() : doc.modifiedDate
|
tags.DateTimeOriginal ? new Date(tags.DateTimeOriginal * 1000).toISOString() : doc.modifiedDate
|
||||||
);
|
);
|
||||||
const { _id, _rev } = doc;
|
|
||||||
const id = `${PREFIX}_${originalDate.getTime().toString(36)}_${digest.substr(0, 6)}`;
|
const id = `${PREFIX}_${originalDate.getTime().toString(36)}_${digest.substr(0, 6)}`;
|
||||||
|
|
||||||
let continueProcessing = true;
|
|
||||||
try {
|
|
||||||
const existingRecord = await find([id]);
|
|
||||||
if (existingRecord.rows[0].doc.digest === digest) {
|
|
||||||
continueProcessing = false;
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
// Basically this means there are no existing records
|
|
||||||
}
|
|
||||||
|
|
||||||
if (continueProcessing) {
|
|
||||||
const newDoc = Object.assign(
|
const newDoc = Object.assign(
|
||||||
{},
|
{},
|
||||||
doc,
|
doc,
|
||||||
@ -144,8 +148,6 @@ const processImportables = backgroundTask(async function _processImportables() {
|
|||||||
} catch (e) {
|
} catch (e) {
|
||||||
error(`Error processing Image ${id}`, e);
|
error(`Error processing Image ${id}`, e);
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
imported.fire(id, _id, false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await db.remove({ _id, _rev });
|
await db.remove({ _id, _rev });
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user