From f3b3373a975c3988abd1bb7b325225866068d13e Mon Sep 17 00:00:00 2001 From: Jesse Chan Date: Tue, 24 Nov 2020 19:53:33 +0800 Subject: [PATCH] server: torrents: allow contents to be viewed inline --- package-lock.json | 14 ++++++++++++++ package.json | 2 ++ server/routes/api/torrents.ts | 14 ++++++++++++-- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 886282e1..fd16350c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -27,6 +27,7 @@ "@types/classnames": "^2.2.11", "@types/clipboard": "^2.0.1", "@types/compression": "^1.7.0", + "@types/content-disposition": "^0.5.3", "@types/cookie-parser": "^1.4.2", "@types/create-torrent": "^4.4.0", "@types/d3": "^6.1.0", @@ -69,6 +70,7 @@ "classnames": "^2.2.6", "clipboard": "^2.0.6", "compression": "^1.7.4", + "content-disposition": "^0.5.3", "cookie-parser": "^1.4.5", "create-torrent": "^4.4.2", "css-loader": "^5.0.1", @@ -2357,6 +2359,12 @@ "@types/node": "*" } }, + "node_modules/@types/content-disposition": { + "version": "0.5.3", + "resolved": "https://registry.npmjs.org/@types/content-disposition/-/content-disposition-0.5.3.tgz", + "integrity": "sha512-P1bffQfhD3O4LW0ioENXUhZ9OIa0Zn+P7M+pWgkCKaT53wVLSq0mrKksCID/FGHpFhRSxRGhgrQmfhRuzwtKdg==", + "dev": true + }, "node_modules/@types/cookie-parser": { "version": "1.4.2", "resolved": "https://registry.npmjs.org/@types/cookie-parser/-/cookie-parser-1.4.2.tgz", @@ -28182,6 +28190,12 @@ "@types/node": "*" } }, + "@types/content-disposition": { + "version": "0.5.3", + "resolved": "https://registry.npmjs.org/@types/content-disposition/-/content-disposition-0.5.3.tgz", + "integrity": "sha512-P1bffQfhD3O4LW0ioENXUhZ9OIa0Zn+P7M+pWgkCKaT53wVLSq0mrKksCID/FGHpFhRSxRGhgrQmfhRuzwtKdg==", + "dev": true + }, "@types/cookie-parser": { "version": "1.4.2", "resolved": "https://registry.npmjs.org/@types/cookie-parser/-/cookie-parser-1.4.2.tgz", diff --git a/package.json b/package.json index 3b6736b6..aa4c0be2 100644 --- a/package.json +++ b/package.json @@ -76,6 +76,7 @@ "@types/classnames": "^2.2.11", "@types/clipboard": "^2.0.1", "@types/compression": "^1.7.0", + "@types/content-disposition": "^0.5.3", "@types/cookie-parser": "^1.4.2", "@types/create-torrent": "^4.4.0", "@types/d3": "^6.1.0", @@ -118,6 +119,7 @@ "classnames": "^2.2.6", "clipboard": "^2.0.6", "compression": "^1.7.4", + "content-disposition": "^0.5.3", "cookie-parser": "^1.4.5", "create-torrent": "^4.4.2", "css-loader": "^5.0.1", diff --git a/server/routes/api/torrents.ts b/server/routes/api/torrents.ts index 041a2f90..c0c5613b 100644 --- a/server/routes/api/torrents.ts +++ b/server/routes/api/torrents.ts @@ -1,4 +1,5 @@ import childProcess from 'child_process'; +import contentDisposition from 'content-disposition'; import createTorrent from 'create-torrent'; import express from 'express'; import fs from 'fs'; @@ -555,8 +556,17 @@ router.get('/:hash/contents/:indices/data', (req, res) => { const file = filePathsToDownload[0]; if (!fs.existsSync(file)) return res.status(404).json({error: 'File not found.'}); - res.attachment(path.basename(file)); - return res.download(file); + const filename = path.basename(file); + + // Browsers don't support MKV streaming. However, browsers do support WebM which is a + // subset of MKV. Chromium supports MKV when encoded in selected codecs. + res.type(filename.endsWith('.mkv') ? 'video/webm' : filename); + + // Allow browsers to display the content inline when only a single content is requested. + // This is useful for texts, videos and audios. Users can still download them if needed. + res.setHeader('content-disposition', contentDisposition(filename, {type: 'inline'})); + + return res.sendFile(file); } const archiveRootFolder = sanitizePath(selectedTorrent.directory);