MCPcopy Create free account
hub / github.com/Acode-Foundation/Acode / sendFile

Function sendFile

src/lib/run.js:434–487  ·  view source on GitHub ↗

* Sends file * @param {string} path * @param {string} id * @returns

(path, id)

Source from the content-addressed store, hash-verified

432 * @returns
433 */
434 async function sendFile(path, id) {
435 if (isLoading) {
436 queue.push(() => {
437 sendFile(path, id);
438 });
439 return;
440 }
441
442 isLoading = true;
443 const protocol = Url.getProtocol(path);
444 const ext = Url.extname(path);
445 const mimetype = mimeType.lookup(ext);
446 if (/s?ftp:/.test(protocol)) {
447 const cacheFile = Url.join(
448 CACHE_STORAGE,
449 protocol.slice(0, -1) + path.hashCode(),
450 );
451 const fs = fsOperation(path);
452 try {
453 await fs.readFile(); // Because reading the remote file will create cache file
454 path = cacheFile;
455 } catch (err) {
456 error(id);
457 isLoading = false;
458 return;
459 }
460 } else if (protocol === "content:") {
461 path = await new Promise((resolve, reject) => {
462 sdcard.formatUri(path, resolve, reject);
463 });
464 } else if (!/^file:/.test(protocol)) {
465 const fileContent = await fsOperation(path).readFile();
466 const tempFileName = path.hashCode();
467 const tempFile = Url.join(CACHE_STORAGE, tempFileName);
468 if (!(await fsOperation(tempFile).exists())) {
469 await fsOperation(CACHE_STORAGE).createFile(tempFileName, fileContent);
470 } else {
471 await fsOperation(tempFile).writeFile(fileContent);
472 }
473 path = tempFile;
474 }
475
476 webServer?.send(id, {
477 status: 200,
478 path,
479 headers: {
480 "Content-Type": mimetype,
481 },
482 });
483
484 isLoading = false;
485 const action = queue.splice(-1, 1)[0];
486 if (typeof action === "function") action();
487 }
488
489 /**
490 * Sends file content

Callers 2

sendByExtFunction · 0.85
sendIcoFunction · 0.85

Calls 10

fsOperationFunction · 0.85
actionFunction · 0.85
hashCodeMethod · 0.80
errorFunction · 0.70
formatUriMethod · 0.65
existsMethod · 0.65
createFileMethod · 0.65
sendMethod · 0.65
readFileMethod · 0.45
writeFileMethod · 0.45

Tested by

no test coverage detected