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