]>
Commit | Line | Data |
---|---|---|
3fd3ab2d | 1 | import { join } from 'path' |
a1587156 | 2 | import { FILES_CACHE } from '../../initializers/constants' |
3fd3ab2d | 3 | import { VideoModel } from '../../models/video/video' |
40e87e9e | 4 | import { AbstractVideoStaticFileCache } from './abstract-video-static-file-cache' |
ca6d3622 | 5 | import { doRequestAndSaveToFile } from '@server/helpers/requests' |
f981dae8 | 6 | |
40e87e9e | 7 | class VideosPreviewCache extends AbstractVideoStaticFileCache <string> { |
f981dae8 C |
8 | |
9 | private static instance: VideosPreviewCache | |
10 | ||
40e87e9e C |
11 | private constructor () { |
12 | super() | |
13 | } | |
f981dae8 C |
14 | |
15 | static get Instance () { | |
16 | return this.instance || (this.instance = new this()) | |
17 | } | |
18 | ||
e8bafea3 | 19 | async getFilePathImpl (videoUUID: string) { |
e2600d8b | 20 | const video = await VideoModel.loadByUUID(videoUUID) |
8fa5653a C |
21 | if (!video) return undefined |
22 | ||
536598cf | 23 | if (video.isOwned()) return { isOwned: true, path: video.getPreview().getPath() } |
8fa5653a | 24 | |
e8bafea3 | 25 | return this.loadRemoteFile(videoUUID) |
f981dae8 C |
26 | } |
27 | ||
40e87e9e | 28 | protected async loadRemoteFile (key: string) { |
627621c1 | 29 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(key) |
f5028693 | 30 | if (!video) return undefined |
f981dae8 | 31 | |
40e87e9e | 32 | if (video.isOwned()) throw new Error('Cannot load remote preview of owned video.') |
f981dae8 | 33 | |
ca6d3622 C |
34 | const preview = video.getPreview() |
35 | const destPath = join(FILES_CACHE.PREVIEWS.DIRECTORY, preview.filename) | |
f981dae8 | 36 | |
ca6d3622 C |
37 | const remoteUrl = preview.getFileUrl(video) |
38 | await doRequestAndSaveToFile({ uri: remoteUrl }, destPath) | |
3acc5084 | 39 | |
dc852737 | 40 | return { isOwned: false, path: destPath } |
f981dae8 C |
41 | } |
42 | } | |
43 | ||
44 | export { | |
45 | VideosPreviewCache | |
46 | } |