diff options
author | Chocobozzz <me@florianbigard.com> | 2022-10-12 16:09:02 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2022-10-24 14:48:24 +0200 |
commit | 3545e72c686ff1725bbdfd8d16d693e2f4aa75a3 (patch) | |
tree | e7f1d12ef5dae1e1142c3a8d0b681c1dbbb0de10 /scripts | |
parent | 38a3ccc7f8ad0ea94362b58c732af7c387ab46be (diff) | |
download | PeerTube-3545e72c686ff1725bbdfd8d16d693e2f4aa75a3.tar.gz PeerTube-3545e72c686ff1725bbdfd8d16d693e2f4aa75a3.tar.zst PeerTube-3545e72c686ff1725bbdfd8d16d693e2f4aa75a3.zip |
Put private videos under a specific subdirectory
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/migrations/peertube-2.1.ts | 74 | ||||
-rwxr-xr-x | scripts/prune-storage.ts | 28 |
2 files changed, 20 insertions, 82 deletions
diff --git a/scripts/migrations/peertube-2.1.ts b/scripts/migrations/peertube-2.1.ts deleted file mode 100644 index 2e316d996..000000000 --- a/scripts/migrations/peertube-2.1.ts +++ /dev/null | |||
@@ -1,74 +0,0 @@ | |||
1 | import { pathExists, stat, writeFile } from 'fs-extra' | ||
2 | import parseTorrent from 'parse-torrent' | ||
3 | import { join } from 'path' | ||
4 | import * as Sequelize from 'sequelize' | ||
5 | import { logger } from '@server/helpers/logger' | ||
6 | import { createTorrentPromise } from '@server/helpers/webtorrent' | ||
7 | import { CONFIG } from '@server/initializers/config' | ||
8 | import { HLS_STREAMING_PLAYLIST_DIRECTORY, STATIC_PATHS, WEBSERVER } from '@server/initializers/constants' | ||
9 | import { initDatabaseModels, sequelizeTypescript } from '../../server/initializers/database' | ||
10 | |||
11 | run() | ||
12 | .then(() => process.exit(0)) | ||
13 | .catch(err => { | ||
14 | console.error(err) | ||
15 | process.exit(-1) | ||
16 | }) | ||
17 | |||
18 | async function run () { | ||
19 | logger.info('Creating torrents and updating database for HSL files.') | ||
20 | |||
21 | await initDatabaseModels(true) | ||
22 | |||
23 | const query = 'select "videoFile".id as id, "videoFile".resolution as resolution, "video".uuid as uuid from "videoFile" ' + | ||
24 | 'inner join "videoStreamingPlaylist" ON "videoStreamingPlaylist".id = "videoFile"."videoStreamingPlaylistId" ' + | ||
25 | 'inner join video ON video.id = "videoStreamingPlaylist"."videoId" ' + | ||
26 | 'WHERE video.remote IS FALSE' | ||
27 | const options = { | ||
28 | type: Sequelize.QueryTypes.SELECT | ||
29 | } | ||
30 | const res = await sequelizeTypescript.query(query, options) | ||
31 | |||
32 | for (const row of res) { | ||
33 | const videoFilename = `${row['uuid']}-${row['resolution']}-fragmented.mp4` | ||
34 | const videoFilePath = join(HLS_STREAMING_PLAYLIST_DIRECTORY, row['uuid'], videoFilename) | ||
35 | |||
36 | logger.info('Processing %s.', videoFilePath) | ||
37 | |||
38 | if (!await pathExists(videoFilePath)) { | ||
39 | console.warn('Cannot generate torrent of %s: file does not exist.', videoFilePath) | ||
40 | continue | ||
41 | } | ||
42 | |||
43 | const createTorrentOptions = { | ||
44 | // Keep the extname, it's used by the client to stream the file inside a web browser | ||
45 | name: `video ${row['uuid']}`, | ||
46 | createdBy: 'PeerTube', | ||
47 | announceList: [ | ||
48 | [ WEBSERVER.WS + '://' + WEBSERVER.HOSTNAME + ':' + WEBSERVER.PORT + '/tracker/socket' ], | ||
49 | [ WEBSERVER.URL + '/tracker/announce' ] | ||
50 | ], | ||
51 | urlList: [ WEBSERVER.URL + join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, row['uuid'], videoFilename) ] | ||
52 | } | ||
53 | const torrent = await createTorrentPromise(videoFilePath, createTorrentOptions) | ||
54 | |||
55 | const torrentName = `${row['uuid']}-${row['resolution']}-hls.torrent` | ||
56 | const filePath = join(CONFIG.STORAGE.TORRENTS_DIR, torrentName) | ||
57 | |||
58 | await writeFile(filePath, torrent) | ||
59 | |||
60 | const parsedTorrent = parseTorrent(torrent) | ||
61 | const infoHash = parsedTorrent.infoHash | ||
62 | |||
63 | const stats = await stat(videoFilePath) | ||
64 | const size = stats.size | ||
65 | |||
66 | const queryUpdate = 'UPDATE "videoFile" SET "infoHash" = ?, "size" = ? WHERE id = ?' | ||
67 | |||
68 | const options = { | ||
69 | type: Sequelize.QueryTypes.UPDATE, | ||
70 | replacements: [ infoHash, size, row['id'] ] | ||
71 | } | ||
72 | await sequelizeTypescript.query(queryUpdate, options) | ||
73 | } | ||
74 | } | ||
diff --git a/scripts/prune-storage.ts b/scripts/prune-storage.ts index 3012bdb94..d19594a60 100755 --- a/scripts/prune-storage.ts +++ b/scripts/prune-storage.ts | |||
@@ -2,7 +2,7 @@ import { map } from 'bluebird' | |||
2 | import { readdir, remove, stat } from 'fs-extra' | 2 | import { readdir, remove, stat } from 'fs-extra' |
3 | import { basename, join } from 'path' | 3 | import { basename, join } from 'path' |
4 | import { get, start } from 'prompt' | 4 | import { get, start } from 'prompt' |
5 | import { HLS_REDUNDANCY_DIRECTORY, HLS_STREAMING_PLAYLIST_DIRECTORY } from '@server/initializers/constants' | 5 | import { DIRECTORIES } from '@server/initializers/constants' |
6 | import { VideoFileModel } from '@server/models/video/video-file' | 6 | import { VideoFileModel } from '@server/models/video/video-file' |
7 | import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist' | 7 | import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist' |
8 | import { uniqify } from '@shared/core-utils' | 8 | import { uniqify } from '@shared/core-utils' |
@@ -37,9 +37,11 @@ async function run () { | |||
37 | console.log('Detecting files to remove, it could take a while...') | 37 | console.log('Detecting files to remove, it could take a while...') |
38 | 38 | ||
39 | toDelete = toDelete.concat( | 39 | toDelete = toDelete.concat( |
40 | await pruneDirectory(CONFIG.STORAGE.VIDEOS_DIR, doesWebTorrentFileExist()), | 40 | await pruneDirectory(DIRECTORIES.VIDEOS.PUBLIC, doesWebTorrentFileExist()), |
41 | await pruneDirectory(DIRECTORIES.VIDEOS.PRIVATE, doesWebTorrentFileExist()), | ||
41 | 42 | ||
42 | await pruneDirectory(HLS_STREAMING_PLAYLIST_DIRECTORY, doesHLSPlaylistExist()), | 43 | await pruneDirectory(DIRECTORIES.HLS_STREAMING_PLAYLIST.PRIVATE, doesHLSPlaylistExist()), |
44 | await pruneDirectory(DIRECTORIES.HLS_STREAMING_PLAYLIST.PUBLIC, doesHLSPlaylistExist()), | ||
43 | 45 | ||
44 | await pruneDirectory(CONFIG.STORAGE.TORRENTS_DIR, doesTorrentFileExist()), | 46 | await pruneDirectory(CONFIG.STORAGE.TORRENTS_DIR, doesTorrentFileExist()), |
45 | 47 | ||
@@ -75,7 +77,7 @@ async function run () { | |||
75 | } | 77 | } |
76 | } | 78 | } |
77 | 79 | ||
78 | type ExistFun = (file: string) => Promise<boolean> | 80 | type ExistFun = (file: string) => Promise<boolean> | boolean |
79 | async function pruneDirectory (directory: string, existFun: ExistFun) { | 81 | async function pruneDirectory (directory: string, existFun: ExistFun) { |
80 | const files = await readdir(directory) | 82 | const files = await readdir(directory) |
81 | 83 | ||
@@ -92,11 +94,21 @@ async function pruneDirectory (directory: string, existFun: ExistFun) { | |||
92 | } | 94 | } |
93 | 95 | ||
94 | function doesWebTorrentFileExist () { | 96 | function doesWebTorrentFileExist () { |
95 | return (filePath: string) => VideoFileModel.doesOwnedWebTorrentVideoFileExist(basename(filePath)) | 97 | return (filePath: string) => { |
98 | // Don't delete private directory | ||
99 | if (filePath === DIRECTORIES.VIDEOS.PRIVATE) return true | ||
100 | |||
101 | return VideoFileModel.doesOwnedWebTorrentVideoFileExist(basename(filePath)) | ||
102 | } | ||
96 | } | 103 | } |
97 | 104 | ||
98 | function doesHLSPlaylistExist () { | 105 | function doesHLSPlaylistExist () { |
99 | return (hlsPath: string) => VideoStreamingPlaylistModel.doesOwnedHLSPlaylistExist(basename(hlsPath)) | 106 | return (hlsPath: string) => { |
107 | // Don't delete private directory | ||
108 | if (hlsPath === DIRECTORIES.HLS_STREAMING_PLAYLIST.PRIVATE) return true | ||
109 | |||
110 | return VideoStreamingPlaylistModel.doesOwnedHLSPlaylistExist(basename(hlsPath)) | ||
111 | } | ||
100 | } | 112 | } |
101 | 113 | ||
102 | function doesTorrentFileExist () { | 114 | function doesTorrentFileExist () { |
@@ -127,8 +139,8 @@ async function doesRedundancyExist (filePath: string) { | |||
127 | const isPlaylist = (await stat(filePath)).isDirectory() | 139 | const isPlaylist = (await stat(filePath)).isDirectory() |
128 | 140 | ||
129 | if (isPlaylist) { | 141 | if (isPlaylist) { |
130 | // Don't delete HLS directory | 142 | // Don't delete HLS redundancy directory |
131 | if (filePath === HLS_REDUNDANCY_DIRECTORY) return true | 143 | if (filePath === DIRECTORIES.HLS_REDUNDANCY) return true |
132 | 144 | ||
133 | const uuid = getUUIDFromFilename(filePath) | 145 | const uuid = getUUIDFromFilename(filePath) |
134 | const video = await VideoModel.loadWithFiles(uuid) | 146 | const video = await VideoModel.loadWithFiles(uuid) |