diff options
author | Chocobozzz <me@florianbigard.com> | 2018-07-12 19:02:00 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-07-16 11:50:08 +0200 |
commit | 40e87e9ecc54e3513fb586928330a7855eb192c6 (patch) | |
tree | af1111ecba85f9cd8286811ff332a67cf21be2f6 /server/lib/cache/videos-preview-cache.ts | |
parent | d4557fd3ecc8d4ed4fb0e5c868929bc36c959ed2 (diff) | |
download | PeerTube-40e87e9ecc54e3513fb586928330a7855eb192c6.tar.gz PeerTube-40e87e9ecc54e3513fb586928330a7855eb192c6.tar.zst PeerTube-40e87e9ecc54e3513fb586928330a7855eb192c6.zip |
Implement captions/subtitles
Diffstat (limited to 'server/lib/cache/videos-preview-cache.ts')
-rw-r--r-- | server/lib/cache/videos-preview-cache.ts | 60 |
1 files changed, 14 insertions, 46 deletions
diff --git a/server/lib/cache/videos-preview-cache.ts b/server/lib/cache/videos-preview-cache.ts index d09d55e11..1c0e7ed9d 100644 --- a/server/lib/cache/videos-preview-cache.ts +++ b/server/lib/cache/videos-preview-cache.ts | |||
@@ -1,71 +1,39 @@ | |||
1 | import * as asyncLRU from 'async-lru' | ||
2 | import { createWriteStream } from 'fs' | ||
3 | import { join } from 'path' | 1 | import { join } from 'path' |
4 | import { unlinkPromise } from '../../helpers/core-utils' | 2 | import { CACHE, CONFIG, STATIC_PATHS } from '../../initializers' |
5 | import { logger } from '../../helpers/logger' | ||
6 | import { CACHE, CONFIG } from '../../initializers' | ||
7 | import { VideoModel } from '../../models/video/video' | 3 | import { VideoModel } from '../../models/video/video' |
8 | import { fetchRemoteVideoPreview } from '../activitypub' | 4 | import { AbstractVideoStaticFileCache } from './abstract-video-static-file-cache' |
9 | 5 | ||
10 | class VideosPreviewCache { | 6 | class VideosPreviewCache extends AbstractVideoStaticFileCache <string> { |
11 | 7 | ||
12 | private static instance: VideosPreviewCache | 8 | private static instance: VideosPreviewCache |
13 | 9 | ||
14 | private lru | 10 | private constructor () { |
15 | 11 | super() | |
16 | private constructor () { } | 12 | } |
17 | 13 | ||
18 | static get Instance () { | 14 | static get Instance () { |
19 | return this.instance || (this.instance = new this()) | 15 | return this.instance || (this.instance = new this()) |
20 | } | 16 | } |
21 | 17 | ||
22 | init (max: number) { | 18 | async getFilePath (videoUUID: string) { |
23 | this.lru = new asyncLRU({ | 19 | const video = await VideoModel.loadByUUID(videoUUID) |
24 | max, | ||
25 | load: (key, cb) => { | ||
26 | this.loadPreviews(key) | ||
27 | .then(res => cb(null, res)) | ||
28 | .catch(err => cb(err)) | ||
29 | } | ||
30 | }) | ||
31 | |||
32 | this.lru.on('evict', (obj: { key: string, value: string }) => { | ||
33 | unlinkPromise(obj.value).then(() => logger.debug('%s evicted from VideosPreviewCache', obj.value)) | ||
34 | }) | ||
35 | } | ||
36 | |||
37 | async getPreviewPath (key: string) { | ||
38 | const video = await VideoModel.loadByUUID(key) | ||
39 | if (!video) return undefined | 20 | if (!video) return undefined |
40 | 21 | ||
41 | if (video.isOwned()) return join(CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName()) | 22 | if (video.isOwned()) return join(CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName()) |
42 | 23 | ||
43 | return new Promise<string>((res, rej) => { | 24 | return this.loadFromLRU(videoUUID) |
44 | this.lru.get(key, (err, value) => { | ||
45 | err ? rej(err) : res(value) | ||
46 | }) | ||
47 | }) | ||
48 | } | 25 | } |
49 | 26 | ||
50 | private async loadPreviews (key: string) { | 27 | protected async loadRemoteFile (key: string) { |
51 | const video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(key) | 28 | const video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(key) |
52 | if (!video) return undefined | 29 | if (!video) return undefined |
53 | 30 | ||
54 | if (video.isOwned()) throw new Error('Cannot load preview of owned video.') | 31 | if (video.isOwned()) throw new Error('Cannot load remote preview of owned video.') |
55 | |||
56 | return this.saveRemotePreviewAndReturnPath(video) | ||
57 | } | ||
58 | 32 | ||
59 | private saveRemotePreviewAndReturnPath (video: VideoModel) { | 33 | const remoteStaticPath = join(STATIC_PATHS.PREVIEWS, video.getPreviewName()) |
60 | return new Promise<string>((res, rej) => { | 34 | const destPath = join(CACHE.DIRECTORIES.PREVIEWS, video.getPreviewName()) |
61 | const req = fetchRemoteVideoPreview(video, rej) | ||
62 | const path = join(CACHE.DIRECTORIES.PREVIEWS, video.getPreviewName()) | ||
63 | const stream = createWriteStream(path) | ||
64 | 35 | ||
65 | req.pipe(stream) | 36 | return this.saveRemoteVideoFileAndReturnPath(video, remoteStaticPath, destPath) |
66 | .on('error', (err) => rej(err)) | ||
67 | .on('finish', () => res(path)) | ||
68 | }) | ||
69 | } | 37 | } |
70 | } | 38 | } |
71 | 39 | ||