aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/webtorrent.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2022-10-26 10:55:12 +0200
committerChocobozzz <me@florianbigard.com>2022-10-26 10:55:12 +0200
commit44df702502ff5defc42b262b15507f6d88aacc50 (patch)
tree0623d2a0c6402006f5c1208f9381a871b6a1d53b /server/helpers/webtorrent.ts
parentce3121efebfd4e97b2fddbf2f3f79ddcfcb94004 (diff)
downloadPeerTube-44df702502ff5defc42b262b15507f6d88aacc50.tar.gz
PeerTube-44df702502ff5defc42b262b15507f6d88aacc50.tar.zst
PeerTube-44df702502ff5defc42b262b15507f6d88aacc50.zip
More robust updateTorrentMetadata
Diffstat (limited to 'server/helpers/webtorrent.ts')
-rw-r--r--server/helpers/webtorrent.ts9
1 files changed, 7 insertions, 2 deletions
diff --git a/server/helpers/webtorrent.ts b/server/helpers/webtorrent.ts
index b458e86d2..a3c93e6fe 100644
--- a/server/helpers/webtorrent.ts
+++ b/server/helpers/webtorrent.ts
@@ -1,6 +1,6 @@
1import { decode, encode } from 'bencode' 1import { decode, encode } from 'bencode'
2import createTorrent from 'create-torrent' 2import createTorrent from 'create-torrent'
3import { createWriteStream, ensureDir, readFile, remove, writeFile } from 'fs-extra' 3import { createWriteStream, ensureDir, pathExists, readFile, remove, writeFile } from 'fs-extra'
4import magnetUtil from 'magnet-uri' 4import magnetUtil from 'magnet-uri'
5import parseTorrent from 'parse-torrent' 5import parseTorrent from 'parse-torrent'
6import { dirname, join } from 'path' 6import { dirname, join } from 'path'
@@ -134,6 +134,11 @@ async function updateTorrentMetadata (videoOrPlaylist: MVideo | MStreamingPlayli
134 134
135 const oldTorrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, videoFile.torrentFilename) 135 const oldTorrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, videoFile.torrentFilename)
136 136
137 if (!await pathExists(oldTorrentPath)) {
138 logger.info('Do not update torrent metadata %s of video %s because the file does not exist anymore.', video.uuid, oldTorrentPath)
139 return
140 }
141
137 const torrentContent = await readFile(oldTorrentPath) 142 const torrentContent = await readFile(oldTorrentPath)
138 const decoded = decode(torrentContent) 143 const decoded = decode(torrentContent)
139 144
@@ -151,7 +156,7 @@ async function updateTorrentMetadata (videoOrPlaylist: MVideo | MStreamingPlayli
151 logger.info('Updating torrent metadata %s -> %s.', oldTorrentPath, newTorrentPath) 156 logger.info('Updating torrent metadata %s -> %s.', oldTorrentPath, newTorrentPath)
152 157
153 await writeFile(newTorrentPath, encode(decoded)) 158 await writeFile(newTorrentPath, encode(decoded))
154 await remove(join(CONFIG.STORAGE.TORRENTS_DIR, videoFile.torrentFilename)) 159 await remove(oldTorrentPath)
155 160
156 videoFile.torrentFilename = newTorrentFilename 161 videoFile.torrentFilename = newTorrentFilename
157 videoFile.infoHash = sha1(encode(decoded.info)) 162 videoFile.infoHash = sha1(encode(decoded.info))