mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 18:10:06 -05:00
Every attachment gets its SHA-256 (contentHash: value, size, time) and its plain search text (with the time it was made) when it is added, and - for one added before this - the first time it is read whole. Text, Markdown, CSV, code, JSON, HTML and e-mail are read as text, PDF and Office through the document extractor, Outlook .msg and OpenDocument from their slideshow rows. A new attachment's "Added" card history row carries its hash, and so do the soft delete and restore rows. Each whole read hashes the bytes on their way out, with no second read: no stored hash - it is stored; the same - nothing; another hash that the card history knows - an earlier version an undo or restore brought back - a new hash and search text are made; anything else - a warning in Admin Panel / Problems (integrity), keeping the stored hash. A range is no evidence. Filter's text field, the board's Search and Search All Boards find a card by its attachments' text; each index row is checked against its live attachment on that card, and the text is never sent to the browser. tests/attachmentContent.test.cjs pins the decisions and the wiring; tests/playwright/specs/attachment-content-search.e2e.js adds a file, finds it three ways, changes it on disk (warned), reads a range (nothing) and brings back a known version (accepted, indexed again). Thanks to xet7 !
33 lines
1.6 KiB
JavaScript
33 lines
1.6 KiB
JavaScript
// Read-only, bounded-batch join. Callers supply an authorized card scope.
|
|
// No child text or unverified child cardId is returned to the client.
|
|
export async function boardTextSearch({ cards, children, scope, term, stopped = () => false }) {
|
|
const regex = new RegExp(term.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'i');
|
|
const matches = new Set();
|
|
let after;
|
|
while (!stopped()) {
|
|
const batch = await cards.find({ $and: [scope, ...(after ? [{ _id: { $gt: after } }] : [])] },
|
|
{ projection: { _id: 1, title: 1, description: 1 } }).sort({ _id: 1 }).limit(250).toArray();
|
|
if (!batch.length) break;
|
|
const ids = batch.map(card => card._id), allowed = new Set(ids);
|
|
for (const card of batch) {
|
|
if ([card.title, card.description].some(text => typeof text === 'string' && regex.test(text))) matches.add(card._id);
|
|
}
|
|
for (const { collection, field } of children) {
|
|
if (stopped()) return [];
|
|
const cursor = collection.find({ cardId: { $in: ids }, boardId: scope.boardId, [field]: regex },
|
|
{ projection: { cardId: 1 } }).batchSize(250);
|
|
try {
|
|
for await (const row of cursor) if (allowed.has(row.cardId)) matches.add(row.cardId);
|
|
} finally { await cursor.close(); }
|
|
}
|
|
// The text inside the cards' attachments (server/lib/attachmentContentIndex.js),
|
|
// live attachments only.
|
|
if (!stopped()) {
|
|
const { attachmentTextCardIds } = require('/server/lib/attachmentContentIndex');
|
|
for (const id of await attachmentTextCardIds({ cardIds: ids, regex })) matches.add(id);
|
|
}
|
|
after = batch[batch.length - 1]._id;
|
|
}
|
|
return stopped() ? [] : [...matches];
|
|
}
|