Tutorials > A File Manager tool WHOLE CHAPTER IN ONE FILE INTRODUCTION > INTRODUCTION You'll build: a Library Doctor card on the File Manager page that scans the SD card, shows book counts and sizes per folder, finds probable duplicates, and moves the duplicates into a /Duplicates folder with one click. You'll learn: "mount": "files", the same-origin file endpoints (/api/files, /mkdir, /move), going easy on a small device, and reading an EPUB with the preloaded JSZip. FILES > OVERVIEW /plugins/library-doctor/ +-- manifest.json +-- plugin.js FILES > MANIFEST.JSON { "name": "library-doctor", "title": "Library Doctor", "description": "Book statistics and duplicate finder for the File Manager.", "version": "0.1.0", "mount": "files" } "mount": "files" puts the card under the file list on /files. On /plugins it only gets a small info card. FILES > PLUGIN.JS (1/4) 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) => ({ '&': '&', '<': '<', '>': '>', '"': '"' }[c])); const form = (o) => new URLSearchParams(o); container.innerHTML = `
`; let duplicates = []; // paths to move // One request at a time: the reader's web server is single-threaded. async function listDir(path) { const r = await fetch('/api/files?path=' + encodeURIComponent(path)); if (!r.ok) throw new Error(`list ${path}: HTTP ${r.status}`); return r.json(); } async function scan(root) { const books = []; FILES > PLUGIN.JS (2/4) const queue = [root.replace(/\/+$/, '') || '/']; let dirs = 0; while (queue.length) { const dir = queue.shift(); if (++dirs > 500) throw new Error('more than 500 folders; pick a smaller scan folder'); $('[data-status]').textContent = `Scanning ${dir} ... (${books.length} books so far)`; for (const e of await listDir(dir)) { const path = (dir === '/' ? '' : dir) + '/' + e.name; if (e.name.startsWith('.')) continue; // hidden files and folders if (e.isDirectory) { if (!SKIP_DIRS.includes(path)) queue.push(path); } else if (BOOK.test(e.name)) { books.push({ path, dir, name: e.name, size: e.size }); } } } return books; } // "Dune (1).epub", "dune.epub", "Dune - copy.EPUB" -> "dune.epub". // The extension stays part of the key: Dune.epub and Dune.txt are not duplicates. const key = (name) => { const ext = (name.match(BOOK) || [''])[0].toLowerCase(); return name.toLowerCase() .replace(BOOK, '') .replace(/\s*(\(\d+\)|- ?copy|copy)\s*$/g, '') .replace(/[^a-z0-9]+/g, '') + ext; }; function report(books) { const byDir = new Map(); for (const b of books) { const d = byDir.get(b.dir) || { n: 0, size: 0 }; d.n++; d.size += b.size; byDir.set(b.dir, d); } const groups = new Map(); FILES > PLUGIN.JS (3/4) for (const b of books) (groups.get(key(b.name)) || groups.set(key(b.name), []).get(key(b.name))).push(b); duplicates = []; const dupRows = []; for (const list of groups.values()) { if (list.length < 2) continue; list.sort((a, b) => b.size - a.size); // keep the biggest copy for (const extra of list.slice(1)) duplicates.push(extra.path); dupRows.push(`
${books.length} books, ${fmt(total)} total.
| ${esc(d)} | ${v.n} | ${fmt(v.size)} |
${duplicates.length} probable duplicates