diff --git a/icons/icons.js b/icons/icons.js new file mode 100644 index 0000000..548138b --- /dev/null +++ b/icons/icons.js @@ -0,0 +1,24 @@ +// Base64 entries for all PNG icon files. Built using: +// `npx datauri-cli filename`. Initial, common portion of +// data URI and firist few bytes of PNG data are stripped, +// see below when reconstructed. +const base64Images = { + back: 'BAMAAAAyb6E1AAAAElBMVEX////M//+ZmZlmZmYzMzMAAACei5rnAAAAAnRSTlP/AOW3MEoAAAABYktHRACIBR1IAAAAYElEQVQIW0XP0QnAMAhFUVew4ACFLpARUl4XKHH/VdqXRPUjHC5+GNEc4dOQvICelY4KVEWviqo2qVz1Re3HQxq33UnXP02a7xENDcn4Sshb1lv37jh5pC3Me70+ZMWYD08uJsBsi+cYAAAAVnRFWHRjb21tZW50AFRoaXMgYXJ0IGlzIGluIHRoZSBwdWJsaWMgZG9tYWluLiBLZXZpbiBIdWdoZXMsIGtldmluaEBlaXQuY29tLCBTZXB0ZW1iZXIgMTk5NXb275wAAAAASUVORK5CYII=', + blank: 'iVBORw0KGgoAAAANSUhEUgAAABQAAAAWAQMAAAD6jy5FAAAABlBMVEX////M//9zUa6lAAAAAnRSTlP/AOW3MEoAAAABYktHRACIBR1IAAAAD0lEQVQIHWP8z/CRkQYYAFlpKreJcPlsAAAAVnRFWHRjb21tZW50AFRoaXMgYXJ0IGlzIGluIHRoZSBwdWJsaWMgZG9tYWluLiBLZXZpbiBIdWdoZXMsIGtldmluaEBlaXQuY29tLCBTZXB0ZW1iZXIgMTk5NXb275wAAAAASUVORK5CYII=', + folder: 'iVBORw0KGgoAAAANSUhEUgAAABQAAAAWBAMAAAAyb6E1AAAAElBMVEX/////zJnM//+ZZjMzMzMAAADCEvqoAAAAA3RSTlP//wDXyg1BAAAAAWJLR0QAiAUdSAAAAFJJREFUCFtjUIIDBsLMUCCAMFUFgSAIzAwEMUVDQ4OUGIJBTEMgDyhqDARAnnAQRAEQGLvCmcKuVBYVhgJXBlVjCDBxZVAKcYGAEAYl1VAIcAIAfgAg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//+ZmZkzMzMAAABVsTOVAAAAAnRSTlP/AOW3MEoAAAABYktHRACIBR1IAAAAT0lEQVQIW5XIsQ3AIAwF0VNgAa+AGABkD0Dx958pRRy55qrTw93dfZsZC6C1WnZtq2Ubq0unR0Rql5TKM2YqjPmpdCjlVuFG//XxFYYpsxfEkhYAImC9XwAAAFZ0RVh0Y29tbWVudABUaGlzIGFydCBpcyBpbiB0aGUgcHVibGljIGRvbWFpbi4gS2V2aW4gSHVnaGVzLCBrZXZpbmhAZWl0LmNvbSwgU2VwdGVtYmVyIDE5OTV29u+cAAAAAElFTkSuQmCC', + unknown: 'iVBORw0KGgoAAAANSUhEUgAAABQAAAAWBAMAAAAyb6E1AAAAD1BMVEX////M//+ZmZkzMzMAAABVsTOVAAAAAnRSTlP/AOW3MEoAAAABYktHRACIBR1IAAAAYklEQVQIW23O0Q2AIBAD0AZcgA0IYQAJDGCw+8/k3SFoov16FEKKaikhBOyQeP8QxZjIE17pqJR6kK014cZuJ+MBMuXxg1vUewxmst+UMi5GLGLS8mn/+Xo7WdOIjAw60EZeVZkZLhf9K5EAAABWdEVYdGNvbW1lbnQAVGhpcyBhcnQgaXMgaW4gdGhlIHB1YmxpYyBkb21haW4uIEtldmluIEh1Z2hlcywga2V2aW5oQGVpdC5jb20sIFNlcHRlbWJlciAxOTk1dvbvnAAAAABJRU5ErkJggg==' +}; + +module.exports = { + getDataUri: function(name) { + const imgData = base64Images[name]; + if(!imgData) { + throw new Error(`Invalid icon name: ${name}`); + } + + return `data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAW${imgData}`; + } +}; diff --git a/src/html-formatter.js b/src/html-formatter.js index 639988b..3e4ecd7 100644 --- a/src/html-formatter.js +++ b/src/html-formatter.js @@ -1,13 +1,6 @@ const { isMedia, isImage, getMimeType } = require('./content-type'); const { path } = require('filer'); - -const iconImage = require('../icons/image2.png'); -const iconFolder = require('../icons/folder.png'); -const iconMovie = require('../icons/movie.png'); -const iconText = require('../icons/text.png'); -const iconUnknown = require('../icons/unknown.png'); -const iconBack = require('../icons/back.png'); -const iconBlank = require('../icons/blank.png'); +const { getDataUri } = require('../icons/icon.js'); // 20-Apr-2004 17:14 const formatDate = d => { @@ -29,21 +22,15 @@ const formatSize = s => { }; const formatRow = ( - icon = iconUnknown, alt = '[ ]', href, name, modified, size -) => { - modified = formatDate(new Date(modified)); - size = formatSize(size); - - return `${alt} - ${name} - ${modified} - ${size} `; -}; +) => `${alt} + ${name} + ${formatDate(new Date(modified))} + ${formatSize(size)} `; const footerClose = '
nohost (Web Browser Server)
'; @@ -103,11 +90,11 @@ function formatDir(route, dirPath, entries) { Index of ${dirPath}

Index of ${dirPath}

- +
[ICO]
- + `; const footer = `
[ICO] NameLast modified SizeDescription

[DIR]
[DIR] Parent Directory  -  

${footerClose}`; @@ -118,18 +105,19 @@ function formatDir(route, dirPath, entries) { let icon; let alt; + // TODO: switch this to entry.isDirectory() if possible if (entry.type === 'DIRECTORY') { - icon = iconFolder; + icon = getDataUri('folder'); alt = '[DIR]'; } else { if (isImage(ext)) { - icon = iconImage; + icon = getDataUri('image2'); alt = '[IMG]'; } else if (isMedia(ext)) { - icon = iconMovie; + icon = getDataUri('movie'); alt = '[MOV]'; } else { - icon = iconText; + icon = getDataUri('text'); alt = '[TXT]'; } }