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