]>
Commit | Line | Data |
---|---|---|
1 | import { registerTSPaths } from '../server/helpers/register-ts-paths' | |
2 | registerTSPaths() | |
3 | ||
4 | import * as prompt from 'prompt' | |
5 | import { join } 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 } from 'fs-extra' | |
10 | import { VideoRedundancyModel } from '../server/models/redundancy/video-redundancy' | |
11 | import * as Bluebird from 'bluebird' | |
12 | import { getUUIDFromFilename } from '../server/helpers/utils' | |
13 | import { ThumbnailModel } from '../server/models/video/thumbnail' | |
14 | import { ActorImageModel } from '../server/models/account/actor-image' | |
15 | import { uniq, values } from 'lodash' | |
16 | import { ThumbnailType } from '@shared/models' | |
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 () { | |
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 | ||
33 | await initDatabaseModels(true) | |
34 | ||
35 | let toDelete: string[] = [] | |
36 | ||
37 | toDelete = toDelete.concat( | |
38 | await pruneDirectory(CONFIG.STORAGE.VIDEOS_DIR, doesVideoExist(true)), | |
39 | await pruneDirectory(CONFIG.STORAGE.TORRENTS_DIR, doesVideoExist(true)), | |
40 | ||
41 | await pruneDirectory(CONFIG.STORAGE.REDUNDANCY_DIR, doesRedundancyExist), | |
42 | ||
43 | await pruneDirectory(CONFIG.STORAGE.PREVIEWS_DIR, doesThumbnailExist(true, ThumbnailType.PREVIEW)), | |
44 | await pruneDirectory(CONFIG.STORAGE.THUMBNAILS_DIR, doesThumbnailExist(false, ThumbnailType.MINIATURE)), | |
45 | ||
46 | await pruneDirectory(CONFIG.STORAGE.ACTOR_IMAGES, doesActorImageExist) | |
47 | ) | |
48 | ||
49 | const tmpFiles = await readdir(CONFIG.STORAGE.TMP_DIR) | |
50 | toDelete = toDelete.concat(tmpFiles.map(t => join(CONFIG.STORAGE.TMP_DIR, t))) | |
51 | ||
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) { | |
64 | await remove(path) | |
65 | } | |
66 | ||
67 | console.log('Done!') | |
68 | } else { | |
69 | console.log('Exiting without deleting files.') | |
70 | } | |
71 | } | |
72 | ||
73 | type ExistFun = (file: string) => Promise<boolean> | |
74 | async function pruneDirectory (directory: string, existFun: ExistFun) { | |
75 | const files = await readdir(directory) | |
76 | ||
77 | const toDelete: string[] = [] | |
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) => { | |
89 | const uuid = getUUIDFromFilename(file) | |
90 | const video = await VideoModel.loadByUUID(uuid) | |
91 | ||
92 | return video && (keepOnlyOwned === false || video.isOwned()) | |
93 | } | |
94 | } | |
95 | ||
96 | function doesThumbnailExist (keepOnlyOwned: boolean, type: ThumbnailType) { | |
97 | return async (file: string) => { | |
98 | const thumbnail = await ThumbnailModel.loadByFilename(file, type) | |
99 | if (!thumbnail) return false | |
100 | ||
101 | if (keepOnlyOwned) { | |
102 | const video = await VideoModel.load(thumbnail.videoId) | |
103 | if (video.isOwned() === false) return false | |
104 | } | |
105 | ||
106 | return true | |
107 | } | |
108 | } | |
109 | ||
110 | async function doesActorImageExist (file: string) { | |
111 | const image = await ActorImageModel.loadByName(file) | |
112 | ||
113 | return !!image | |
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 | ||
138 | const videoFile = video.getWebTorrentFile(resolution) | |
139 | if (!videoFile) { | |
140 | console.error('Cannot find webtorrent file of video %s - %d', video.url, resolution) | |
141 | return true | |
142 | } | |
143 | ||
144 | const redundancy = await VideoRedundancyModel.loadLocalByFileId(videoFile.id) | |
145 | return !!redundancy | |
146 | } | |
147 | ||
148 | async function askConfirmation () { | |
149 | return new Promise((res, rej) => { | |
150 | prompt.start() | |
151 | const schema = { | |
152 | properties: { | |
153 | confirm: { | |
154 | type: 'string', | |
155 | description: 'These following unused files can be deleted, but please check your backups first (bugs happen).' + | |
156 | ' Notice PeerTube must have been stopped when your ran this script.' + | |
157 | ' Can we delete these files?', | |
158 | default: 'n', | |
159 | required: true | |
160 | } | |
161 | } | |
162 | } | |
163 | prompt.get(schema, function (err, result) { | |
164 | if (err) return rej(err) | |
165 | ||
166 | return res(result.confirm?.match(/y/) !== null) | |
167 | }) | |
168 | }) | |
169 | } |