Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix torrent download extension override #539

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const DirectoryFiles: FC<DirectoryFilesProps> = ({depth, items, hash, path, onIt
<div className="file__name">
{/* TODO: Add a WebAssembly decoding player if the feature is popular */}
<a
href={`${ConfigStore.baseURI}api/torrents/${hash}/contents/${file.index}/data`}
href={`${ConfigStore.baseURI}api/torrents/${hash}/contents/${file.index}/data?streaming`}
style={{textDecoration: 'none'}}
target="_blank"
rel="noreferrer"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,29 +143,17 @@ const TorrentContents: FC = observer(() => {
}}
/>
</FormRowItem>
<Button
onClick={(event) => {
event.preventDefault();
const {baseURI} = ConfigStore;
const link = document.createElement('a');

link.download = '';
link.href = `${baseURI}api/torrents/${hash}/contents/${selectedIndices.join(',')}/data`;
link.style.display = 'none';

document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}}
grow={false}
shrink={false}
>
<Trans
id="torrents.details.files.download.file"
values={{
count: selectedIndices.length,
}}
/>
<Button>
<a
href={`${ConfigStore.baseURI}api/torrents/${hash}/contents/${selectedIndices.join(',')}/data`}
style={{textDecoration: 'none'}}
download
>
<Trans
id="torrents.details.files.download.file"
values={{count: selectedIndices.length}}
/>
</a>
</Button>
<Select id="file-priority" persistentPlaceholder shrink={false} defaultID="">
<SelectItem id={-1} isPlaceholder>
Expand Down
57 changes: 27 additions & 30 deletions server/routes/api/torrents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import contentDisposition from 'content-disposition';
import createTorrent from 'create-torrent';
import express, {Response} from 'express';
import fs from 'fs';
import qs from 'qs';
import path from 'path';
import rateLimit from 'express-rate-limit';
import sanitize from 'sanitize-filename';
Expand Down Expand Up @@ -692,9 +693,10 @@ router.get<{hash: string; indices: string}, unknown, unknown, {token: string}>(
* @security User
* @param {string} hash.path
* @param {string} indices.path - 'all' or indices of selected contents separated by ','
* @return {object} 200 - contents archived in .tar - application/x-tar
* @param {string} streaming - if present with a single index will try to make browsers display the data
* @return {object} 200 - file data for a single index or multiple files archived in .tar - application/x-tar
*/
router.get<{hash: string; indices: string}, unknown, unknown, {token: string}>(
router.get<{hash: string; indices: string}, unknown, unknown, {token: string; streaming?: string}>(
'/:hash/contents/:indices/data',
// This operation is resource-intensive
// Limit each IP to 200 requests every 5 minutes
Expand All @@ -706,17 +708,13 @@ router.get<{hash: string; indices: string}, unknown, unknown, {token: string}>(
const {hash, indices: stringIndices} = req.params;

if (req.user != null && req.query.token == null) {
// https://bugzilla.mozilla.org/show_bug.cgi?id=1689018
if (req.headers?.['user-agent']?.includes('Firefox/') !== true) {
res.redirect(
`?token=${getToken<ContentToken>({
username: req.user.username,
hash,
indices: stringIndices,
})}`,
);
return res;
}
const token = getToken<ContentToken>({
username: req.user.username,
hash,
indices: stringIndices,
});
res.redirect('?' + qs.stringify({...req.query, token}));
return res;
}

const selectedTorrent = req.services.torrentService.getTorrent(hash);
Expand Down Expand Up @@ -763,26 +761,25 @@ router.get<{hash: string; indices: string}, unknown, unknown, {token: string}>(
const fileExt = path.extname(file);

let processedType: string = fileExt;
switch (fileExt) {
// 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.
case '.mkv':
processedType = 'video/webm';
break;
// MIME database uses x-flac which is not recognized by browsers as streamable audio.
case '.flac':
processedType = 'audio/flac';
break;
default:
break;
let contentDispositionType = 'attachment';

if (req.query.streaming !== undefined) {
processedType =
{
// 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.
'.mkv': 'video/webm',
// MIME database uses x-flac which is not recognized by browsers as streamable audio.
'.flac': 'audio/flac',
}[fileExt] ?? fileExt;

// 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.
contentDispositionType = 'inline';
}

res.type(processedType);

// 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'}));

res.setHeader('content-disposition', contentDisposition(fileName, {type: contentDispositionType}));
res.sendFile(file);

return res;
Expand Down