diff options
author | Chocobozzz <me@florianbigard.com> | 2019-04-24 09:28:06 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2019-04-24 16:26:21 +0200 |
commit | dc8527376482293c87fc6f30027d626f58a1197b (patch) | |
tree | 688625c5ab1619c3235f808a22bed0501c670a62 /server | |
parent | 3acc50844047a37698f0618fa235c138e386a053 (diff) | |
download | PeerTube-dc8527376482293c87fc6f30027d626f58a1197b.tar.gz PeerTube-dc8527376482293c87fc6f30027d626f58a1197b.tar.zst PeerTube-dc8527376482293c87fc6f30027d626f58a1197b.zip |
Refactor video caption/preview caches
Diffstat (limited to 'server')
-rw-r--r-- | server/lib/activitypub/videos.ts | 8 | ||||
-rw-r--r-- | server/lib/files-cache/abstract-video-static-file-cache.ts | 16 | ||||
-rw-r--r-- | server/lib/files-cache/videos-caption-cache.ts | 5 | ||||
-rw-r--r-- | server/lib/files-cache/videos-preview-cache.ts | 5 |
4 files changed, 10 insertions, 24 deletions
diff --git a/server/lib/activitypub/videos.ts b/server/lib/activitypub/videos.ts index 63bb07ec1..4f26cb6be 100644 --- a/server/lib/activitypub/videos.ts +++ b/server/lib/activitypub/videos.ts | |||
@@ -15,7 +15,7 @@ import { sanitizeAndCheckVideoTorrentObject } from '../../helpers/custom-validat | |||
15 | import { isVideoFileInfoHashValid } from '../../helpers/custom-validators/videos' | 15 | import { isVideoFileInfoHashValid } from '../../helpers/custom-validators/videos' |
16 | import { resetSequelizeInstance, retryTransactionWrapper } from '../../helpers/database-utils' | 16 | import { resetSequelizeInstance, retryTransactionWrapper } from '../../helpers/database-utils' |
17 | import { logger } from '../../helpers/logger' | 17 | import { logger } from '../../helpers/logger' |
18 | import { doRequest } from '../../helpers/requests' | 18 | import { doRequest, doRequestAndSaveToFile } from '../../helpers/requests' |
19 | import { | 19 | import { |
20 | ACTIVITY_PUB, | 20 | ACTIVITY_PUB, |
21 | MIMETYPES, | 21 | MIMETYPES, |
@@ -108,13 +108,11 @@ async function fetchRemoteVideoDescription (video: VideoModel) { | |||
108 | return body.description ? body.description : '' | 108 | return body.description ? body.description : '' |
109 | } | 109 | } |
110 | 110 | ||
111 | function fetchRemoteVideoStaticFile (video: VideoModel, path: string, reject: Function) { | 111 | function fetchRemoteVideoStaticFile (video: VideoModel, path: string, destPath: string) { |
112 | const url = buildRemoteBaseUrl(video, path) | 112 | const url = buildRemoteBaseUrl(video, path) |
113 | 113 | ||
114 | // We need to provide a callback, if no we could have an uncaught exception | 114 | // We need to provide a callback, if no we could have an uncaught exception |
115 | return request.get(url, err => { | 115 | return doRequestAndSaveToFile({ uri: url }, destPath) |
116 | if (err) reject(err) | ||
117 | }) | ||
118 | } | 116 | } |
119 | 117 | ||
120 | function buildRemoteBaseUrl (video: VideoModel, path: string) { | 118 | function buildRemoteBaseUrl (video: VideoModel, path: string) { |
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 84ed74c98..1908cfb06 100644 --- a/server/lib/files-cache/abstract-video-static-file-cache.ts +++ b/server/lib/files-cache/abstract-video-static-file-cache.ts | |||
@@ -1,7 +1,5 @@ | |||
1 | import { createWriteStream, remove } from 'fs-extra' | 1 | import { remove } from 'fs-extra' |
2 | import { logger } from '../../helpers/logger' | 2 | import { logger } from '../../helpers/logger' |
3 | import { VideoModel } from '../../models/video/video' | ||
4 | import { fetchRemoteVideoStaticFile } from '../activitypub' | ||
5 | import * as memoizee from 'memoizee' | 3 | import * as memoizee from 'memoizee' |
6 | 4 | ||
7 | type GetFilePathResult = { isOwned: boolean, path: string } | undefined | 5 | type GetFilePathResult = { isOwned: boolean, path: string } | undefined |
@@ -29,16 +27,4 @@ export abstract class AbstractVideoStaticFileCache <T> { | |||
29 | } | 27 | } |
30 | }) | 28 | }) |
31 | } | 29 | } |
32 | |||
33 | protected saveRemoteVideoFileAndReturnPath (video: VideoModel, remoteStaticPath: string, destPath: string) { | ||
34 | return new Promise<string>((res, rej) => { | ||
35 | const req = fetchRemoteVideoStaticFile(video, remoteStaticPath, rej) | ||
36 | |||
37 | const stream = createWriteStream(destPath) | ||
38 | |||
39 | req.pipe(stream) | ||
40 | .on('error', (err) => rej(err)) | ||
41 | .on('finish', () => res(destPath)) | ||
42 | }) | ||
43 | } | ||
44 | } | 30 | } |
diff --git a/server/lib/files-cache/videos-caption-cache.ts b/server/lib/files-cache/videos-caption-cache.ts index 305e39c35..440c3fde8 100644 --- a/server/lib/files-cache/videos-caption-cache.ts +++ b/server/lib/files-cache/videos-caption-cache.ts | |||
@@ -5,6 +5,7 @@ import { VideoCaptionModel } from '../../models/video/video-caption' | |||
5 | import { AbstractVideoStaticFileCache } from './abstract-video-static-file-cache' | 5 | import { AbstractVideoStaticFileCache } from './abstract-video-static-file-cache' |
6 | import { CONFIG } from '../../initializers/config' | 6 | import { CONFIG } from '../../initializers/config' |
7 | import { logger } from '../../helpers/logger' | 7 | import { logger } from '../../helpers/logger' |
8 | import { fetchRemoteVideoStaticFile } from '../activitypub' | ||
8 | 9 | ||
9 | type GetPathParam = { videoId: string, language: string } | 10 | type GetPathParam = { videoId: string, language: string } |
10 | 11 | ||
@@ -49,9 +50,9 @@ class VideosCaptionCache extends AbstractVideoStaticFileCache <GetPathParam> { | |||
49 | const remoteStaticPath = videoCaption.getCaptionStaticPath() | 50 | const remoteStaticPath = videoCaption.getCaptionStaticPath() |
50 | const destPath = join(FILES_CACHE.VIDEO_CAPTIONS.DIRECTORY, videoCaption.getCaptionName()) | 51 | const destPath = join(FILES_CACHE.VIDEO_CAPTIONS.DIRECTORY, videoCaption.getCaptionName()) |
51 | 52 | ||
52 | const path = await this.saveRemoteVideoFileAndReturnPath(video, remoteStaticPath, destPath) | 53 | await fetchRemoteVideoStaticFile(video, remoteStaticPath, destPath) |
53 | 54 | ||
54 | return { isOwned: false, path } | 55 | return { isOwned: false, path: destPath } |
55 | } | 56 | } |
56 | } | 57 | } |
57 | 58 | ||
diff --git a/server/lib/files-cache/videos-preview-cache.ts b/server/lib/files-cache/videos-preview-cache.ts index c117ae426..14be7f24a 100644 --- a/server/lib/files-cache/videos-preview-cache.ts +++ b/server/lib/files-cache/videos-preview-cache.ts | |||
@@ -3,6 +3,7 @@ import { FILES_CACHE, STATIC_PATHS } from '../../initializers/constants' | |||
3 | import { VideoModel } from '../../models/video/video' | 3 | import { VideoModel } from '../../models/video/video' |
4 | import { AbstractVideoStaticFileCache } from './abstract-video-static-file-cache' | 4 | import { AbstractVideoStaticFileCache } from './abstract-video-static-file-cache' |
5 | import { CONFIG } from '../../initializers/config' | 5 | import { CONFIG } from '../../initializers/config' |
6 | import { fetchRemoteVideoStaticFile } from '../activitypub' | ||
6 | 7 | ||
7 | class VideosPreviewCache extends AbstractVideoStaticFileCache <string> { | 8 | class VideosPreviewCache extends AbstractVideoStaticFileCache <string> { |
8 | 9 | ||
@@ -35,9 +36,9 @@ class VideosPreviewCache extends AbstractVideoStaticFileCache <string> { | |||
35 | const remoteStaticPath = join(STATIC_PATHS.PREVIEWS, video.getPreview().filename) | 36 | const remoteStaticPath = join(STATIC_PATHS.PREVIEWS, video.getPreview().filename) |
36 | const destPath = join(FILES_CACHE.PREVIEWS.DIRECTORY, video.getPreview().filename) | 37 | const destPath = join(FILES_CACHE.PREVIEWS.DIRECTORY, video.getPreview().filename) |
37 | 38 | ||
38 | const path = await this.saveRemoteVideoFileAndReturnPath(video, remoteStaticPath, destPath) | 39 | await fetchRemoteVideoStaticFile(video, remoteStaticPath, destPath) |
39 | 40 | ||
40 | return { isOwned: false, path } | 41 | return { isOwned: false, path: destPath } |
41 | } | 42 | } |
42 | } | 43 | } |
43 | 44 | ||