aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/files-cache/videos-caption-cache.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2023-06-06 15:59:51 +0200
committerChocobozzz <me@florianbigard.com>2023-06-29 10:19:33 +0200
commitf162d32da098aa55f6de2367142faa166edb7c08 (patch)
tree31c6a96972994171853cb6c4e0b88b63241f8979 /server/lib/files-cache/videos-caption-cache.ts
parenta673d9e848e51186602548a621e05925663b98be (diff)
downloadPeerTube-f162d32da098aa55f6de2367142faa166edb7c08.tar.gz
PeerTube-f162d32da098aa55f6de2367142faa166edb7c08.tar.zst
PeerTube-f162d32da098aa55f6de2367142faa166edb7c08.zip
Support lazy download thumbnails
Diffstat (limited to 'server/lib/files-cache/videos-caption-cache.ts')
-rw-r--r--server/lib/files-cache/videos-caption-cache.ts59
1 files changed, 0 insertions, 59 deletions
diff --git a/server/lib/files-cache/videos-caption-cache.ts b/server/lib/files-cache/videos-caption-cache.ts
deleted file mode 100644
index d21acf4ef..000000000
--- a/server/lib/files-cache/videos-caption-cache.ts
+++ /dev/null
@@ -1,59 +0,0 @@
1import { join } from 'path'
2import { logger } from '@server/helpers/logger'
3import { doRequestAndSaveToFile } from '@server/helpers/requests'
4import { CONFIG } from '../../initializers/config'
5import { FILES_CACHE } from '../../initializers/constants'
6import { VideoModel } from '../../models/video/video'
7import { VideoCaptionModel } from '../../models/video/video-caption'
8import { AbstractVideoStaticFileCache } from './abstract-video-static-file-cache'
9
10class VideosCaptionCache extends AbstractVideoStaticFileCache <string> {
11
12 private static instance: VideosCaptionCache
13
14 private constructor () {
15 super()
16 }
17
18 static get Instance () {
19 return this.instance || (this.instance = new this())
20 }
21
22 async getFilePathImpl (filename: string) {
23 const videoCaption = await VideoCaptionModel.loadWithVideoByFilename(filename)
24 if (!videoCaption) return undefined
25
26 if (videoCaption.isOwned()) return { isOwned: true, path: join(CONFIG.STORAGE.CAPTIONS_DIR, videoCaption.filename) }
27
28 return this.loadRemoteFile(filename)
29 }
30
31 // Key is the caption filename
32 protected async loadRemoteFile (key: string) {
33 const videoCaption = await VideoCaptionModel.loadWithVideoByFilename(key)
34 if (!videoCaption) return undefined
35
36 if (videoCaption.isOwned()) throw new Error('Cannot load remote caption of owned video.')
37
38 // Used to fetch the path
39 const video = await VideoModel.loadFull(videoCaption.videoId)
40 if (!video) return undefined
41
42 const remoteUrl = videoCaption.getFileUrl(video)
43 const destPath = join(FILES_CACHE.VIDEO_CAPTIONS.DIRECTORY, videoCaption.filename)
44
45 try {
46 await doRequestAndSaveToFile(remoteUrl, destPath)
47
48 return { isOwned: false, path: destPath }
49 } catch (err) {
50 logger.info('Cannot fetch remote caption file %s.', remoteUrl, { err })
51
52 return undefined
53 }
54 }
55}
56
57export {
58 VideosCaptionCache
59}