@ngageoint/mage.sftp.service
Version:
The SFTP service package is a MAGE server plugin that sends observations to and SFTP location on create and update.
109 lines • 5.65 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 __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GeoJsonFormatter = void 0;
const entities_events_1 = require("@ngageoint/mage.service/lib/entities/events/entities.events");
const entities_events_forms_1 = require("@ngageoint/mage.service/lib/entities/events/entities.events.forms");
const entities_observations_1 = require("@ngageoint/mage.service/lib/entities/observations/entities.observations");
const archiver_1 = __importDefault(require("archiver"));
const entities_format_1 = require("./entities.format");
const fs_1 = require("fs");
const node_stream_1 = require("node:stream");
class GeoJsonFormatter {
constructor(userRepository, attachmentStore) {
this.userRepository = userRepository;
this.attachmentStore = attachmentStore;
}
createArchive(observation, event) {
return __awaiter(this, void 0, void 0, function* () {
try {
const archive = (0, archiver_1.default)('zip');
const geojson = yield this.createObservationGeoJSON(observation, event);
archive.append(JSON.stringify(geojson), { name: 'observation.geojson' });
for (const attachment of observation.attachments) {
const stream = yield this.attachmentStore.readContent(attachment.id, entities_observations_1.Observation.evaluate(observation, new entities_events_1.MageEvent(event)));
if (stream instanceof fs_1.ReadStream) {
archive.append(node_stream_1.Readable.from(stream), { name: this.getAttachmentPath(attachment) });
}
else {
return entities_format_1.ArchiveResult.incomplete(archive);
}
}
return entities_format_1.ArchiveResult.complete(archive);
}
catch (e) {
return entities_format_1.ArchiveError.error(e, observation.id);
}
});
}
createObservationGeoJSON(observation, event) {
return __awaiter(this, void 0, void 0, function* () {
var formDefinitions = new Map(event.forms.map(form => [form.id, form]));
const forms = [];
observation.properties.forms.forEach((observationForm) => {
var _a;
const formDefinition = formDefinitions.get(observationForm.formId);
const fields = {};
(_a = formDefinition === null || formDefinition === void 0 ? void 0 : formDefinition.fields) === null || _a === void 0 ? void 0 : _a.forEach((fieldDefinition) => {
if (fieldDefinition.type === entities_events_forms_1.FormFieldType.Attachment) {
const attachments = observation.attachments.filter((attachment) => {
return attachment.fieldName === fieldDefinition.name && attachment.observationFormId === observationForm.id;
}).map((attachment) => {
return this.getAttachmentPath(attachment);
});
fields[fieldDefinition.title] = attachments;
}
else {
const fieldValue = observationForm[fieldDefinition.name];
if (fieldValue !== undefined) {
fields[fieldDefinition.title] = fieldValue;
}
}
});
forms.push({
title: formDefinition === null || formDefinition === void 0 ? void 0 : formDefinition.name,
fields
});
});
let user = null;
if (observation.userId) {
user = yield this.userRepository.findById(observation.userId);
}
const geojson = {
type: "Feature",
geometry: observation.geometry,
properties: {
timestamp: observation.properties.timestamp,
user: {
id: user === null || user === void 0 ? void 0 : user.id,
name: user === null || user === void 0 ? void 0 : user.displayName
},
event: {
id: event.id,
name: event.name,
description: event.description
},
forms,
}
};
return geojson;
});
}
getAttachmentPath(attachment) {
const filename = attachment.name || `${attachment.id}}`;
return `media/${attachment.id}/${filename}`;
}
}
exports.GeoJsonFormatter = GeoJsonFormatter;
//# sourceMappingURL=geojson.js.map