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