]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - scripts/prune-storage.ts
Translated using Weblate (Occitan)
[github/Chocobozzz/PeerTube.git] / scripts / prune-storage.ts
CommitLineData
2aaa1a3f
C
1import { registerTSPaths } from '../server/helpers/register-ts-paths'
2registerTSPaths()
3
a9729e21 4import * as prompt from 'prompt'
a9729e21 5import { join } from 'path'
74dc3bca 6import { CONFIG } from '../server/initializers/config'
a9729e21 7import { VideoModel } from '../server/models/video/video'
80fdaf06 8import { initDatabaseModels } from '../server/initializers/database'
e2600d8b 9import { readdir, remove } from 'fs-extra'
5ce1208a 10import { VideoRedundancyModel } from '../server/models/redundancy/video-redundancy'
e2600d8b 11import * as Bluebird from 'bluebird'
edb4ffc7 12import { getUUIDFromFilename } from '../server/helpers/utils'
e2600d8b
C
13import { ThumbnailModel } from '../server/models/video/thumbnail'
14import { AvatarModel } from '../server/models/avatar/avatar'
40b89069 15import { uniq, values } from 'lodash'
a9729e21
C
16
17run()
18 .then(() => process.exit(0))
19 .catch(err => {
20 console.error(err)
21 process.exit(-1)
22 })
23
24async function run () {
40b89069
C
25 const dirs = values(CONFIG.STORAGE)
26
27 if (uniq(dirs).length !== dirs.length) {
28 console.error('Cannot prune storage because you put multiple storage keys in the same directory.')
29 process.exit(0)
30 }
31
a9729e21
C
32 await initDatabaseModels(true)
33
e2600d8b 34 let toDelete: string[] = []
a9729e21 35
e2600d8b
C
36 toDelete = toDelete.concat(
37 await pruneDirectory(CONFIG.STORAGE.VIDEOS_DIR, doesVideoExist(true)),
38 await pruneDirectory(CONFIG.STORAGE.TORRENTS_DIR, doesVideoExist(true)),
5ce1208a 39
e2600d8b 40 await pruneDirectory(CONFIG.STORAGE.REDUNDANCY_DIR, doesRedundancyExist),
5ce1208a 41
e2600d8b
C
42 await pruneDirectory(CONFIG.STORAGE.PREVIEWS_DIR, doesThumbnailExist(true)),
43 await pruneDirectory(CONFIG.STORAGE.THUMBNAILS_DIR, doesThumbnailExist(false)),
44
45 await pruneDirectory(CONFIG.STORAGE.AVATARS_DIR, doesAvatarExist)
46 )
a9729e21 47
be9727bd
C
48 const tmpFiles = await readdir(CONFIG.STORAGE.TMP_DIR)
49 toDelete = toDelete.concat(tmpFiles.map(t => join(CONFIG.STORAGE.TMP_DIR, t)))
3ba862da 50
a9729e21
C
51 if (toDelete.length === 0) {
52 console.log('No files to delete.')
53 return
54 }
55
56 console.log('Will delete %d files:\n\n%s\n\n', toDelete.length, toDelete.join('\n'))
57
58 const res = await askConfirmation()
59 if (res === true) {
60 console.log('Processing delete...\n')
61
62 for (const path of toDelete) {
62689b94 63 await remove(path)
a9729e21
C
64 }
65
66 console.log('Done!')
67 } else {
68 console.log('Exiting without deleting files.')
69 }
70}
71
e2600d8b
C
72type ExistFun = (file: string) => Promise<boolean>
73async function pruneDirectory (directory: string, existFun: ExistFun) {
62689b94 74 const files = await readdir(directory)
a9729e21
C
75
76 const toDelete: string[] = []
e2600d8b
C
77 await Bluebird.map(files, async file => {
78 if (await existFun(file) !== true) {
79 toDelete.push(join(directory, file))
80 }
81 }, { concurrency: 20 })
82
83 return toDelete
84}
85
86function doesVideoExist (keepOnlyOwned: boolean) {
87 return async (file: string) => {
a9729e21 88 const uuid = getUUIDFromFilename(file)
e2600d8b 89 const video = await VideoModel.loadByUUID(uuid)
a9729e21 90
e2600d8b
C
91 return video && (keepOnlyOwned === false || video.isOwned())
92 }
93}
a9729e21 94
e2600d8b
C
95function doesThumbnailExist (keepOnlyOwned: boolean) {
96 return async (file: string) => {
97 const thumbnail = await ThumbnailModel.loadByName(file)
98 if (!thumbnail) return false
99
100 if (keepOnlyOwned) {
101 const video = await VideoModel.load(thumbnail.videoId)
102 if (video.isOwned() === false) return false
5ce1208a 103 }
e2600d8b
C
104
105 return true
a9729e21 106 }
e2600d8b 107}
a9729e21 108
e2600d8b
C
109async function doesAvatarExist (file: string) {
110 const avatar = await AvatarModel.loadByName(file)
111
112 return !!avatar
113}
114
115async function doesRedundancyExist (file: string) {
116 const uuid = getUUIDFromFilename(file)
117 const video = await VideoModel.loadWithFiles(uuid)
118
119 if (!video) return false
120
121 const isPlaylist = file.includes('.') === false
122
123 if (isPlaylist) {
124 const p = video.getHLSPlaylist()
125 if (!p) return false
126
127 const redundancy = await VideoRedundancyModel.loadLocalByStreamingPlaylistId(p.id)
128 return !!redundancy
129 }
130
131 const resolution = parseInt(file.split('-')[5], 10)
132 if (isNaN(resolution)) {
133 console.error('Cannot prune %s because we cannot guess guess the resolution.', file)
134 return true
135 }
136
d7a25329 137 const videoFile = video.getWebTorrentFile(resolution)
e2600d8b 138 if (!videoFile) {
d7a25329 139 console.error('Cannot find webtorrent file of video %s - %d', video.url, resolution)
e2600d8b
C
140 return true
141 }
142
143 const redundancy = await VideoRedundancyModel.loadLocalByFileId(videoFile.id)
144 return !!redundancy
a9729e21
C
145}
146
a9729e21
C
147async function askConfirmation () {
148 return new Promise((res, rej) => {
149 prompt.start()
150 const schema = {
151 properties: {
152 confirm: {
153 type: 'string',
5ce1208a 154 description: 'These following unused files can be deleted, but please check your backups first (bugs happen).' +
7089e7b4 155 ' Notice PeerTube must have been stopped when your ran this script.' +
5ce1208a 156 ' Can we delete these files?',
a9729e21
C
157 default: 'n',
158 required: true
159 }
160 }
161 }
162 prompt.get(schema, function (err, result) {
163 if (err) return rej(err)
f0af38e6
C
164
165 return res(result.confirm?.match(/y/) !== null)
a9729e21
C
166 })
167 })
168}