]>
Commit | Line | Data |
---|---|---|
1 | import * as asyncLRU from 'async-lru' | |
2 | import { join } from 'path' | |
3 | import { createWriteStream } from 'fs' | |
4 | import * as Promise from 'bluebird' | |
5 | ||
6 | import { database as db, CONFIG, CACHE } from '../../initializers' | |
7 | import { logger, unlinkPromise } from '../../helpers' | |
8 | import { VideoInstance } from '../../models' | |
9 | import { fetchRemotePreview } from '../../lib' | |
10 | ||
11 | class VideosPreviewCache { | |
12 | ||
13 | private static instance: VideosPreviewCache | |
14 | ||
15 | private lru | |
16 | ||
17 | private constructor () { } | |
18 | ||
19 | static get Instance () { | |
20 | return this.instance || (this.instance = new this()) | |
21 | } | |
22 | ||
23 | init (max: number) { | |
24 | this.lru = new asyncLRU({ | |
25 | max, | |
26 | load: (key, cb) => { | |
27 | this.loadPreviews(key) | |
28 | .then(res => cb(null, res)) | |
29 | .catch(err => cb(err)) | |
30 | } | |
31 | }) | |
32 | ||
33 | this.lru.on('evict', (obj: { key: string, value: string }) => { | |
34 | unlinkPromise(obj.value).then(() => logger.debug('%s evicted from VideosPreviewCache', obj.value)) | |
35 | }) | |
36 | } | |
37 | ||
38 | getPreviewPath (key: string) { | |
39 | return new Promise<string>((res, rej) => { | |
40 | this.lru.get(key, (err, value) => { | |
41 | err ? rej(err) : res(value) | |
42 | }) | |
43 | }) | |
44 | } | |
45 | ||
46 | private loadPreviews (key: string) { | |
47 | return db.Video.loadByUUIDAndPopulateAuthorAndPodAndTags(key) | |
48 | .then(video => { | |
49 | if (!video) return undefined | |
50 | ||
51 | if (video.isOwned()) return join(CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName()) | |
52 | ||
53 | return this.saveRemotePreviewAndReturnPath(video) | |
54 | }) | |
55 | } | |
56 | ||
57 | private saveRemotePreviewAndReturnPath (video: VideoInstance) { | |
58 | const req = fetchRemotePreview(video.Author.Pod, video) | |
59 | ||
60 | return new Promise<string>((res, rej) => { | |
61 | const path = join(CACHE.DIRECTORIES.PREVIEWS, video.getPreviewName()) | |
62 | const stream = createWriteStream(path) | |
63 | ||
64 | req.pipe(stream) | |
65 | .on('finish', () => res(path)) | |
66 | .on('error', (err) => rej(err)) | |
67 | }) | |
68 | } | |
69 | } | |
70 | ||
71 | export { | |
72 | VideosPreviewCache | |
73 | } |