]>
Commit | Line | Data |
---|---|---|
a9729e21 | 1 | import * as prompt from 'prompt' |
a9729e21 | 2 | import { join } from 'path' |
a9729e21 C |
3 | import { readdirPromise, unlinkPromise } from '../server/helpers/core-utils' |
4 | import { CONFIG } from '../server/initializers/constants' | |
5 | import { VideoModel } from '../server/models/video/video' | |
6 | import { initDatabaseModels } from '../server/initializers' | |
7 | ||
8 | run() | |
9 | .then(() => process.exit(0)) | |
10 | .catch(err => { | |
11 | console.error(err) | |
12 | process.exit(-1) | |
13 | }) | |
14 | ||
15 | async function run () { | |
16 | await initDatabaseModels(true) | |
17 | ||
18 | const storageToPrune = [ | |
19 | CONFIG.STORAGE.VIDEOS_DIR, | |
20 | CONFIG.STORAGE.PREVIEWS_DIR, | |
21 | CONFIG.STORAGE.THUMBNAILS_DIR, | |
22 | CONFIG.STORAGE.TORRENTS_DIR | |
23 | ] | |
24 | ||
25 | let toDelete: string[] = [] | |
26 | for (const directory of storageToPrune) { | |
27 | toDelete = toDelete.concat(await pruneDirectory(directory)) | |
28 | } | |
29 | ||
30 | if (toDelete.length === 0) { | |
31 | console.log('No files to delete.') | |
32 | return | |
33 | } | |
34 | ||
35 | console.log('Will delete %d files:\n\n%s\n\n', toDelete.length, toDelete.join('\n')) | |
36 | ||
37 | const res = await askConfirmation() | |
38 | if (res === true) { | |
39 | console.log('Processing delete...\n') | |
40 | ||
41 | for (const path of toDelete) { | |
42 | await unlinkPromise(path) | |
43 | } | |
44 | ||
45 | console.log('Done!') | |
46 | } else { | |
47 | console.log('Exiting without deleting files.') | |
48 | } | |
49 | } | |
50 | ||
51 | async function pruneDirectory (directory: string) { | |
52 | const files = await readdirPromise(directory) | |
53 | ||
54 | const toDelete: string[] = [] | |
55 | for (const file of files) { | |
56 | const uuid = getUUIDFromFilename(file) | |
57 | let video: VideoModel | |
58 | ||
59 | if (uuid) video = await VideoModel.loadByUUID(uuid) | |
60 | ||
61 | if (!uuid || !video) toDelete.push(join(directory, file)) | |
62 | } | |
63 | ||
64 | return toDelete | |
65 | } | |
66 | ||
67 | function getUUIDFromFilename (filename: string) { | |
68 | const regex = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/ | |
69 | const result = filename.match(regex) | |
70 | ||
71 | if (!result || Array.isArray(result) === false) return null | |
72 | ||
73 | return result[0] | |
74 | } | |
75 | ||
76 | async function askConfirmation () { | |
77 | return new Promise((res, rej) => { | |
78 | prompt.start() | |
79 | const schema = { | |
80 | properties: { | |
81 | confirm: { | |
82 | type: 'string', | |
83 | description: 'Are you sure you want to delete these files? Please check carefully', | |
84 | default: 'n', | |
85 | required: true | |
86 | } | |
87 | } | |
88 | } | |
89 | prompt.get(schema, function (err, result) { | |
90 | if (err) return rej(err) | |
91 | return res(result.confirm && result.confirm.match(/y/) !== null) | |
92 | }) | |
93 | }) | |
94 | } |