diff options
author | Chocobozzz <me@florianbigard.com> | 2019-03-19 14:23:17 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2019-03-19 14:30:43 +0100 |
commit | d74d29ad9e35929491cf37223398d2535ab23de0 (patch) | |
tree | 2812c9acbc05be0603eb671f8e6bd81086cf84d5 /server/lib/files-cache/videos-caption-cache.ts | |
parent | 9f79ade627f0044606a9fbbe16ca0154661d12b9 (diff) | |
download | PeerTube-d74d29ad9e35929491cf37223398d2535ab23de0.tar.gz PeerTube-d74d29ad9e35929491cf37223398d2535ab23de0.tar.zst PeerTube-d74d29ad9e35929491cf37223398d2535ab23de0.zip |
Limit user tokens cache
Diffstat (limited to 'server/lib/files-cache/videos-caption-cache.ts')
-rw-r--r-- | server/lib/files-cache/videos-caption-cache.ts | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/server/lib/files-cache/videos-caption-cache.ts b/server/lib/files-cache/videos-caption-cache.ts new file mode 100644 index 000000000..fe5b441af --- /dev/null +++ b/server/lib/files-cache/videos-caption-cache.ts | |||
@@ -0,0 +1,53 @@ | |||
1 | import { join } from 'path' | ||
2 | import { FILES_CACHE, CONFIG } from '../../initializers' | ||
3 | import { VideoModel } from '../../models/video/video' | ||
4 | import { VideoCaptionModel } from '../../models/video/video-caption' | ||
5 | import { AbstractVideoStaticFileCache } from './abstract-video-static-file-cache' | ||
6 | |||
7 | type GetPathParam = { videoId: string, language: string } | ||
8 | |||
9 | class VideosCaptionCache extends AbstractVideoStaticFileCache <GetPathParam> { | ||
10 | |||
11 | private static readonly KEY_DELIMITER = '%' | ||
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 getFilePath (params: GetPathParam) { | ||
23 | const videoCaption = await VideoCaptionModel.loadByVideoIdAndLanguage(params.videoId, params.language) | ||
24 | if (!videoCaption) return undefined | ||
25 | |||
26 | if (videoCaption.isOwned()) return join(CONFIG.STORAGE.CAPTIONS_DIR, videoCaption.getCaptionName()) | ||
27 | |||
28 | const key = params.videoId + VideosCaptionCache.KEY_DELIMITER + params.language | ||
29 | return this.loadFromLRU(key) | ||
30 | } | ||
31 | |||
32 | protected async loadRemoteFile (key: string) { | ||
33 | const [ videoId, language ] = key.split(VideosCaptionCache.KEY_DELIMITER) | ||
34 | |||
35 | const videoCaption = await VideoCaptionModel.loadByVideoIdAndLanguage(videoId, language) | ||
36 | if (!videoCaption) return undefined | ||
37 | |||
38 | if (videoCaption.isOwned()) throw new Error('Cannot load remote caption of owned video.') | ||
39 | |||
40 | // Used to fetch the path | ||
41 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoId) | ||
42 | if (!video) return undefined | ||
43 | |||
44 | const remoteStaticPath = videoCaption.getCaptionStaticPath() | ||
45 | const destPath = join(FILES_CACHE.VIDEO_CAPTIONS.DIRECTORY, videoCaption.getCaptionName()) | ||
46 | |||
47 | return this.saveRemoteVideoFileAndReturnPath(video, remoteStaticPath, destPath) | ||
48 | } | ||
49 | } | ||
50 | |||
51 | export { | ||
52 | VideosCaptionCache | ||
53 | } | ||