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