diff options
Diffstat (limited to 'server/helpers')
-rw-r--r-- | server/helpers/actors.ts | 17 | ||||
-rw-r--r-- | server/helpers/decache.ts | 78 | ||||
-rw-r--r-- | server/helpers/webtorrent.ts | 18 |
3 files changed, 108 insertions, 5 deletions
diff --git a/server/helpers/actors.ts b/server/helpers/actors.ts new file mode 100644 index 000000000..c31fe6f8e --- /dev/null +++ b/server/helpers/actors.ts | |||
@@ -0,0 +1,17 @@ | |||
1 | import { WEBSERVER } from '@server/initializers/constants' | ||
2 | |||
3 | function handleToNameAndHost (handle: string) { | ||
4 | let [ name, host ] = handle.split('@') | ||
5 | if (host === WEBSERVER.HOST) host = null | ||
6 | |||
7 | return { name, host, handle } | ||
8 | } | ||
9 | |||
10 | function handlesToNameAndHost (handles: string[]) { | ||
11 | return handles.map(h => handleToNameAndHost(h)) | ||
12 | } | ||
13 | |||
14 | export { | ||
15 | handleToNameAndHost, | ||
16 | handlesToNameAndHost | ||
17 | } | ||
diff --git a/server/helpers/decache.ts b/server/helpers/decache.ts new file mode 100644 index 000000000..e31973b7a --- /dev/null +++ b/server/helpers/decache.ts | |||
@@ -0,0 +1,78 @@ | |||
1 | // Thanks: https://github.com/dwyl/decache | ||
2 | // We reuse this file to also uncache plugin base path | ||
3 | |||
4 | import { extname } from 'path' | ||
5 | |||
6 | function decachePlugin (pluginPath: string, libraryPath: string) { | ||
7 | const moduleName = find(libraryPath) | ||
8 | |||
9 | if (!moduleName) return | ||
10 | |||
11 | searchCache(moduleName, function (mod) { | ||
12 | delete require.cache[mod.id] | ||
13 | }) | ||
14 | |||
15 | removeCachedPath(pluginPath) | ||
16 | } | ||
17 | |||
18 | function decacheModule (name: string) { | ||
19 | const moduleName = find(name) | ||
20 | |||
21 | if (!moduleName) return | ||
22 | |||
23 | searchCache(moduleName, function (mod) { | ||
24 | delete require.cache[mod.id] | ||
25 | }) | ||
26 | |||
27 | removeCachedPath(moduleName) | ||
28 | } | ||
29 | |||
30 | // --------------------------------------------------------------------------- | ||
31 | |||
32 | export { | ||
33 | decacheModule, | ||
34 | decachePlugin | ||
35 | } | ||
36 | |||
37 | // --------------------------------------------------------------------------- | ||
38 | |||
39 | function find (moduleName: string) { | ||
40 | try { | ||
41 | return require.resolve(moduleName) | ||
42 | } catch { | ||
43 | return '' | ||
44 | } | ||
45 | } | ||
46 | |||
47 | function searchCache (moduleName: string, callback: (current: NodeModule) => void) { | ||
48 | const resolvedModule = require.resolve(moduleName) | ||
49 | let mod: NodeModule | ||
50 | const visited = {} | ||
51 | |||
52 | if (resolvedModule && ((mod = require.cache[resolvedModule]) !== undefined)) { | ||
53 | // Recursively go over the results | ||
54 | (function run (current) { | ||
55 | visited[current.id] = true | ||
56 | |||
57 | current.children.forEach(function (child) { | ||
58 | if (extname(child.filename) !== '.node' && !visited[child.id]) { | ||
59 | run(child) | ||
60 | } | ||
61 | }) | ||
62 | |||
63 | // Call the specified callback providing the | ||
64 | // found module | ||
65 | callback(current) | ||
66 | })(mod) | ||
67 | } | ||
68 | }; | ||
69 | |||
70 | function removeCachedPath (pluginPath: string) { | ||
71 | const pathCache = (module.constructor as any)._pathCache | ||
72 | |||
73 | Object.keys(pathCache).forEach(function (cacheKey) { | ||
74 | if (cacheKey.includes(pluginPath)) { | ||
75 | delete pathCache[cacheKey] | ||
76 | } | ||
77 | }) | ||
78 | } | ||
diff --git a/server/helpers/webtorrent.ts b/server/helpers/webtorrent.ts index c75c058e4..ecc703646 100644 --- a/server/helpers/webtorrent.ts +++ b/server/helpers/webtorrent.ts | |||
@@ -14,7 +14,7 @@ import { MVideo } from '@server/types/models/video/video' | |||
14 | import { MVideoFile, MVideoFileRedundanciesOpt } from '@server/types/models/video/video-file' | 14 | import { MVideoFile, MVideoFileRedundanciesOpt } from '@server/types/models/video/video-file' |
15 | import { MStreamingPlaylistVideo } from '@server/types/models/video/video-streaming-playlist' | 15 | import { MStreamingPlaylistVideo } from '@server/types/models/video/video-streaming-playlist' |
16 | import { CONFIG } from '../initializers/config' | 16 | import { CONFIG } from '../initializers/config' |
17 | import { promisify2 } from './core-utils' | 17 | import { promisify2, sha1 } from './core-utils' |
18 | import { logger } from './logger' | 18 | import { logger } from './logger' |
19 | import { generateVideoImportTmpPath } from './utils' | 19 | import { generateVideoImportTmpPath } from './utils' |
20 | import { extractVideo } from './video' | 20 | import { extractVideo } from './video' |
@@ -94,7 +94,7 @@ function createTorrentAndSetInfoHash (videoOrPlaylist: MVideo | MStreamingPlayli | |||
94 | 94 | ||
95 | const options = { | 95 | const options = { |
96 | // Keep the extname, it's used by the client to stream the file inside a web browser | 96 | // Keep the extname, it's used by the client to stream the file inside a web browser |
97 | name: `${video.name} ${videoFile.resolution}p${videoFile.extname}`, | 97 | name: buildInfoName(video, videoFile), |
98 | createdBy: 'PeerTube', | 98 | createdBy: 'PeerTube', |
99 | announceList: buildAnnounceList(), | 99 | announceList: buildAnnounceList(), |
100 | urlList: buildUrlList(video, videoFile) | 100 | urlList: buildUrlList(video, videoFile) |
@@ -120,7 +120,7 @@ function createTorrentAndSetInfoHash (videoOrPlaylist: MVideo | MStreamingPlayli | |||
120 | }) | 120 | }) |
121 | } | 121 | } |
122 | 122 | ||
123 | async function updateTorrentUrls (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile) { | 123 | async function updateTorrentMetadata (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile) { |
124 | const video = extractVideo(videoOrPlaylist) | 124 | const video = extractVideo(videoOrPlaylist) |
125 | 125 | ||
126 | const oldTorrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, videoFile.torrentFilename) | 126 | const oldTorrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, videoFile.torrentFilename) |
@@ -133,15 +133,19 @@ async function updateTorrentUrls (videoOrPlaylist: MVideo | MStreamingPlaylistVi | |||
133 | 133 | ||
134 | decoded['url-list'] = buildUrlList(video, videoFile) | 134 | decoded['url-list'] = buildUrlList(video, videoFile) |
135 | 135 | ||
136 | decoded.info.name = buildInfoName(video, videoFile) | ||
137 | decoded['creation date'] = Math.ceil(Date.now() / 1000) | ||
138 | |||
136 | const newTorrentFilename = generateTorrentFileName(videoOrPlaylist, videoFile.resolution) | 139 | const newTorrentFilename = generateTorrentFileName(videoOrPlaylist, videoFile.resolution) |
137 | const newTorrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, newTorrentFilename) | 140 | const newTorrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, newTorrentFilename) |
138 | 141 | ||
139 | logger.info('Updating torrent URLs %s -> %s.', oldTorrentPath, newTorrentPath) | 142 | logger.info('Updating torrent metadata %s -> %s.', oldTorrentPath, newTorrentPath) |
140 | 143 | ||
141 | await writeFile(newTorrentPath, encode(decoded)) | 144 | await writeFile(newTorrentPath, encode(decoded)) |
142 | await remove(join(CONFIG.STORAGE.TORRENTS_DIR, videoFile.torrentFilename)) | 145 | await remove(join(CONFIG.STORAGE.TORRENTS_DIR, videoFile.torrentFilename)) |
143 | 146 | ||
144 | videoFile.torrentFilename = newTorrentFilename | 147 | videoFile.torrentFilename = newTorrentFilename |
148 | videoFile.infoHash = sha1(encode(decoded.info)) | ||
145 | } | 149 | } |
146 | 150 | ||
147 | function generateMagnetUri ( | 151 | function generateMagnetUri ( |
@@ -171,7 +175,7 @@ function generateMagnetUri ( | |||
171 | 175 | ||
172 | export { | 176 | export { |
173 | createTorrentPromise, | 177 | createTorrentPromise, |
174 | updateTorrentUrls, | 178 | updateTorrentMetadata, |
175 | createTorrentAndSetInfoHash, | 179 | createTorrentAndSetInfoHash, |
176 | generateMagnetUri, | 180 | generateMagnetUri, |
177 | downloadWebTorrentVideo | 181 | downloadWebTorrentVideo |
@@ -226,3 +230,7 @@ function buildAnnounceList () { | |||
226 | function buildUrlList (video: MVideo, videoFile: MVideoFile) { | 230 | function buildUrlList (video: MVideo, videoFile: MVideoFile) { |
227 | return [ videoFile.getFileUrl(video) ] | 231 | return [ videoFile.getFileUrl(video) ] |
228 | } | 232 | } |
233 | |||
234 | function buildInfoName (video: MVideo, videoFile: MVideoFile) { | ||
235 | return `${video.name} ${videoFile.resolution}p${videoFile.extname}` | ||
236 | } | ||