Skip to content

Commit

Permalink
Merge pull request #1481 from Shelf-nu/1471-bug-search-bug-android-pwa
Browse files Browse the repository at this point in the history
fix: escaping of special characters in AssetViewSearch
  • Loading branch information
DonKoko authored Dec 5, 2024
2 parents ff5bfb8 + 7304a13 commit f400740
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions app/modules/asset/service.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,17 +242,33 @@ async function getAssetsFromView(params: {

/** If the search string exists, add it to the where object */
if (search) {
const words = search
.replace(/([()&|!'<>])/g, "\\$1") // escape special characters
.trim()
.replace(/ +/g, " ") //replace multiple spaces into 1
.split(" ")
.map((w) => w.trim() + ":*") //remove leading and trailing spaces
.filter(Boolean)
.join(" & ");
where.searchVector = {
search: words,
};
try {
const words = search
.replace(/([()&|!':><])/g, "\\$1")
.trim()
.replace(/ +/g, " ")
.split(" ")
.map((w) => {
const trimmed = w.trim();
return trimmed ? trimmed + ":*" : "";
})
.filter(Boolean)
.join(" & ");

where.searchVector = {
search: words || undefined, // Prevent empty search causing DB error
};
} catch (error) {
// Log error but allow query to continue without search filter
Logger.error(
new ShelfError({
cause: error,
message: "Failed to parse search string for tsquery",
additionalData: { search },
label: "Assets",
})
);
}
}

if (status && where.asset) {
Expand Down

0 comments on commit f400740

Please sign in to comment.