aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib')
-rw-r--r--server/lib/cache/index.ts1
-rw-r--r--server/lib/cache/videos-preview-cache.ts74
-rw-r--r--server/lib/friends.ts14
-rw-r--r--server/lib/index.ts1
4 files changed, 88 insertions, 2 deletions
diff --git a/server/lib/cache/index.ts b/server/lib/cache/index.ts
new file mode 100644
index 000000000..7bf63790a
--- /dev/null
+++ b/server/lib/cache/index.ts
@@ -0,0 +1 @@
export * from './videos-preview-cache'
diff --git a/server/lib/cache/videos-preview-cache.ts b/server/lib/cache/videos-preview-cache.ts
new file mode 100644
index 000000000..9d365e496
--- /dev/null
+++ b/server/lib/cache/videos-preview-cache.ts
@@ -0,0 +1,74 @@
1import * as request from 'request'
2import * as asyncLRU from 'async-lru'
3import { join } from 'path'
4import { createWriteStream } from 'fs'
5import * as Promise from 'bluebird'
6
7import { database as db, CONFIG, CACHE } from '../../initializers'
8import { logger, writeFilePromise, unlinkPromise } from '../../helpers'
9import { VideoInstance } from '../../models'
10import { fetchRemotePreview } from '../../lib'
11
12class VideosPreviewCache {
13
14 private static instance: VideosPreviewCache
15
16 private lru
17
18 private constructor () { }
19
20 static get Instance () {
21 return this.instance || (this.instance = new this())
22 }
23
24 init (max: number) {
25 this.lru = new asyncLRU({
26 max,
27 load: (key, cb) => {
28 this.loadPreviews(key)
29 .then(res => cb(null, res))
30 .catch(err => cb(err))
31 }
32 })
33
34 this.lru.on('evict', (obj: { key: string, value: string }) => {
35 unlinkPromise(obj.value).then(() => logger.debug('%s evicted from VideosPreviewCache', obj.value))
36 })
37 }
38
39 getPreviewPath (key: string) {
40 return new Promise<string>((res, rej) => {
41 this.lru.get(key, (err, value) => {
42 err ? rej(err) : res(value)
43 })
44 })
45 }
46
47 private loadPreviews (key: string) {
48 return db.Video.loadByUUIDAndPopulateAuthorAndPodAndTags(key)
49 .then(video => {
50 if (!video) return undefined
51
52 if (video.isOwned()) return join(CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName())
53
54 return this.saveRemotePreviewAndReturnPath(video)
55 })
56 }
57
58 private saveRemotePreviewAndReturnPath (video: VideoInstance) {
59 const req = fetchRemotePreview(video.Author.Pod, video)
60
61 return new Promise<string>((res, rej) => {
62 const path = join(CACHE.DIRECTORIES.PREVIEWS, video.getPreviewName())
63 const stream = createWriteStream(path)
64
65 req.pipe(stream)
66 .on('finish', () => res(path))
67 .on('error', (err) => rej(err))
68 })
69 }
70}
71
72export {
73 VideosPreviewCache
74}
diff --git a/server/lib/friends.ts b/server/lib/friends.ts
index 6ed0da013..50355d5d1 100644
--- a/server/lib/friends.ts
+++ b/server/lib/friends.ts
@@ -1,6 +1,7 @@
1import * as request from 'request' 1import * as request from 'request'
2import * as Sequelize from 'sequelize' 2import * as Sequelize from 'sequelize'
3import * as Promise from 'bluebird' 3import * as Promise from 'bluebird'
4import { join } from 'path'
4 5
5import { database as db } from '../initializers/database' 6import { database as db } from '../initializers/database'
6import { 7import {
@@ -9,7 +10,8 @@ import {
9 REQUESTS_IN_PARALLEL, 10 REQUESTS_IN_PARALLEL,
10 REQUEST_ENDPOINTS, 11 REQUEST_ENDPOINTS,
11 REQUEST_ENDPOINT_ACTIONS, 12 REQUEST_ENDPOINT_ACTIONS,
12 REMOTE_SCHEME 13 REMOTE_SCHEME,
14 STATIC_PATHS
13} from '../initializers' 15} from '../initializers'
14import { 16import {
15 logger, 17 logger,
@@ -233,6 +235,13 @@ function sendOwnedVideosToPod (podId: number) {
233 }) 235 })
234} 236}
235 237
238function fetchRemotePreview (pod: PodInstance, video: VideoInstance) {
239 const host = video.Author.Pod.host
240 const path = join(STATIC_PATHS.PREVIEWS, video.getPreviewName())
241
242 return request.get(REMOTE_SCHEME.HTTP + '://' + host + path)
243}
244
236function getRequestScheduler () { 245function getRequestScheduler () {
237 return requestScheduler 246 return requestScheduler
238} 247}
@@ -263,7 +272,8 @@ export {
263 sendOwnedVideosToPod, 272 sendOwnedVideosToPod,
264 getRequestScheduler, 273 getRequestScheduler,
265 getRequestVideoQaduScheduler, 274 getRequestVideoQaduScheduler,
266 getRequestVideoEventScheduler 275 getRequestVideoEventScheduler,
276 fetchRemotePreview
267} 277}
268 278
269// --------------------------------------------------------------------------- 279// ---------------------------------------------------------------------------
diff --git a/server/lib/index.ts b/server/lib/index.ts
index b8697fb96..8628da4dd 100644
--- a/server/lib/index.ts
+++ b/server/lib/index.ts
@@ -1,3 +1,4 @@
1export * from './cache'
1export * from './jobs' 2export * from './jobs'
2export * from './request' 3export * from './request'
3export * from './friends' 4export * from './friends'