Skip to content
This repository has been archived by the owner on Aug 9, 2021. It is now read-only.

Commit

Permalink
Make sure we only try to parse thermalRaw files!
Browse files Browse the repository at this point in the history
  • Loading branch information
hardiesoft committed Jun 3, 2021
1 parent 9075ecb commit 8e82226
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 40 deletions.
77 changes: 40 additions & 37 deletions api/V1/recordingUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,50 +134,53 @@ function makeUploadHandler(mungeData?: (any) => any) {
if (mungeData) {
data = mungeData(data);
}

// Read the file back out from s3 and decode/parse it.
const fileData = await modelsUtil
.openS3()
.getObject({
Bucket: config.s3.bucket,
Key: key,
})
.promise()
.catch((err) => {
return err;
});
const {CptvDecoder} = await dynamicImportESM("cptv-decoder");
const decoder = new CptvDecoder();
const metadata = await decoder.getBytesMetadata(new Uint8Array(fileData.Body));
// If true, the parser failed for some reason, so the file is probably corrupt, and should be investigated later.
const fileIsCorrupt = await decoder.hasStreamError();
if (fileIsCorrupt) {
log.warn("CPTV Stream error", await decoder.getStreamError());
}
decoder.close();
const recording = models.Recording.buildSafely(data);

// Add the filehash if present
if (data.fileHash) {
recording.rawFileHash = data.fileHash;
}

if (!data.hasOwnProperty("location") && metadata.latitude && metadata.longitude) {
// @ts-ignore
recording.location = [metadata.latitude, metadata.longitude];
}
if (!data.hasOwnProperty("duration") && metadata.duration) {
recording.duration = metadata.duration;
}
if (!data.hasOwnProperty("recordingDateTime") && metadata.timestamp) {
recording.recordingDateTime = new Date(metadata.timestamp / 1000).toISOString();
}
if (!data.hasOwnProperty("additionalMetadata") && metadata.previewSecs) {
// NOTE: Algorithm property gets filled in later by AI
recording.additionalMetadata = {
previewSecs: metadata.previewSecs,
totalFrames: metadata.totalFrames,
};
let fileIsCorrupt = false;
if (data.type === "thermalRaw") {
// Read the file back out from s3 and decode/parse it.
const fileData = await modelsUtil
.openS3()
.getObject({
Bucket: config.s3.bucket,
Key: key,
})
.promise()
.catch((err) => {
return err;
});
const {CptvDecoder} = await dynamicImportESM("cptv-decoder");
const decoder = new CptvDecoder();
const metadata = await decoder.getBytesMetadata(new Uint8Array(fileData.Body));
// If true, the parser failed for some reason, so the file is probably corrupt, and should be investigated later.
fileIsCorrupt = await decoder.hasStreamError();
if (fileIsCorrupt) {
log.warn("CPTV Stream error", await decoder.getStreamError());
}
decoder.close();

if (!data.hasOwnProperty("location") && metadata.latitude && metadata.longitude) {
// @ts-ignore
recording.location = [metadata.latitude, metadata.longitude];
}
if (!data.hasOwnProperty("duration") && metadata.duration) {
recording.duration = metadata.duration;
}
if (!data.hasOwnProperty("recordingDateTime") && metadata.timestamp) {
recording.recordingDateTime = new Date(metadata.timestamp / 1000).toISOString();
}
if (!data.hasOwnProperty("additionalMetadata") && metadata.previewSecs) {
// NOTE: Algorithm property gets filled in later by AI
recording.additionalMetadata = {
previewSecs: metadata.previewSecs,
totalFrames: metadata.totalFrames,
};
}
}

recording.rawFileKey = key;
Expand Down
3 changes: 0 additions & 3 deletions test/test_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,6 @@ def check_line(self, rec, device, exp_audio_bait_name=None, exp_audio_bait_time=
if device.location:
assert line["Latitude"] == "{}".format(device.location[0])
assert line["Longitude"] == "{}".format(device.location[1])
else:
assert line["Latitude"] == ""
assert line["Longitude"] == ""

assert line["Comment"] == rec["comment"]
assert line["BatteryPercent"] == "98"
Expand Down

0 comments on commit 8e82226

Please sign in to comment.