aboutsummaryrefslogtreecommitdiffhomepage
path: root/scripts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2022-10-12 16:09:02 +0200
committerChocobozzz <chocobozzz@cpy.re>2022-10-24 14:48:24 +0200
commit3545e72c686ff1725bbdfd8d16d693e2f4aa75a3 (patch)
treee7f1d12ef5dae1e1142c3a8d0b681c1dbbb0de10 /scripts
parent38a3ccc7f8ad0ea94362b58c732af7c387ab46be (diff)
downloadPeerTube-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.ts74
-rwxr-xr-xscripts/prune-storage.ts28
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 @@
1import { pathExists, stat, writeFile } from 'fs-extra'
2import parseTorrent from 'parse-torrent'
3import { join } from 'path'
4import * as Sequelize from 'sequelize'
5import { logger } from '@server/helpers/logger'
6import { createTorrentPromise } from '@server/helpers/webtorrent'
7import { CONFIG } from '@server/initializers/config'
8import { HLS_STREAMING_PLAYLIST_DIRECTORY, STATIC_PATHS, WEBSERVER } from '@server/initializers/constants'
9import { initDatabaseModels, sequelizeTypescript } from '../../server/initializers/database'
10
11run()
12 .then(() => process.exit(0))
13 .catch(err => {
14 console.error(err)
15 process.exit(-1)
16 })
17
18async 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'
2import { readdir, remove, stat } from 'fs-extra' 2import { readdir, remove, stat } from 'fs-extra'
3import { basename, join } from 'path' 3import { basename, join } from 'path'
4import { get, start } from 'prompt' 4import { get, start } from 'prompt'
5import { HLS_REDUNDANCY_DIRECTORY, HLS_STREAMING_PLAYLIST_DIRECTORY } from '@server/initializers/constants' 5import { DIRECTORIES } from '@server/initializers/constants'
6import { VideoFileModel } from '@server/models/video/video-file' 6import { VideoFileModel } from '@server/models/video/video-file'
7import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist' 7import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist'
8import { uniqify } from '@shared/core-utils' 8import { 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
78type ExistFun = (file: string) => Promise<boolean> 80type ExistFun = (file: string) => Promise<boolean> | boolean
79async function pruneDirectory (directory: string, existFun: ExistFun) { 81async 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
94function doesWebTorrentFileExist () { 96function 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
98function doesHLSPlaylistExist () { 105function 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
102function doesTorrentFileExist () { 114function 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)