03 plugin.js (1/5)

File: examples/plugins/file-tool/plugin.js (133 lines)

// file-tool: a File Manager plugin ("mount": "files").
//
// Renders below the file list on /files. Scans a folder tree with
// /api/files, shows book counts and sizes per folder, finds probable
// duplicates by normalised file name, and can move them to /Duplicates.
// JSZip is preloaded on /files (window.JSZip) if you want to open EPUBs.
//
// Also registers a "stats" job action (see the job-runner example).

CrossPoint.registerPlugin((container, api) => {
  const $ = (s) => container.querySelector(s);
  const BOOK = /\.(epub|xtc|txt|md|pdf)$/i;
  // Never scan plugin folders (their README.md files are not books) or our own output.
  const SKIP_DIRS = ['/plugins', '/Duplicates', '/System Volume Information'];
  const fmt = (b) => b > 1048576 ? (b / 1048576).toFixed(1) + ' MB' : Math.ceil(b / 1024) + ' KB';
  const esc = (s) => s.replace(/[&<>"]/g, (c) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;' }[c]));
  const form = (o) => new URLSearchParams(o);
‹ PreviousReader file (.txt)Next ›