diff options
author | Chocobozzz <me@florianbigard.com> | 2023-06-06 15:59:51 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2023-06-29 10:19:33 +0200 |
commit | f162d32da098aa55f6de2367142faa166edb7c08 (patch) | |
tree | 31c6a96972994171853cb6c4e0b88b63241f8979 /server/lib/files-cache/videos-torrent-cache.ts | |
parent | a673d9e848e51186602548a621e05925663b98be (diff) | |
download | PeerTube-f162d32da098aa55f6de2367142faa166edb7c08.tar.gz PeerTube-f162d32da098aa55f6de2367142faa166edb7c08.tar.zst PeerTube-f162d32da098aa55f6de2367142faa166edb7c08.zip |
Support lazy download thumbnails
Diffstat (limited to 'server/lib/files-cache/videos-torrent-cache.ts')
-rw-r--r-- | server/lib/files-cache/videos-torrent-cache.ts | 70 |
1 files changed, 0 insertions, 70 deletions
diff --git a/server/lib/files-cache/videos-torrent-cache.ts b/server/lib/files-cache/videos-torrent-cache.ts deleted file mode 100644 index a6bf98dd4..000000000 --- a/server/lib/files-cache/videos-torrent-cache.ts +++ /dev/null | |||
@@ -1,70 +0,0 @@ | |||
1 | import { join } from 'path' | ||
2 | import { logger } from '@server/helpers/logger' | ||
3 | import { doRequestAndSaveToFile } from '@server/helpers/requests' | ||
4 | import { VideoFileModel } from '@server/models/video/video-file' | ||
5 | import { MVideo, MVideoFile } from '@server/types/models' | ||
6 | import { CONFIG } from '../../initializers/config' | ||
7 | import { FILES_CACHE } from '../../initializers/constants' | ||
8 | import { VideoModel } from '../../models/video/video' | ||
9 | import { AbstractVideoStaticFileCache } from './abstract-video-static-file-cache' | ||
10 | |||
11 | class VideosTorrentCache extends AbstractVideoStaticFileCache <string> { | ||
12 | |||
13 | private static instance: VideosTorrentCache | ||
14 | |||
15 | private constructor () { | ||
16 | super() | ||
17 | } | ||
18 | |||
19 | static get Instance () { | ||
20 | return this.instance || (this.instance = new this()) | ||
21 | } | ||
22 | |||
23 | async getFilePathImpl (filename: string) { | ||
24 | const file = await VideoFileModel.loadWithVideoOrPlaylistByTorrentFilename(filename) | ||
25 | if (!file) return undefined | ||
26 | |||
27 | if (file.getVideo().isOwned()) { | ||
28 | const downloadName = this.buildDownloadName(file.getVideo(), file) | ||
29 | |||
30 | return { isOwned: true, path: join(CONFIG.STORAGE.TORRENTS_DIR, file.torrentFilename), downloadName } | ||
31 | } | ||
32 | |||
33 | return this.loadRemoteFile(filename) | ||
34 | } | ||
35 | |||
36 | // Key is the torrent filename | ||
37 | protected async loadRemoteFile (key: string) { | ||
38 | const file = await VideoFileModel.loadWithVideoOrPlaylistByTorrentFilename(key) | ||
39 | if (!file) return undefined | ||
40 | |||
41 | if (file.getVideo().isOwned()) throw new Error('Cannot load remote file of owned video.') | ||
42 | |||
43 | // Used to fetch the path | ||
44 | const video = await VideoModel.loadFull(file.getVideo().id) | ||
45 | if (!video) return undefined | ||
46 | |||
47 | const remoteUrl = file.getRemoteTorrentUrl(video) | ||
48 | const destPath = join(FILES_CACHE.TORRENTS.DIRECTORY, file.torrentFilename) | ||
49 | |||
50 | try { | ||
51 | await doRequestAndSaveToFile(remoteUrl, destPath) | ||
52 | |||
53 | const downloadName = this.buildDownloadName(video, file) | ||
54 | |||
55 | return { isOwned: false, path: destPath, downloadName } | ||
56 | } catch (err) { | ||
57 | logger.info('Cannot fetch remote torrent file %s.', remoteUrl, { err }) | ||
58 | |||
59 | return undefined | ||
60 | } | ||
61 | } | ||
62 | |||
63 | private buildDownloadName (video: MVideo, file: MVideoFile) { | ||
64 | return `${video.name}-${file.resolution}p.torrent` | ||
65 | } | ||
66 | } | ||
67 | |||
68 | export { | ||
69 | VideosTorrentCache | ||
70 | } | ||