UNPKG

five-server

Version:

Development Server with Live Reload Capability. (Maintained Fork of Live Server)

109 lines (108 loc) 4.72 kB
"use strict"; /** * @author Yannick Deubel (https://github.com/yandeu) * @copyright Copyright (c) 2021 Yannick Deubel * @license {@link https://github.com/yandeu/five-server/blob/main/LICENSE LICENSE} */ Object.defineProperty(exports, "__esModule", { value: true }); exports.preview = void 0; const public_1 = require("../public"); const path_1 = require("path"); const fs_1 = require("fs"); const fileTypes_1 = require("../fileTypes"); const explorer_1 = require("./explorer"); const preview = (root, baseURL, injectToAny) => { return (req, res, next) => { if (!injectToAny) return next(); if (!['.preview', '.fullscreen', '.php'].includes((0, path_1.extname)(req.url))) return next(); const isFullscreenPreview = /\.fullscreen$/.test(req.url); // remove .preview req.url = req.url.replace(/\.preview$|\.fullscreen$/, ''); const URL = decodeURI(req.url); const isPHP = (0, path_1.extname)(req.url) === '.php'; const phpMsg = isPHP ? 'Why this preview? Five Server could not detect any head, body or html tag in your file.<br><br>' : ''; try { const filePath = (0, path_1.resolve)((0, path_1.join)(root + URL)); const isFile = (0, fs_1.statSync)(filePath).isFile(); if (!isFile) return next(); let ext = (0, path_1.extname)(URL).replace(/^\./, '').toLowerCase(); const fileName = (0, path_1.basename)(filePath, ext); const isImage = fileTypes_1.fileTypes.isImage(ext); const isVideo = fileTypes_1.fileTypes.isVideo(ext); const isAudio = fileTypes_1.fileTypes.isAudio(ext); const isPDF = fileTypes_1.fileTypes.isPDF(ext); let preview = ''; if (isImage) preview = `<div class="image" style="background: white;"> <img style="max-width: 100%;" src="${URL}"></div>`; else if (isVideo) { const format = ext === 'ogg' ? 'ogg' : ext === 'webm' ? 'webm' : 'mp4'; preview = ` <video style="max-width: 100%;" controls> <source src="${URL}" type="video/${format}"> Your browser does not support the video tag. </video>`; } else if (isAudio) { const format = ext === 'ogg' ? 'ogg' : ext === 'wav' ? 'wav' : 'mpeg'; preview = ` <div style="margin-top: 72px;"> <audio controls> <source src="${URL}" type="audio/${format}"> Your browser does not support the audio element. </audio> </div>`; } else if (isPDF) { preview = ` <div> <iframe style="min-height: calc(100vh - 260px)" frameborder="0" scrolling="no" width="100%" height="100%" src="${URL}"> </iframe> </div>`; } else { const MAX_FILE_SIZE = 250; // KB const fileSize = Math.round((0, fs_1.statSync)(filePath).size / 1024); // KB const tooBig = fileSize > MAX_FILE_SIZE; if (tooBig) ext = 'txt'; let fileContent = !tooBig ? (0, fs_1.readFileSync)(filePath, { encoding: 'utf-8' }) : `File is too big for a preview!\n\n\nFile Size: ${fileSize}KB\nAllowed Size: ${MAX_FILE_SIZE}KB`; // check for .rc file (can be yml or json) if (/^\.[\w]+rc$/.test(fileName)) { const content = fileContent.trim(); ext = content[0] === '{' ? 'json' : 'yml'; } // replace all < with &lt; fileContent = fileContent.replace(/</gm, '&lt;'); preview = ` <div> <pre margin="0px;"><code class="">${fileContent}</code></pre> </div>`; } const TEMPLATE = isFullscreenPreview ? public_1.PREVIEW_FULLSCREEN : public_1.PREVIEW; const html = TEMPLATE.replace('{linked-path}', (0, explorer_1.htmlPath)(URL, baseURL)) .replace(/{fileName}/gm, fileName) .replace(/{ext}/gm, ext) .replace('{phpMsg}', phpMsg ? `<div class="message"><p>${phpMsg}</p></div>` : '') .replace('{preview}', preview); return res.type('html').send(html); } catch (error) { return next(); } }; }; exports.preview = preview; //# sourceMappingURL=preview.js.map