]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - scripts/prune-storage.ts
Don't not autoplay live without autoplay setting
[github/Chocobozzz/PeerTube.git] / scripts / prune-storage.ts
CommitLineData
41fb13c3 1import { map } from 'bluebird'
f8360396 2import { readdir, remove, stat } from 'fs-extra'
f8360396
C
3import { basename, join } from 'path'
4import { get, start } from 'prompt'
3545e72c 5import { DIRECTORIES } from '@server/initializers/constants'
f8360396 6import { VideoFileModel } from '@server/models/video/video-file'
90701ec1 7import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist'
690bb8f9 8import { uniqify } from '@shared/core-utils'
f8360396
C
9import { ThumbnailType } from '@shared/models'
10import { getUUIDFromFilename } from '../server/helpers/utils'
11import { CONFIG } from '../server/initializers/config'
12import { initDatabaseModels } from '../server/initializers/database'
13import { ActorImageModel } from '../server/models/actor/actor-image'
14import { VideoRedundancyModel } from '../server/models/redundancy/video-redundancy'
15import { ThumbnailModel } from '../server/models/video/thumbnail'
16import { VideoModel } from '../server/models/video/video'
a9729e21
C
17
18run()
19 .then(() => process.exit(0))
20 .catch(err => {
21 console.error(err)
22 process.exit(-1)
23 })
24
25async function run () {
690bb8f9 26 const dirs = Object.values(CONFIG.STORAGE)
40b89069 27
690bb8f9 28 if (uniqify(dirs).length !== dirs.length) {
40b89069
C
29 console.error('Cannot prune storage because you put multiple storage keys in the same directory.')
30 process.exit(0)
31 }
32
a9729e21
C
33 await initDatabaseModels(true)
34
e2600d8b 35 let toDelete: string[] = []
a9729e21 36
6b4e74c2
C
37 console.log('Detecting files to remove, it could take a while...')
38
e2600d8b 39 toDelete = toDelete.concat(
3545e72c
C
40 await pruneDirectory(DIRECTORIES.VIDEOS.PUBLIC, doesWebTorrentFileExist()),
41 await pruneDirectory(DIRECTORIES.VIDEOS.PRIVATE, doesWebTorrentFileExist()),
90701ec1 42
3545e72c
C
43 await pruneDirectory(DIRECTORIES.HLS_STREAMING_PLAYLIST.PRIVATE, doesHLSPlaylistExist()),
44 await pruneDirectory(DIRECTORIES.HLS_STREAMING_PLAYLIST.PUBLIC, doesHLSPlaylistExist()),
90701ec1 45
764b1a14 46 await pruneDirectory(CONFIG.STORAGE.TORRENTS_DIR, doesTorrentFileExist()),
5ce1208a 47
e2600d8b 48 await pruneDirectory(CONFIG.STORAGE.REDUNDANCY_DIR, doesRedundancyExist),
5ce1208a 49
a8b1b404
C
50 await pruneDirectory(CONFIG.STORAGE.PREVIEWS_DIR, doesThumbnailExist(true, ThumbnailType.PREVIEW)),
51 await pruneDirectory(CONFIG.STORAGE.THUMBNAILS_DIR, doesThumbnailExist(false, ThumbnailType.MINIATURE)),
e2600d8b 52
f4796856 53 await pruneDirectory(CONFIG.STORAGE.ACTOR_IMAGES, doesActorImageExist)
e2600d8b 54 )
a9729e21 55
be9727bd
C
56 const tmpFiles = await readdir(CONFIG.STORAGE.TMP_DIR)
57 toDelete = toDelete.concat(tmpFiles.map(t => join(CONFIG.STORAGE.TMP_DIR, t)))
3ba862da 58
a9729e21
C
59 if (toDelete.length === 0) {
60 console.log('No files to delete.')
61 return
62 }
63
64 console.log('Will delete %d files:\n\n%s\n\n', toDelete.length, toDelete.join('\n'))
65
66 const res = await askConfirmation()
67 if (res === true) {
68 console.log('Processing delete...\n')
69
70 for (const path of toDelete) {
62689b94 71 await remove(path)
a9729e21
C
72 }
73
74 console.log('Done!')
75 } else {
76 console.log('Exiting without deleting files.')
77 }
78}
79
3545e72c 80type ExistFun = (file: string) => Promise<boolean> | boolean
e2600d8b 81async function pruneDirectory (directory: string, existFun: ExistFun) {
62689b94 82 const files = await readdir(directory)
a9729e21
C
83
84 const toDelete: string[] = []
41fb13c3 85 await map(files, async file => {
764b1a14
C
86 const filePath = join(directory, file)
87
88 if (await existFun(filePath) !== true) {
89 toDelete.push(filePath)
e2600d8b
C
90 }
91 }, { concurrency: 20 })
92
93 return toDelete
94}
95
764b1a14 96function doesWebTorrentFileExist () {
3545e72c
C
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 }
764b1a14 103}
a9729e21 104
90701ec1 105function doesHLSPlaylistExist () {
3545e72c
C
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 }
90701ec1
C
112}
113
764b1a14
C
114function doesTorrentFileExist () {
115 return (filePath: string) => VideoFileModel.doesOwnedTorrentFileExist(basename(filePath))
e2600d8b 116}
a9729e21 117
a8b1b404 118function doesThumbnailExist (keepOnlyOwned: boolean, type: ThumbnailType) {
764b1a14
C
119 return async (filePath: string) => {
120 const thumbnail = await ThumbnailModel.loadByFilename(basename(filePath), type)
e2600d8b
C
121 if (!thumbnail) return false
122
123 if (keepOnlyOwned) {
124 const video = await VideoModel.load(thumbnail.videoId)
125 if (video.isOwned() === false) return false
5ce1208a 126 }
e2600d8b
C
127
128 return true
a9729e21 129 }
e2600d8b 130}
a9729e21 131
764b1a14
C
132async function doesActorImageExist (filePath: string) {
133 const image = await ActorImageModel.loadByName(basename(filePath))
e2600d8b 134
f4796856 135 return !!image
e2600d8b
C
136}
137
764b1a14
C
138async function doesRedundancyExist (filePath: string) {
139 const isPlaylist = (await stat(filePath)).isDirectory()
e2600d8b
C
140
141 if (isPlaylist) {
3545e72c
C
142 // Don't delete HLS redundancy directory
143 if (filePath === DIRECTORIES.HLS_REDUNDANCY) return true
2ede0715 144
764b1a14
C
145 const uuid = getUUIDFromFilename(filePath)
146 const video = await VideoModel.loadWithFiles(uuid)
147 if (!video) return false
148
e2600d8b
C
149 const p = video.getHLSPlaylist()
150 if (!p) return false
151
152 const redundancy = await VideoRedundancyModel.loadLocalByStreamingPlaylistId(p.id)
153 return !!redundancy
154 }
155
764b1a14
C
156 const file = await VideoFileModel.loadByFilename(basename(filePath))
157 if (!file) return false
e2600d8b 158
764b1a14 159 const redundancy = await VideoRedundancyModel.loadLocalByFileId(file.id)
e2600d8b 160 return !!redundancy
a9729e21
C
161}
162
a9729e21
C
163async function askConfirmation () {
164 return new Promise((res, rej) => {
41fb13c3 165 start()
a9729e21
C
166 const schema = {
167 properties: {
168 confirm: {
169 type: 'string',
5ce1208a 170 description: 'These following unused files can be deleted, but please check your backups first (bugs happen).' +
7089e7b4 171 ' Notice PeerTube must have been stopped when your ran this script.' +
5ce1208a 172 ' Can we delete these files?',
a9729e21
C
173 default: 'n',
174 required: true
175 }
176 }
177 }
41fb13c3 178 get(schema, function (err, result) {
a9729e21 179 if (err) return rej(err)
f0af38e6
C
180
181 return res(result.confirm?.match(/y/) !== null)
a9729e21
C
182 })
183 })
184}