diff options
author | Chocobozzz <me@florianbigard.com> | 2019-04-17 10:07:00 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2019-04-24 16:25:52 +0200 |
commit | e8bafea35bc930cb8ac5b2d521a188642a1adffe (patch) | |
tree | 7537f957ed7307b464e3c90b71b813d992acaade /server/lib/files-cache | |
parent | 94565d52bb2883e09f16d1363170ac9c0dccb7a1 (diff) | |
download | PeerTube-e8bafea35bc930cb8ac5b2d521a188642a1adffe.tar.gz PeerTube-e8bafea35bc930cb8ac5b2d521a188642a1adffe.tar.zst PeerTube-e8bafea35bc930cb8ac5b2d521a188642a1adffe.zip |
Create a dedicated table to track video thumbnails
Diffstat (limited to 'server/lib/files-cache')
-rw-r--r-- | server/lib/files-cache/abstract-video-static-file-cache.ts | 32 | ||||
-rw-r--r-- | server/lib/files-cache/videos-caption-cache.ts | 5 | ||||
-rw-r--r-- | server/lib/files-cache/videos-preview-cache.ts | 11 |
3 files changed, 19 insertions, 29 deletions
diff --git a/server/lib/files-cache/abstract-video-static-file-cache.ts b/server/lib/files-cache/abstract-video-static-file-cache.ts index 7512f2b9d..61837e0f8 100644 --- a/server/lib/files-cache/abstract-video-static-file-cache.ts +++ b/server/lib/files-cache/abstract-video-static-file-cache.ts | |||
@@ -1,41 +1,29 @@ | |||
1 | import * as AsyncLRU from 'async-lru' | ||
2 | import { createWriteStream, remove } from 'fs-extra' | 1 | import { createWriteStream, remove } from 'fs-extra' |
3 | import { logger } from '../../helpers/logger' | 2 | import { logger } from '../../helpers/logger' |
4 | import { VideoModel } from '../../models/video/video' | 3 | import { VideoModel } from '../../models/video/video' |
5 | import { fetchRemoteVideoStaticFile } from '../activitypub' | 4 | import { fetchRemoteVideoStaticFile } from '../activitypub' |
5 | import * as memoizee from 'memoizee' | ||
6 | 6 | ||
7 | export abstract class AbstractVideoStaticFileCache <T> { | 7 | export abstract class AbstractVideoStaticFileCache <T> { |
8 | 8 | ||
9 | protected lru | 9 | getFilePath: (params: T) => Promise<string> |
10 | 10 | ||
11 | abstract getFilePath (params: T): Promise<string> | 11 | abstract getFilePathImpl (params: T): Promise<string> |
12 | 12 | ||
13 | // Load and save the remote file, then return the local path from filesystem | 13 | // Load and save the remote file, then return the local path from filesystem |
14 | protected abstract loadRemoteFile (key: string): Promise<string> | 14 | protected abstract loadRemoteFile (key: string): Promise<string> |
15 | 15 | ||
16 | init (max: number, maxAge: number) { | 16 | init (max: number, maxAge: number) { |
17 | this.lru = new AsyncLRU({ | 17 | this.getFilePath = memoizee(this.getFilePathImpl, { |
18 | max, | ||
19 | maxAge, | 18 | maxAge, |
20 | load: (key, cb) => { | 19 | max, |
21 | this.loadRemoteFile(key) | 20 | promise: true, |
22 | .then(res => cb(null, res)) | 21 | dispose: (value: string) => { |
23 | .catch(err => cb(err)) | 22 | remove(value) |
23 | .then(() => logger.debug('%s evicted from %s', value, this.constructor.name)) | ||
24 | .catch(err => logger.error('Cannot remove %s from cache %s.', value, this.constructor.name, { err })) | ||
24 | } | 25 | } |
25 | }) | 26 | }) |
26 | |||
27 | this.lru.on('evict', (obj: { key: string, value: string }) => { | ||
28 | remove(obj.value) | ||
29 | .then(() => logger.debug('%s evicted from %s', obj.value, this.constructor.name)) | ||
30 | }) | ||
31 | } | ||
32 | |||
33 | protected loadFromLRU (key: string) { | ||
34 | return new Promise<string>((res, rej) => { | ||
35 | this.lru.get(key, (err, value) => { | ||
36 | err ? rej(err) : res(value) | ||
37 | }) | ||
38 | }) | ||
39 | } | 27 | } |
40 | 28 | ||
41 | protected saveRemoteVideoFileAndReturnPath (video: VideoModel, remoteStaticPath: string, destPath: string) { | 29 | protected saveRemoteVideoFileAndReturnPath (video: VideoModel, remoteStaticPath: string, destPath: string) { |
diff --git a/server/lib/files-cache/videos-caption-cache.ts b/server/lib/files-cache/videos-caption-cache.ts index 0926f4009..d4a0a3345 100644 --- a/server/lib/files-cache/videos-caption-cache.ts +++ b/server/lib/files-cache/videos-caption-cache.ts | |||
@@ -20,14 +20,14 @@ class VideosCaptionCache extends AbstractVideoStaticFileCache <GetPathParam> { | |||
20 | return this.instance || (this.instance = new this()) | 20 | return this.instance || (this.instance = new this()) |
21 | } | 21 | } |
22 | 22 | ||
23 | async getFilePath (params: GetPathParam) { | 23 | async getFilePathImpl (params: GetPathParam) { |
24 | const videoCaption = await VideoCaptionModel.loadByVideoIdAndLanguage(params.videoId, params.language) | 24 | const videoCaption = await VideoCaptionModel.loadByVideoIdAndLanguage(params.videoId, params.language) |
25 | if (!videoCaption) return undefined | 25 | if (!videoCaption) return undefined |
26 | 26 | ||
27 | if (videoCaption.isOwned()) return join(CONFIG.STORAGE.CAPTIONS_DIR, videoCaption.getCaptionName()) | 27 | if (videoCaption.isOwned()) return join(CONFIG.STORAGE.CAPTIONS_DIR, videoCaption.getCaptionName()) |
28 | 28 | ||
29 | const key = params.videoId + VideosCaptionCache.KEY_DELIMITER + params.language | 29 | const key = params.videoId + VideosCaptionCache.KEY_DELIMITER + params.language |
30 | return this.loadFromLRU(key) | 30 | return this.loadRemoteFile(key) |
31 | } | 31 | } |
32 | 32 | ||
33 | protected async loadRemoteFile (key: string) { | 33 | protected async loadRemoteFile (key: string) { |
@@ -42,6 +42,7 @@ class VideosCaptionCache extends AbstractVideoStaticFileCache <GetPathParam> { | |||
42 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoId) | 42 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoId) |
43 | if (!video) return undefined | 43 | if (!video) return undefined |
44 | 44 | ||
45 | // FIXME: use URL | ||
45 | const remoteStaticPath = videoCaption.getCaptionStaticPath() | 46 | const remoteStaticPath = videoCaption.getCaptionStaticPath() |
46 | const destPath = join(FILES_CACHE.VIDEO_CAPTIONS.DIRECTORY, videoCaption.getCaptionName()) | 47 | const destPath = join(FILES_CACHE.VIDEO_CAPTIONS.DIRECTORY, videoCaption.getCaptionName()) |
47 | 48 | ||
diff --git a/server/lib/files-cache/videos-preview-cache.ts b/server/lib/files-cache/videos-preview-cache.ts index 6575e1c83..fc0d92c78 100644 --- a/server/lib/files-cache/videos-preview-cache.ts +++ b/server/lib/files-cache/videos-preview-cache.ts | |||
@@ -16,13 +16,13 @@ class VideosPreviewCache extends AbstractVideoStaticFileCache <string> { | |||
16 | return this.instance || (this.instance = new this()) | 16 | return this.instance || (this.instance = new this()) |
17 | } | 17 | } |
18 | 18 | ||
19 | async getFilePath (videoUUID: string) { | 19 | async getFilePathImpl (videoUUID: string) { |
20 | const video = await VideoModel.loadByUUIDWithFile(videoUUID) | 20 | const video = await VideoModel.loadByUUIDWithFile(videoUUID) |
21 | if (!video) return undefined | 21 | if (!video) return undefined |
22 | 22 | ||
23 | if (video.isOwned()) return join(CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName()) | 23 | if (video.isOwned()) return join(CONFIG.STORAGE.PREVIEWS_DIR, video.getPreview().filename) |
24 | 24 | ||
25 | return this.loadFromLRU(videoUUID) | 25 | return this.loadRemoteFile(videoUUID) |
26 | } | 26 | } |
27 | 27 | ||
28 | protected async loadRemoteFile (key: string) { | 28 | protected async loadRemoteFile (key: string) { |
@@ -31,8 +31,9 @@ class VideosPreviewCache extends AbstractVideoStaticFileCache <string> { | |||
31 | 31 | ||
32 | if (video.isOwned()) throw new Error('Cannot load remote preview of owned video.') | 32 | if (video.isOwned()) throw new Error('Cannot load remote preview of owned video.') |
33 | 33 | ||
34 | const remoteStaticPath = join(STATIC_PATHS.PREVIEWS, video.getPreviewName()) | 34 | // FIXME: use URL |
35 | const destPath = join(FILES_CACHE.PREVIEWS.DIRECTORY, video.getPreviewName()) | 35 | const remoteStaticPath = join(STATIC_PATHS.PREVIEWS, video.getPreview().filename) |
36 | const destPath = join(FILES_CACHE.PREVIEWS.DIRECTORY, video.getPreview().filename) | ||
36 | 37 | ||
37 | return this.saveRemoteVideoFileAndReturnPath(video, remoteStaticPath, destPath) | 38 | return this.saveRemoteVideoFileAndReturnPath(video, remoteStaticPath, destPath) |
38 | } | 39 | } |