]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/files-cache/videos-torrent-cache.ts
Upgrade client dependencies
[github/Chocobozzz/PeerTube.git] / server / lib / files-cache / videos-torrent-cache.ts
CommitLineData
90a8bd30
C
1import { join } from 'path'
2import { doRequestAndSaveToFile } from '@server/helpers/requests'
3import { VideoFileModel } from '@server/models/video/video-file'
4import { CONFIG } from '../../initializers/config'
5import { FILES_CACHE } from '../../initializers/constants'
6import { VideoModel } from '../../models/video/video'
7import { AbstractVideoStaticFileCache } from './abstract-video-static-file-cache'
8
9class VideosTorrentCache extends AbstractVideoStaticFileCache <string> {
10
11 private static instance: VideosTorrentCache
12
13 private constructor () {
14 super()
15 }
16
17 static get Instance () {
18 return this.instance || (this.instance = new this())
19 }
20
21 async getFilePathImpl (filename: string) {
22 const file = await VideoFileModel.loadWithVideoOrPlaylistByTorrentFilename(filename)
23 if (!file) return undefined
24
25 if (file.getVideo().isOwned()) return { isOwned: true, path: join(CONFIG.STORAGE.TORRENTS_DIR, file.torrentFilename) }
26
27 return this.loadRemoteFile(filename)
28 }
29
30 // Key is the torrent filename
31 protected async loadRemoteFile (key: string) {
32 const file = await VideoFileModel.loadWithVideoOrPlaylistByTorrentFilename(key)
33 if (!file) return undefined
34
35 if (file.getVideo().isOwned()) throw new Error('Cannot load remote file of owned video.')
36
37 // Used to fetch the path
38 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(file.getVideo().id)
39 if (!video) return undefined
40
41 const remoteUrl = file.getRemoteTorrentUrl(video)
42 const destPath = join(FILES_CACHE.TORRENTS.DIRECTORY, file.torrentFilename)
43
db4b15f2 44 await doRequestAndSaveToFile(remoteUrl, destPath)
90a8bd30
C
45
46 const downloadName = `${video.name}-${file.resolution}p.torrent`
47
48 return { isOwned: false, path: destPath, downloadName }
49 }
50}
51
52export {
53 VideosTorrentCache
54}