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