Skip to content

Commit

Permalink
Validate exported markers
Browse files Browse the repository at this point in the history
Ensure all markers we're exporting are valid before adding them to the export
database.
  • Loading branch information
danrahn committed May 4, 2024
1 parent 623166f commit 3d0a0ed
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
17 changes: 16 additions & 1 deletion Server/ImportExport.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,28 @@ WHERE t.tag_id=$tagId`;
// This is okay, since our import method should handle it gracefully.

const txn = new TransactionBuilder(db);
const nullOrUndefined = (...ms) => {
for (const m of ms) {
if (m === null || m === undefined) {
return true;
}
}

return false;
};

for (const marker of markers) {
// TODO: Update the schema to add user_created instead of this current disconnect.
let modifiedAt = MarkerEditCache.getModifiedAt(marker.id);
if (modifiedAt === null && MarkerEditCache.getUserCreated(marker.id)) {
modifiedAt = -marker.created_at;
}

if (!marker || !marker.marker_type || nullOrUndefined(marker.start, marker.end, marker.created_at, marker.extra, marker.guid)) {
Log.warn(marker, `Found invalid marker, cannot export`);
continue;
}

txn.addStatement(
`INSERT INTO markers
(marker_type, start, end, modified_at, created_at, extra, guid) VALUES
Expand All @@ -199,7 +214,7 @@ WHERE t.tag_id=$tagId`;
});
}

Log.info(`Adding ${markers.length} markers to database export.`);
Log.info(`Adding ${txn.statementCount()} markers to database export.`);
await txn.exec();

// All items have been added, close the db for writing and pipe it to the user.
Expand Down
4 changes: 2 additions & 2 deletions Server/SqliteDatabase.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ class SqliteDatabase {
* @param {DbQueryParameters|null} parameters
* @returns {Promise<any>} */
#action(fn, query, parameters=null) {
return new Promise((resolve, reject) => {
return new Promise(resolve => {
const callback = (err, result) => {
if (err) { reject(err.message); }
if (err) { throw new ServerError.FromDbError(err); }

resolve(result);
};
Expand Down

0 comments on commit 3d0a0ed

Please sign in to comment.