@ngageoint/mage.image.service
Version:
Orient images attached to MAGE observations according to EXIF meta-data and generate configurable size thumbnails.
152 lines • 9.34 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
var __asyncValues = (this && this.__asyncValues) || function (o) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var g = generator.apply(thisArg, _arguments || []), i, q = [];
return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
function fulfill(value) { resume("next", value); }
function reject(value) { resume("throw", value); }
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FindUnprocessedAttachments = void 0;
function FindUnprocessedAttachments(getDbConn, console) {
return (eventProcessingStates, lastModifiedAfter, lastModifiedBefore, limit) => __awaiter(this, void 0, void 0, function* () {
return {
[Symbol.asyncIterator]: function () {
return __asyncGenerator(this, arguments, function* () {
var _a, e_1, _b, _c, _d, e_2, _e, _f;
limit = Number(limit);
let remainingCount = limit > 0 ? limit : Number.POSITIVE_INFINITY;
const conn = yield __await(getDbConn());
const eventStateCursor = eventStatesWithCollectionNames(conn, eventProcessingStates, console);
try {
for (var _g = true, eventStateCursor_1 = __asyncValues(eventStateCursor), eventStateCursor_1_1; eventStateCursor_1_1 = yield __await(eventStateCursor_1.next()), _a = eventStateCursor_1_1.done, !_a;) {
_c = eventStateCursor_1_1.value;
_g = false;
try {
const eventState = _c;
const eventId = eventState.event.id;
const queryStages = createAggregationPipeline(eventState, lastModifiedAfter, lastModifiedBefore);
if (limit) {
queryStages.push({ $limit: remainingCount });
}
console.info(`query unprocessed attachments from event ${eventId} (${eventState.event.name}) newer than ${new Date(eventState.latestAttachmentProcessedTimestamp).toISOString()}`);
const collection = conn.collection(eventState.collectionName);
const cursor = yield __await(collection.aggregate(queryStages));
try {
for (var _h = true, cursor_1 = (e_2 = void 0, __asyncValues(cursor)), cursor_1_1; cursor_1_1 = yield __await(cursor_1.next()), _d = cursor_1_1.done, !_d;) {
_f = cursor_1_1.value;
_h = false;
try {
const doc = _f;
yield yield __await({ eventId: eventState.event.id, observationId: doc.observationId.toString(), attachmentId: doc.attachmentId.toString() });
if (--remainingCount === 0) {
console.info(`reached unprocessed attachment limit ${limit}; cancelling query iteration`);
return yield __await(yield __await(cursor.close().then(_ => eventStateCursor.return(null)).then(_ => null)));
}
}
finally {
_h = true;
}
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (!_h && !_d && (_e = cursor_1.return)) yield __await(_e.call(cursor_1));
}
finally { if (e_2) throw e_2.error; }
}
}
finally {
_g = true;
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (!_g && !_a && (_b = eventStateCursor_1.return)) yield __await(_b.call(eventStateCursor_1));
}
finally { if (e_1) throw e_1.error; }
}
});
}
};
});
}
exports.FindUnprocessedAttachments = FindUnprocessedAttachments;
function eventStatesWithCollectionNames(conn, eventStates, console) {
return __asyncGenerator(this, arguments, function* eventStatesWithCollectionNames_1() {
for (const eventState of eventStates) {
const found = yield __await(conn.collection('events').findOne({ _id: eventState.event.id }));
if (found) {
yield yield __await(Object.assign(Object.assign({}, eventState), { collectionName: found.collectionName }));
}
else {
console.warn(`event not found for event processing state:`, eventState);
}
}
});
}
function createAggregationPipeline(eventState, lastModifiedAfter, lastModifiedBefore) {
const match = { $match: {
'attachments.oriented': false,
'attachments.contentType': /^image/,
$or: [
{ 'states.0.name': { $eq: 'active' } },
{ 'states.0.name': { $exists: false } }
]
} };
lastModifiedAfter = typeof lastModifiedAfter === 'number' ? lastModifiedAfter : eventState.latestAttachmentProcessedTimestamp;
const lastModified = { $gte: new Date(lastModifiedAfter) };
if (lastModifiedBefore) {
lastModified.$lte = new Date(lastModifiedBefore);
}
match.$match = Object.assign({ lastModified }, match.$match);
/*
TODO: how does mongo driver account for nondeterministic key order in this sort document?
BSON keys are ordered, but JSON key order in Node/V8 is not guaranteed.
*/
const sort = { $sort: { lastModified: 1, _id: 1 } };
const projectFilteredAttachments = {
$project: {
observationId: '$_id', _id: false,
attachments: {
$filter: { input: '$attachments', as: 'att',
cond: {
$and: [
{ $eq: ['$$att.oriented', false] },
{ $gt: ['$$att.relativePath', null] },
{ $eq: [{ $indexOfBytes: ['$$att.contentType', 'image/'] }, 0] }
]
}
}
}
}
};
const unwindAttachments = { $unwind: '$attachments' };
const projectAttachmentId = { $project: { observationId: 1, attachmentId: '$attachments._id' } };
return [match, sort, projectFilteredAttachments, unwindAttachments, projectAttachmentId];
}
//# sourceMappingURL=adapters.db.mongo.js.map