Skip to content

ActivityPub-federated video streaming platform using P2P directly in your web browser

More robust torrent download

Chocobozzz3229ccea33e2
diff --git a/packages/tests/src/plugins/plugin-helpers.ts b/packages/tests/src/plugins/plugin-helpers.ts
index 9c5ff9ddc6..5f4f5da3d5 100644
--- a/packages/tests/src/plugins/plugin-helpers.ts
+++ b/packages/tests/src/plugins/plugin-helpers.ts
@@ -326,10 +326,8 @@ describe('Test plugin helpers', function () {
expect(await pathExists(miniature.path)).to.be.true
await makeRawRequest({ url: miniature.url, expectedStatus: HttpStatusCode.OK_200 })
- const preview = body.thumbnails.find(t => t.type === 2)
+ const preview = body.thumbnails.find(t => t.type === 2 && t.width === 850 && t.height === 480)
expect(preview).to.exist
- expect(preview.width).to.equal(850)
- expect(preview.height).to.equal(480)
expect(await pathExists(preview.path)).to.be.true
await makeRawRequest({ url: preview.url, expectedStatus: HttpStatusCode.OK_200 })
}
diff --git a/server/core/controllers/api/videos/import.ts b/server/core/controllers/api/videos/import.ts
index 6a23339b14..9e37fc87b6 100644
--- a/server/core/controllers/api/videos/import.ts
+++ b/server/core/controllers/api/videos/import.ts
@@ -242,9 +242,7 @@ async function processTorrentOrAbortRequest (req: express.Request, res: express.
await move(torrentfile.path, newTorrentPath, { overwrite: true })
torrentfile.path = newTorrentPath
- const buf = await readFile(torrentfile.path)
- // FIXME: typings: parseTorrent now returns an async result
- const parsedTorrent = await (parseTorrent(buf) as unknown as Promise<Instance>)
+ const parsedTorrent = await parseTorrentPromise(torrentfile.path)
if (parsedTorrent.files.length !== 1) {
cleanUpReqFiles(req)
@@ -275,3 +273,9 @@ function processMagnetURI (body: VideoImportCreate) {
function extractNameFromArray (name: string | string[]) {
return isArray(name) ? name[0] : name
}
+
+async function parseTorrentPromise (torrentFilePath: string) {
+ const buf = await readFile(torrentFilePath)
+
+ return parseTorrent(buf) as Instance
+}
diff --git a/server/core/lib/webtorrent.ts b/server/core/lib/webtorrent.ts
index bd7c35bcd1..ec5b99507e 100644
--- a/server/core/lib/webtorrent.ts
+++ b/server/core/lib/webtorrent.ts
@@ -40,13 +40,20 @@ export async function downloadWebTorrentVideo (target: { uri: string, torrentNam
uploadLimit: 5_000_000
} as any)
- return new Promise<string>((res, rej) => {
+ return new Promise<string>(async (res, rej) => {
let file: TorrentFile
const torrentId = target.uri || join(CONFIG.STORAGE.TORRENTS_DIR, target.torrentName)
+ let torrentInput: string | Buffer
+
+ try {
+ torrentInput = target.uri || await readFile(torrentId)
+ } catch (err) {
+ return rej(new Error('Cannot read torrent file ' + torrentId + ': ' + (err as Error).message))
+ }
const options = { path: directoryPath }
- const torrent = webtorrent.add(torrentId, options, torrent => {
+ const torrent = webtorrent.add(torrentInput, options, torrent => {
if (torrent.files.length !== 1) {
if (timer) clearTimeout(timer)