]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - scripts/prune-storage.ts
Faster ci using compiled ts files
[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 13import { ThumbnailModel } from '../server/models/video/thumbnail'
7d9ba5c0 14import { ActorImageModel } from '../server/models/actor/actor-image'
40b89069 15import { uniq, values } from 'lodash'
a8b1b404 16import { ThumbnailType } from '@shared/models'
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 () {
40b89069
C
26 const dirs = values(CONFIG.STORAGE)
27
28 if (uniq(dirs).length !== dirs.length) {
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
C
39 toDelete = toDelete.concat(
40 await pruneDirectory(CONFIG.STORAGE.VIDEOS_DIR, doesVideoExist(true)),
41 await pruneDirectory(CONFIG.STORAGE.TORRENTS_DIR, doesVideoExist(true)),
5ce1208a 42
e2600d8b 43 await pruneDirectory(CONFIG.STORAGE.REDUNDANCY_DIR, doesRedundancyExist),
5ce1208a 44
a8b1b404
C
45 await pruneDirectory(CONFIG.STORAGE.PREVIEWS_DIR, doesThumbnailExist(true, ThumbnailType.PREVIEW)),
46 await pruneDirectory(CONFIG.STORAGE.THUMBNAILS_DIR, doesThumbnailExist(false, ThumbnailType.MINIATURE)),
e2600d8b 47
f4796856 48 await pruneDirectory(CONFIG.STORAGE.ACTOR_IMAGES, doesActorImageExist)
e2600d8b 49 )
a9729e21 50
be9727bd
C
51 const tmpFiles = await readdir(CONFIG.STORAGE.TMP_DIR)
52 toDelete = toDelete.concat(tmpFiles.map(t => join(CONFIG.STORAGE.TMP_DIR, t)))
3ba862da 53
a9729e21
C
54 if (toDelete.length === 0) {
55 console.log('No files to delete.')
56 return
57 }
58
59 console.log('Will delete %d files:\n\n%s\n\n', toDelete.length, toDelete.join('\n'))
60
61 const res = await askConfirmation()
62 if (res === true) {
63 console.log('Processing delete...\n')
64
65 for (const path of toDelete) {
62689b94 66 await remove(path)
a9729e21
C
67 }
68
69 console.log('Done!')
70 } else {
71 console.log('Exiting without deleting files.')
72 }
73}
74
e2600d8b
C
75type ExistFun = (file: string) => Promise<boolean>
76async function pruneDirectory (directory: string, existFun: ExistFun) {
62689b94 77 const files = await readdir(directory)
a9729e21
C
78
79 const toDelete: string[] = []
e2600d8b
C
80 await Bluebird.map(files, async file => {
81 if (await existFun(file) !== true) {
82 toDelete.push(join(directory, file))
83 }
84 }, { concurrency: 20 })
85
86 return toDelete
87}
88
89function doesVideoExist (keepOnlyOwned: boolean) {
90 return async (file: string) => {
a9729e21 91 const uuid = getUUIDFromFilename(file)
71d4af1e 92 const video = await VideoModel.load(uuid)
a9729e21 93
e2600d8b
C
94 return video && (keepOnlyOwned === false || video.isOwned())
95 }
96}
a9729e21 97
a8b1b404 98function doesThumbnailExist (keepOnlyOwned: boolean, type: ThumbnailType) {
e2600d8b 99 return async (file: string) => {
a35a2279 100 const thumbnail = await ThumbnailModel.loadByFilename(file, type)
e2600d8b
C
101 if (!thumbnail) return false
102
103 if (keepOnlyOwned) {
104 const video = await VideoModel.load(thumbnail.videoId)
105 if (video.isOwned() === false) return false
5ce1208a 106 }
e2600d8b
C
107
108 return true
a9729e21 109 }
e2600d8b 110}
a9729e21 111
f4796856
C
112async function doesActorImageExist (file: string) {
113 const image = await ActorImageModel.loadByName(file)
e2600d8b 114
f4796856 115 return !!image
e2600d8b
C
116}
117
118async function doesRedundancyExist (file: string) {
119 const uuid = getUUIDFromFilename(file)
120 const video = await VideoModel.loadWithFiles(uuid)
121
122 if (!video) return false
123
124 const isPlaylist = file.includes('.') === false
125
126 if (isPlaylist) {
127 const p = video.getHLSPlaylist()
128 if (!p) return false
129
130 const redundancy = await VideoRedundancyModel.loadLocalByStreamingPlaylistId(p.id)
131 return !!redundancy
132 }
133
134 const resolution = parseInt(file.split('-')[5], 10)
135 if (isNaN(resolution)) {
136 console.error('Cannot prune %s because we cannot guess guess the resolution.', file)
137 return true
138 }
139
d7a25329 140 const videoFile = video.getWebTorrentFile(resolution)
e2600d8b 141 if (!videoFile) {
d7a25329 142 console.error('Cannot find webtorrent file of video %s - %d', video.url, resolution)
e2600d8b
C
143 return true
144 }
145
146 const redundancy = await VideoRedundancyModel.loadLocalByFileId(videoFile.id)
147 return !!redundancy
a9729e21
C
148}
149
a9729e21
C
150async function askConfirmation () {
151 return new Promise((res, rej) => {
152 prompt.start()
153 const schema = {
154 properties: {
155 confirm: {
156 type: 'string',
5ce1208a 157 description: 'These following unused files can be deleted, but please check your backups first (bugs happen).' +
7089e7b4 158 ' Notice PeerTube must have been stopped when your ran this script.' +
5ce1208a 159 ' Can we delete these files?',
a9729e21
C
160 default: 'n',
161 required: true
162 }
163 }
164 }
165 prompt.get(schema, function (err, result) {
166 if (err) return rej(err)
f0af38e6
C
167
168 return res(result.confirm?.match(/y/) !== null)
a9729e21
C
169 })
170 })
171}