node-djiparsetxt
Version:
command-line application that reads a DJI '.txt' file and outputs a json.
176 lines • 7.36 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileParsingService = void 0;
var ServiceManager_1 = require("../common/ServiceManager");
var Version_1 = require("../common/Version");
var BaseService_1 = __importDefault(require("./BaseService"));
var BinaryParserService_1 = require("./BinaryParserService");
var RecordTypes_1 = require("./RecordTypes");
function is_jpeg_soi(buffer, offset) {
return (buffer.readUInt8(offset) === 0xff && buffer.readUInt8(offset + 1) === 0xd8);
}
function is_jpeg_eoi(buffer, offset) {
return (buffer.readUInt8(offset) === 0xff && buffer.readUInt8(offset + 1) === 0xd9);
}
var FileParsingService = /** @class */ (function (_super) {
__extends(FileParsingService, _super);
function FileParsingService() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.name = "file_parsing";
return _this;
}
/**
* Parses the
* @param buffer File buffer to parse.
* @param headerInfo The parsed values from the header part of the file.
*/
FileParsingService.prototype.parse_records = function (buffer, headerInfo) {
if (headerInfo === undefined) {
var fileInfoService = this.serviceMan.get_service(ServiceManager_1.ServiceTypes.FileInfo);
headerInfo = fileInfoService.get_header_info(buffer);
}
var recordsBuff = buffer.slice(100);
var limit = headerInfo.records_size;
var records = this.get_record_cache(recordsBuff, limit, headerInfo.version);
return records;
};
FileParsingService.prototype.filter_records = function (records, type) {
return records.records.filter(function (val) { return val.type === type; });
};
FileParsingService.prototype.createEmptyCache = function () {
var version = Version_1.Version.CreateEmpty();
return {
records: [],
version: version,
isEmpty: true,
stats: {
records_area_size: 0,
record_count: 0,
type_count: {},
invalid_records: 0,
},
};
};
FileParsingService.prototype.parse_record_by_type = function (record, recordType) {
var parserService = this.serviceMan.get_service(ServiceManager_1.ServiceTypes.Parsers);
try {
return parserService.get_record_parser(recordType).parse(record.data[0]);
}
catch (e) {
console.log("Record type " + RecordTypes_1.RecordTypes[recordType] + " had error parsing");
throw e;
}
};
FileParsingService.prototype.get_record_cache = function (buffer, limit, version) {
var parserService = this.serviceMan.get_service(ServiceManager_1.ServiceTypes.Parsers);
var recordParser = parserService.get_parser(BinaryParserService_1.ParserTypes.BaseRecord);
var recordStartParser = parserService.get_parser(BinaryParserService_1.ParserTypes.StartRecord);
var recordCache = {
records: [],
version: version,
stats: {
records_area_size: buffer.length,
record_count: 0,
type_count: {},
invalid_records: 0,
},
};
var start = 0;
while (start < limit) {
var recStart = recordStartParser.parse(buffer.slice(start));
var record = void 0;
if (recStart.type === RecordTypes_1.RecordTypes.JPEG) {
// check for starting zeros
var zeroWatermarkLo = buffer.readUInt8(start + 2);
var zeroWatermarkHi = buffer.readUInt8(start + 3);
if ((zeroWatermarkHi | zeroWatermarkLo) !== 0) {
throw Error("No zero watermark while parsing jpeg record");
}
if (is_jpeg_soi(buffer, start + 4)) {
// handle jpeg record with jpegs in them
var jpegs = [];
var startOfJpeg = start + 4;
var endOfJpeg = startOfJpeg;
while (is_jpeg_soi(buffer, startOfJpeg)) {
endOfJpeg = this.getJpegEoiIndex(buffer, startOfJpeg + 2);
if (endOfJpeg === -1) {
throw new Error("No JPEG_EOI found after JPEG_SOI");
}
jpegs.push(buffer.slice(startOfJpeg, endOfJpeg));
startOfJpeg = endOfJpeg;
}
record = {
type: RecordTypes_1.RecordTypes.JPEG,
length: recStart.length,
data: jpegs,
};
start = endOfJpeg;
}
else {
// handle an empty jpeg record
record = {
type: RecordTypes_1.RecordTypes.JPEG,
length: recStart.length,
data: [Buffer.alloc(0)],
};
start += 4;
}
}
else {
var parsedRecord = recordParser.parse(buffer.slice(start));
record = {
length: parsedRecord.length,
type: parsedRecord.type,
data: [parsedRecord.data],
};
start += record.length + 3;
}
if (record !== null) {
this.addRecordToCache(recordCache, record);
}
}
return recordCache;
};
FileParsingService.prototype.getJpegEoiIndex = function (buffer, index) {
var iter = index;
while (iter < buffer.length - 1) {
if (is_jpeg_eoi(buffer, iter)) {
return iter + 2;
}
iter += 1;
}
return -1;
};
FileParsingService.prototype.addRecordToCache = function (cache, record) {
cache.records.push(record);
cache.stats.record_count += 1;
if (record.type !== RecordTypes_1.RecordTypes.JPEG && record.marker !== 0xff) {
cache.stats.invalid_records += 1;
return;
}
if (cache.stats.type_count[record.type] === null) {
cache.stats.type_count[record.type] = 1;
return;
}
cache.stats.type_count[record.type] += 1;
};
return FileParsingService;
}(BaseService_1.default));
exports.FileParsingService = FileParsingService;
//# sourceMappingURL=FileParsingService.js.map