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 = `

Library Doctor

Scan folder

`; 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(`
  • ${esc(list[0].path)} (kept)
    ` + list.slice(1).map((b) => esc(b.path)).join('
    ') + '
  • '); } const total = books.reduce((s, b) => s + b.size, 0); $('[data-report]').innerHTML = `

    ${books.length} books, ${fmt(total)} total.

    ${[...byDir].sort((a, b) => b[1].size - a[1].size).map(([d, v]) => ``).join('')}
    ${esc(d)}${v.n} ${fmt(v.size)}

    ${duplicates.length} probable duplicates

    `; $('[data-act=move]').disabled = duplicates.length === 0; } $('[data-act=scan]').onclick = async () => { $('[data-act=scan]').disabled = true; try { const books = await scan($('[data-root]').value.trim() || '/'); report(books); $('[data-status]').textContent = 'Done.'; } catch (e) { FILES > PLUGIN.JS (4/4) $('[data-status]').textContent = 'Error: ' + e.message; } finally { $('[data-act=scan]').disabled = false; } }; $('[data-act=move]').onclick = async () => { if (!confirm(`Move ${duplicates.length} files to /Duplicates?`)) return; await fetch('/mkdir', { method: 'POST', body: form({ name: 'Duplicates', path: '/' }) }); // fails harmlessly if it exists let moved = 0; for (const path of duplicates) { const r = await fetch('/move', { method: 'POST', body: form({ path, dest: '/Duplicates' }) }); if (r.ok) moved++; $('[data-status]').textContent = `Moved ${moved}/${duplicates.length}`; } $('[data-status]').textContent += '. Rescan to refresh.'; $('[data-act=move]').disabled = true; }; // Also usable from scripts (chapter 6): returns only counts, results must stay < 192 bytes. api.registerAction('stats', async (args) => { const books = await scan(args.root || '/'); return { books: books.length, bytes: books.reduce((s, b) => s + b.size, 0) }; }); }); TRY IT > TRY IT Upload, open /files, scroll below the file list, and click Scan. GOING FURTHER: READ EPUB METADATA WITH JSZIP > GOING FURTHER: READ EPUB METADATA WITH JSZIP On /files, JSZip is loaded before your plugin. To compare real titles instead of file names: async function epubTitle(path) { const buf = await (await fetch('/download?path=' + encodeURIComponent(path))).arrayBuffer(); const zip = await JSZip.loadAsync(buf); const container = await zip.file('META-INF/container.xml').async('string'); const opfPath = container.match(/full-path="([^"]+)"/)[1]; const opf = await zip.file(opfPath).async('string'); return (opf.match(/]*>([^<]*) THINGS TO KNOW - /move and /rename only work on files, and the target folder must exist. /delete refuses non-empty folders. - Moving an EPUB clears its layout cache, so the book re-paginates the next time it is opened. Test what happens to reading progress on your device before you move books people are reading. - Skip plugin folders. Every plugin has a README.md, and .md is a book format for the reader. A naive scan treats those READMEs as duplicate "books" and moves them out of the plugins. The code above skips /plugins, and dot-folders (/.crosspoint/plugins, /.plugins) are skipped as hidden. - Dot-folders are hidden from /api/files unless "show hidden files" is on. Skip them anyway. - Don't parallelise requests with Promise.all. The reader serves one request at a time. - Always confirm before destructive actions. Your plugin runs with full access. Next: Tutorial 6: Remote control with the job queue. End of this folder.