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