aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/files-cache
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2019-04-23 09:50:57 +0200
committerChocobozzz <me@florianbigard.com>2019-04-24 16:26:21 +0200
commit3acc50844047a37698f0618fa235c138e386a053 (patch)
treee33243bf7fadbcf2df616fc41814245094fd881a /server/lib/files-cache
parent1735c825726edaa0af5035cb6cbb0cc0db502c6d (diff)
downloadPeerTube-3acc50844047a37698f0618fa235c138e386a053.tar.gz
PeerTube-3acc50844047a37698f0618fa235c138e386a053.tar.zst
PeerTube-3acc50844047a37698f0618fa235c138e386a053.zip
Upgrade sequelize
Diffstat (limited to 'server/lib/files-cache')
-rw-r--r--server/lib/files-cache/abstract-video-static-file-cache.ts18
-rw-r--r--server/lib/files-cache/videos-caption-cache.ts9
-rw-r--r--server/lib/files-cache/videos-preview-cache.ts6
3 files changed, 22 insertions, 11 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 61837e0f8..84ed74c98 100644
--- a/server/lib/files-cache/abstract-video-static-file-cache.ts
+++ b/server/lib/files-cache/abstract-video-static-file-cache.ts
@@ -4,24 +4,28 @@ import { VideoModel } from '../../models/video/video'
4import { fetchRemoteVideoStaticFile } from '../activitypub' 4import { fetchRemoteVideoStaticFile } from '../activitypub'
5import * as memoizee from 'memoizee' 5import * as memoizee from 'memoizee'
6 6
7type GetFilePathResult = { isOwned: boolean, path: string } | undefined
8
7export abstract class AbstractVideoStaticFileCache <T> { 9export abstract class AbstractVideoStaticFileCache <T> {
8 10
9 getFilePath: (params: T) => Promise<string> 11 getFilePath: (params: T) => Promise<GetFilePathResult>
10 12
11 abstract getFilePathImpl (params: T): Promise<string> 13 abstract getFilePathImpl (params: T): Promise<GetFilePathResult>
12 14
13 // Load and save the remote file, then return the local path from filesystem 15 // Load and save the remote file, then return the local path from filesystem
14 protected abstract loadRemoteFile (key: string): Promise<string> 16 protected abstract loadRemoteFile (key: string): Promise<GetFilePathResult>
15 17
16 init (max: number, maxAge: number) { 18 init (max: number, maxAge: number) {
17 this.getFilePath = memoizee(this.getFilePathImpl, { 19 this.getFilePath = memoizee(this.getFilePathImpl, {
18 maxAge, 20 maxAge,
19 max, 21 max,
20 promise: true, 22 promise: true,
21 dispose: (value: string) => { 23 dispose: (result: GetFilePathResult) => {
22 remove(value) 24 if (result.isOwned !== true) {
23 .then(() => logger.debug('%s evicted from %s', value, this.constructor.name)) 25 remove(result.path)
24 .catch(err => logger.error('Cannot remove %s from cache %s.', value, this.constructor.name, { err })) 26 .then(() => logger.debug('%s removed from %s', result.path, this.constructor.name))
27 .catch(err => logger.error('Cannot remove %s from cache %s.', result.path, this.constructor.name, { err }))
28 }
25 } 29 }
26 }) 30 })
27 } 31 }
diff --git a/server/lib/files-cache/videos-caption-cache.ts b/server/lib/files-cache/videos-caption-cache.ts
index d4a0a3345..305e39c35 100644
--- a/server/lib/files-cache/videos-caption-cache.ts
+++ b/server/lib/files-cache/videos-caption-cache.ts
@@ -4,6 +4,7 @@ import { VideoModel } from '../../models/video/video'
4import { VideoCaptionModel } from '../../models/video/video-caption' 4import { VideoCaptionModel } from '../../models/video/video-caption'
5import { AbstractVideoStaticFileCache } from './abstract-video-static-file-cache' 5import { AbstractVideoStaticFileCache } from './abstract-video-static-file-cache'
6import { CONFIG } from '../../initializers/config' 6import { CONFIG } from '../../initializers/config'
7import { logger } from '../../helpers/logger'
7 8
8type GetPathParam = { videoId: string, language: string } 9type GetPathParam = { videoId: string, language: string }
9 10
@@ -24,13 +25,15 @@ class VideosCaptionCache extends AbstractVideoStaticFileCache <GetPathParam> {
24 const videoCaption = await VideoCaptionModel.loadByVideoIdAndLanguage(params.videoId, params.language) 25 const videoCaption = await VideoCaptionModel.loadByVideoIdAndLanguage(params.videoId, params.language)
25 if (!videoCaption) return undefined 26 if (!videoCaption) return undefined
26 27
27 if (videoCaption.isOwned()) return join(CONFIG.STORAGE.CAPTIONS_DIR, videoCaption.getCaptionName()) 28 if (videoCaption.isOwned()) return { isOwned: true, path: join(CONFIG.STORAGE.CAPTIONS_DIR, videoCaption.getCaptionName()) }
28 29
29 const key = params.videoId + VideosCaptionCache.KEY_DELIMITER + params.language 30 const key = params.videoId + VideosCaptionCache.KEY_DELIMITER + params.language
30 return this.loadRemoteFile(key) 31 return this.loadRemoteFile(key)
31 } 32 }
32 33
33 protected async loadRemoteFile (key: string) { 34 protected async loadRemoteFile (key: string) {
35 logger.debug('Loading remote caption file %s.', key)
36
34 const [ videoId, language ] = key.split(VideosCaptionCache.KEY_DELIMITER) 37 const [ videoId, language ] = key.split(VideosCaptionCache.KEY_DELIMITER)
35 38
36 const videoCaption = await VideoCaptionModel.loadByVideoIdAndLanguage(videoId, language) 39 const videoCaption = await VideoCaptionModel.loadByVideoIdAndLanguage(videoId, language)
@@ -46,7 +49,9 @@ class VideosCaptionCache extends AbstractVideoStaticFileCache <GetPathParam> {
46 const remoteStaticPath = videoCaption.getCaptionStaticPath() 49 const remoteStaticPath = videoCaption.getCaptionStaticPath()
47 const destPath = join(FILES_CACHE.VIDEO_CAPTIONS.DIRECTORY, videoCaption.getCaptionName()) 50 const destPath = join(FILES_CACHE.VIDEO_CAPTIONS.DIRECTORY, videoCaption.getCaptionName())
48 51
49 return this.saveRemoteVideoFileAndReturnPath(video, remoteStaticPath, destPath) 52 const path = await this.saveRemoteVideoFileAndReturnPath(video, remoteStaticPath, destPath)
53
54 return { isOwned: false, path }
50 } 55 }
51} 56}
52 57
diff --git a/server/lib/files-cache/videos-preview-cache.ts b/server/lib/files-cache/videos-preview-cache.ts
index fc0d92c78..c117ae426 100644
--- a/server/lib/files-cache/videos-preview-cache.ts
+++ b/server/lib/files-cache/videos-preview-cache.ts
@@ -20,7 +20,7 @@ class VideosPreviewCache extends AbstractVideoStaticFileCache <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.getPreview().filename) 23 if (video.isOwned()) return { isOwned: true, path: join(CONFIG.STORAGE.PREVIEWS_DIR, video.getPreview().filename) }
24 24
25 return this.loadRemoteFile(videoUUID) 25 return this.loadRemoteFile(videoUUID)
26 } 26 }
@@ -35,7 +35,9 @@ class VideosPreviewCache extends AbstractVideoStaticFileCache <string> {
35 const remoteStaticPath = join(STATIC_PATHS.PREVIEWS, video.getPreview().filename) 35 const remoteStaticPath = join(STATIC_PATHS.PREVIEWS, video.getPreview().filename)
36 const destPath = join(FILES_CACHE.PREVIEWS.DIRECTORY, video.getPreview().filename) 36 const destPath = join(FILES_CACHE.PREVIEWS.DIRECTORY, video.getPreview().filename)
37 37
38 return this.saveRemoteVideoFileAndReturnPath(video, remoteStaticPath, destPath) 38 const path = await this.saveRemoteVideoFileAndReturnPath(video, remoteStaticPath, destPath)
39
40 return { isOwned: false, path }
39 } 41 }
40} 42}
41 43