]>
Commit | Line | Data |
---|---|---|
1 | import * as prompt from 'prompt' | |
2 | import { join } from 'path' | |
3 | import { CONFIG } from '../server/initializers/constants' | |
4 | import { VideoModel } from '../server/models/video/video' | |
5 | import { initDatabaseModels } from '../server/initializers' | |
6 | import { remove, readdir } from 'fs-extra' | |
7 | import { VideoRedundancyModel } from '../server/models/redundancy/video-redundancy' | |
8 | ||
9 | run() | |
10 | .then(() => process.exit(0)) | |
11 | .catch(err => { | |
12 | console.error(err) | |
13 | process.exit(-1) | |
14 | }) | |
15 | ||
16 | async function run () { | |
17 | await initDatabaseModels(true) | |
18 | ||
19 | const storageOnlyOwnedToPrune = [ | |
20 | CONFIG.STORAGE.VIDEOS_DIR, | |
21 | CONFIG.STORAGE.TORRENTS_DIR | |
22 | ] | |
23 | ||
24 | const storageForAllToPrune = [ | |
25 | CONFIG.STORAGE.PREVIEWS_DIR, | |
26 | CONFIG.STORAGE.THUMBNAILS_DIR | |
27 | ] | |
28 | ||
29 | let toDelete: string[] = [] | |
30 | for (const directory of storageOnlyOwnedToPrune) { | |
31 | toDelete = toDelete.concat(await pruneDirectory(directory, true)) | |
32 | } | |
33 | ||
34 | for (const directory of storageForAllToPrune) { | |
35 | toDelete = toDelete.concat(await pruneDirectory(directory, false)) | |
36 | } | |
37 | ||
38 | if (toDelete.length === 0) { | |
39 | console.log('No files to delete.') | |
40 | return | |
41 | } | |
42 | ||
43 | console.log('Will delete %d files:\n\n%s\n\n', toDelete.length, toDelete.join('\n')) | |
44 | ||
45 | const res = await askConfirmation() | |
46 | if (res === true) { | |
47 | console.log('Processing delete...\n') | |
48 | ||
49 | for (const path of toDelete) { | |
50 | await remove(path) | |
51 | } | |
52 | ||
53 | console.log('Done!') | |
54 | } else { | |
55 | console.log('Exiting without deleting files.') | |
56 | } | |
57 | } | |
58 | ||
59 | async function pruneDirectory (directory: string, onlyOwned = false) { | |
60 | const files = await readdir(directory) | |
61 | ||
62 | const toDelete: string[] = [] | |
63 | for (const file of files) { | |
64 | const uuid = getUUIDFromFilename(file) | |
65 | let video: VideoModel | |
66 | let localRedundancy: boolean | |
67 | ||
68 | if (uuid) { | |
69 | video = await VideoModel.loadByUUIDWithFile(uuid) | |
70 | localRedundancy = await VideoRedundancyModel.isLocalByVideoUUIDExists(uuid) | |
71 | } | |
72 | ||
73 | if ( | |
74 | !uuid || | |
75 | !video || | |
76 | (onlyOwned === true && (video.isOwned() === false && localRedundancy === false)) | |
77 | ) { | |
78 | toDelete.push(join(directory, file)) | |
79 | } | |
80 | } | |
81 | ||
82 | return toDelete | |
83 | } | |
84 | ||
85 | function getUUIDFromFilename (filename: string) { | |
86 | const regex = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/ | |
87 | const result = filename.match(regex) | |
88 | ||
89 | if (!result || Array.isArray(result) === false) return null | |
90 | ||
91 | return result[0] | |
92 | } | |
93 | ||
94 | async function askConfirmation () { | |
95 | return new Promise((res, rej) => { | |
96 | prompt.start() | |
97 | const schema = { | |
98 | properties: { | |
99 | confirm: { | |
100 | type: 'string', | |
101 | description: 'These following unused files can be deleted, but please check your backups first (bugs happen).' + | |
102 | ' Can we delete these files?', | |
103 | default: 'n', | |
104 | required: true | |
105 | } | |
106 | } | |
107 | } | |
108 | prompt.get(schema, function (err, result) { | |
109 | if (err) return rej(err) | |
110 | return res(result.confirm && result.confirm.match(/y/) !== null) | |
111 | }) | |
112 | }) | |
113 | } |