MCPcopy Create free account
hub / github.com/TechJeeper/Printventory / scanDirectory

Function scanDirectory

scan-worker.js:217–354  ·  view source on GitHub ↗
(directoryPath, maxFileSize, enableZipArchives = false, scanExtensions = null)

Source from the content-addressed store, hash-verified

215 try {
216 const fileHash = hash.digest('hex');
217 resolve(fileHash);
218 } catch (err) {
219 console.error(`Error generating final hash for file: ${filePath}`, err);
220 reject(err);
221 }
222 });
223 });
224}
225
226async function scanDirectory(directoryPath, maxFileSize, enableZipArchives = false, scanExtensions = null) {
227 const files = [];
228 /** Every file-type dirent seen while walking the tree (matches legacy totalFiles meaning). */
229 let traversedFileEntries = 0;
230 const extSet = buildScanExtensionSet(scanExtensions);
231
232 const shouldQueueFile = (fileName) => {
233 // Skip Printventory zip-extract temps if they somehow land under a scanned tree
234 if (typeof fileName === 'string' && fileName.startsWith('printventory_')) return false;
235 const ext = path.extname(fileName).toLowerCase();
236 if (extSet.has(ext)) return true;
237 if (enableZipArchives && ext === '.zip') return true;
238 return false;
239 };
240
241 // Throttle progress IPC: walking huge folders is sync; avoid flooding the main process.
242 const TRAVERSE_PROGRESS_INTERVAL = isDockerContainer() ? 2000 : 1000;
243
244 const reportTraversedFile = () => {
245 traversedFileEntries++;
246 if (traversedFileEntries % TRAVERSE_PROGRESS_INTERVAL === 0) {
247 parentPort.postMessage({
248 type: 'progress',
249 processed: traversedFileEntries
250 });
251 }
252 };
253
254 // Use a simple queue system
255 const queue = [{ type: 'dir', path: directoryPath }];
256 const seenDirs = new Set();
257 let activeOps = 0;
258
259 // Promise to signal completion
260 let resolveDone;
261 const donePromise = new Promise(resolve => { resolveDone = resolve; });
262
263 const processNext = () => {
264 // If no active ops and queue is empty, we are done
265 if (activeOps === 0 && queue.length === 0) {
266 if (traversedFileEntries > 0) {
267 parentPort.postMessage({
268 type: 'progress',
269 processed: traversedFileEntries
270 });
271 }
272 resolveDone({ files, totalFiles: traversedFileEntries });
273 return;
274 }

Callers 1

scan-worker.jsFile · 0.70

Calls 3

buildScanExtensionSetFunction · 0.85
processNextFunction · 0.85
isDockerContainerFunction · 0.70

Tested by

no test coverage detected