Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hack: ignore filename for path equality #614

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading