Skip to content

Commit

Permalink
Hack: ignore filename for path equality
Browse files Browse the repository at this point in the history
In order to be able to see/exclude pictures from a specific directory.
This feels more natural, since the file path uses a directory as a filter by default.

Could be otherwise implemented by upping schema version and saving another column for directory path in db, but it's more duplicate data, and would prevent going back.
  • Loading branch information
AlanDrake committed Dec 17, 2023
1 parent 631cfb5 commit ef99687
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/backend/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { FileSearchDTO } from '../api/file-search';
import { ID } from '../api/id';
import { LocationDTO } from '../api/location';
import { ROOT_TAG_ID, TagDTO } from '../api/tag';
import Path from 'path';

/**
* The backend of the application serves as an API, even though it runs on the same machine.
Expand Down Expand Up @@ -427,14 +428,40 @@ function filterStringWhere<T>(
'startsWith',
] as const;

// Special case for absolutePath equality: only match the directory part
if (
crit.key === 'absolutePath' &&
['equals', 'equalsIgnoreCase', 'notEqual'].includes(crit.operator)
) {
return filterDirectoryPath(crit);
}

if ((dbStringOperators as readonly string[]).includes(crit.operator)) {
const funcName = crit.operator as unknown as (typeof dbStringOperators)[number];
const funcName = crit.operator as unknown as typeof dbStringOperators[number];
return where[funcName](crit.value);
}
// Use normal string filter as fallback for functions not supported by the DB
return filterStringLambda(crit);
}

// Filter directory path without filename
function filterDirectoryPath<T>(crit: StringConditionDTO<T>): (t: any) => boolean {
const { key, value } = crit;
const valLow = value.toLowerCase();

switch (crit.operator) {
case 'equals':
return (t: any) => Path.relative(Path.dirname(t[key] as string), value) === '';
case 'equalsIgnoreCase':
return (t: any) => Path.relative(Path.dirname(t[key] as string).toLowerCase(), valLow) === '';
case 'notEqual':
return (t: any) => Path.relative(Path.dirname(t[key] as string), value) !== '';
default:
console.log('String operator not allowed:', crit.operator);
return () => false;
}
}

function filterStringLambda<T>(crit: StringConditionDTO<T>): (t: any) => boolean {
const { key, value } = crit;
const valLow = value.toLowerCase();
Expand Down

0 comments on commit ef99687

Please sign in to comment.