]>
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' |
a8b1b404 C |
6 | import { ThumbnailModel } from '@server/models/video/thumbnail' |
7 | import { ThumbnailType } from '@shared/models' | |
8 | import { logger } from '@server/helpers/logger' | |
f981dae8 | 9 | |
40e87e9e | 10 | class VideosPreviewCache extends AbstractVideoStaticFileCache <string> { |
f981dae8 C |
11 | |
12 | private static instance: VideosPreviewCache | |
13 | ||
40e87e9e C |
14 | private constructor () { |
15 | super() | |
16 | } | |
f981dae8 C |
17 | |
18 | static get Instance () { | |
19 | return this.instance || (this.instance = new this()) | |
20 | } | |
21 | ||
a8b1b404 | 22 | async getFilePathImpl (filename: string) { |
a35a2279 | 23 | const thumbnail = await ThumbnailModel.loadWithVideoByFilename(filename, ThumbnailType.PREVIEW) |
a8b1b404 | 24 | if (!thumbnail) return undefined |
8fa5653a | 25 | |
a8b1b404 | 26 | if (thumbnail.Video.isOwned()) return { isOwned: true, path: thumbnail.getPath() } |
8fa5653a | 27 | |
a8b1b404 | 28 | return this.loadRemoteFile(thumbnail.Video.uuid) |
f981dae8 C |
29 | } |
30 | ||
6302d599 | 31 | // Key is the video UUID |
40e87e9e | 32 | protected async loadRemoteFile (key: string) { |
627621c1 | 33 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(key) |
f5028693 | 34 | if (!video) return undefined |
f981dae8 | 35 | |
40e87e9e | 36 | if (video.isOwned()) throw new Error('Cannot load remote preview of owned video.') |
f981dae8 | 37 | |
ca6d3622 C |
38 | const preview = video.getPreview() |
39 | const destPath = join(FILES_CACHE.PREVIEWS.DIRECTORY, preview.filename) | |
f981dae8 | 40 | |
ca6d3622 | 41 | const remoteUrl = preview.getFileUrl(video) |
db4b15f2 | 42 | await doRequestAndSaveToFile(remoteUrl, destPath) |
3acc5084 | 43 | |
a8b1b404 C |
44 | logger.debug('Fetched remote preview %s to %s.', remoteUrl, destPath) |
45 | ||
dc852737 | 46 | return { isOwned: false, path: destPath } |
f981dae8 C |
47 | } |
48 | } | |
49 | ||
50 | export { | |
51 | VideosPreviewCache | |
52 | } |