X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Ffiles-cache%2Fvideos-torrent-cache.ts;h=a6bf98dd48daaa8b5a12faad5dac088591a55dd0;hb=785471d3e681117b1b33285f04ecc7933055f75d;hp=881fa9cedf374b16cab927e7b423a17cc41e3726;hpb=db4b15f21fbf4e33434e930ffc7fb768cdcf9d42;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/files-cache/videos-torrent-cache.ts b/server/lib/files-cache/videos-torrent-cache.ts index 881fa9ced..a6bf98dd4 100644 --- a/server/lib/files-cache/videos-torrent-cache.ts +++ b/server/lib/files-cache/videos-torrent-cache.ts @@ -1,6 +1,8 @@ import { join } from 'path' +import { logger } from '@server/helpers/logger' import { doRequestAndSaveToFile } from '@server/helpers/requests' import { VideoFileModel } from '@server/models/video/video-file' +import { MVideo, MVideoFile } from '@server/types/models' import { CONFIG } from '../../initializers/config' import { FILES_CACHE } from '../../initializers/constants' import { VideoModel } from '../../models/video/video' @@ -22,7 +24,11 @@ class VideosTorrentCache extends AbstractVideoStaticFileCache { const file = await VideoFileModel.loadWithVideoOrPlaylistByTorrentFilename(filename) if (!file) return undefined - if (file.getVideo().isOwned()) return { isOwned: true, path: join(CONFIG.STORAGE.TORRENTS_DIR, file.torrentFilename) } + if (file.getVideo().isOwned()) { + const downloadName = this.buildDownloadName(file.getVideo(), file) + + return { isOwned: true, path: join(CONFIG.STORAGE.TORRENTS_DIR, file.torrentFilename), downloadName } + } return this.loadRemoteFile(filename) } @@ -35,17 +41,27 @@ class VideosTorrentCache extends AbstractVideoStaticFileCache { if (file.getVideo().isOwned()) throw new Error('Cannot load remote file of owned video.') // Used to fetch the path - const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(file.getVideo().id) + const video = await VideoModel.loadFull(file.getVideo().id) if (!video) return undefined const remoteUrl = file.getRemoteTorrentUrl(video) const destPath = join(FILES_CACHE.TORRENTS.DIRECTORY, file.torrentFilename) - await doRequestAndSaveToFile(remoteUrl, destPath) + try { + await doRequestAndSaveToFile(remoteUrl, destPath) + + const downloadName = this.buildDownloadName(video, file) - const downloadName = `${video.name}-${file.resolution}p.torrent` + return { isOwned: false, path: destPath, downloadName } + } catch (err) { + logger.info('Cannot fetch remote torrent file %s.', remoteUrl, { err }) + + return undefined + } + } - return { isOwned: false, path: destPath, downloadName } + private buildDownloadName (video: MVideo, file: MVideoFile) { + return `${video.name}-${file.resolution}p.torrent` } }