diff options
Diffstat (limited to 'scripts/prune-storage.ts')
-rwxr-xr-x | scripts/prune-storage.ts | 62 |
1 files changed, 27 insertions, 35 deletions
diff --git a/scripts/prune-storage.ts b/scripts/prune-storage.ts index 58d24816e..5b029d215 100755 --- a/scripts/prune-storage.ts +++ b/scripts/prune-storage.ts | |||
@@ -2,11 +2,11 @@ import { registerTSPaths } from '../server/helpers/register-ts-paths' | |||
2 | registerTSPaths() | 2 | registerTSPaths() |
3 | 3 | ||
4 | import * as prompt from 'prompt' | 4 | import * as prompt from 'prompt' |
5 | import { join } from 'path' | 5 | import { join, basename } from 'path' |
6 | import { CONFIG } from '../server/initializers/config' | 6 | import { CONFIG } from '../server/initializers/config' |
7 | import { VideoModel } from '../server/models/video/video' | 7 | import { VideoModel } from '../server/models/video/video' |
8 | import { initDatabaseModels } from '../server/initializers/database' | 8 | import { initDatabaseModels } from '../server/initializers/database' |
9 | import { readdir, remove } from 'fs-extra' | 9 | import { readdir, remove, stat } from 'fs-extra' |
10 | import { VideoRedundancyModel } from '../server/models/redundancy/video-redundancy' | 10 | import { VideoRedundancyModel } from '../server/models/redundancy/video-redundancy' |
11 | import * as Bluebird from 'bluebird' | 11 | import * as Bluebird from 'bluebird' |
12 | import { getUUIDFromFilename } from '../server/helpers/utils' | 12 | import { getUUIDFromFilename } from '../server/helpers/utils' |
@@ -14,6 +14,7 @@ import { ThumbnailModel } from '../server/models/video/thumbnail' | |||
14 | import { ActorImageModel } from '../server/models/actor/actor-image' | 14 | import { ActorImageModel } from '../server/models/actor/actor-image' |
15 | import { uniq, values } from 'lodash' | 15 | import { uniq, values } from 'lodash' |
16 | import { ThumbnailType } from '@shared/models' | 16 | import { ThumbnailType } from '@shared/models' |
17 | import { VideoFileModel } from '@server/models/video/video-file' | ||
17 | 18 | ||
18 | run() | 19 | run() |
19 | .then(() => process.exit(0)) | 20 | .then(() => process.exit(0)) |
@@ -37,8 +38,8 @@ async function run () { | |||
37 | console.log('Detecting files to remove, it could take a while...') | 38 | console.log('Detecting files to remove, it could take a while...') |
38 | 39 | ||
39 | toDelete = toDelete.concat( | 40 | toDelete = toDelete.concat( |
40 | await pruneDirectory(CONFIG.STORAGE.VIDEOS_DIR, doesVideoExist(true)), | 41 | await pruneDirectory(CONFIG.STORAGE.VIDEOS_DIR, doesWebTorrentFileExist()), |
41 | await pruneDirectory(CONFIG.STORAGE.TORRENTS_DIR, doesVideoExist(true)), | 42 | await pruneDirectory(CONFIG.STORAGE.TORRENTS_DIR, doesTorrentFileExist()), |
42 | 43 | ||
43 | await pruneDirectory(CONFIG.STORAGE.REDUNDANCY_DIR, doesRedundancyExist), | 44 | await pruneDirectory(CONFIG.STORAGE.REDUNDANCY_DIR, doesRedundancyExist), |
44 | 45 | ||
@@ -78,26 +79,27 @@ async function pruneDirectory (directory: string, existFun: ExistFun) { | |||
78 | 79 | ||
79 | const toDelete: string[] = [] | 80 | const toDelete: string[] = [] |
80 | await Bluebird.map(files, async file => { | 81 | await Bluebird.map(files, async file => { |
81 | if (await existFun(file) !== true) { | 82 | const filePath = join(directory, file) |
82 | toDelete.push(join(directory, file)) | 83 | |
84 | if (await existFun(filePath) !== true) { | ||
85 | toDelete.push(filePath) | ||
83 | } | 86 | } |
84 | }, { concurrency: 20 }) | 87 | }, { concurrency: 20 }) |
85 | 88 | ||
86 | return toDelete | 89 | return toDelete |
87 | } | 90 | } |
88 | 91 | ||
89 | function doesVideoExist (keepOnlyOwned: boolean) { | 92 | function doesWebTorrentFileExist () { |
90 | return async (file: string) => { | 93 | return (filePath: string) => VideoFileModel.doesOwnedWebTorrentVideoFileExist(basename(filePath)) |
91 | const uuid = getUUIDFromFilename(file) | 94 | } |
92 | const video = await VideoModel.load(uuid) | ||
93 | 95 | ||
94 | return video && (keepOnlyOwned === false || video.isOwned()) | 96 | function doesTorrentFileExist () { |
95 | } | 97 | return (filePath: string) => VideoFileModel.doesOwnedTorrentFileExist(basename(filePath)) |
96 | } | 98 | } |
97 | 99 | ||
98 | function doesThumbnailExist (keepOnlyOwned: boolean, type: ThumbnailType) { | 100 | function doesThumbnailExist (keepOnlyOwned: boolean, type: ThumbnailType) { |
99 | return async (file: string) => { | 101 | return async (filePath: string) => { |
100 | const thumbnail = await ThumbnailModel.loadByFilename(file, type) | 102 | const thumbnail = await ThumbnailModel.loadByFilename(basename(filePath), type) |
101 | if (!thumbnail) return false | 103 | if (!thumbnail) return false |
102 | 104 | ||
103 | if (keepOnlyOwned) { | 105 | if (keepOnlyOwned) { |
@@ -109,21 +111,20 @@ function doesThumbnailExist (keepOnlyOwned: boolean, type: ThumbnailType) { | |||
109 | } | 111 | } |
110 | } | 112 | } |
111 | 113 | ||
112 | async function doesActorImageExist (file: string) { | 114 | async function doesActorImageExist (filePath: string) { |
113 | const image = await ActorImageModel.loadByName(file) | 115 | const image = await ActorImageModel.loadByName(basename(filePath)) |
114 | 116 | ||
115 | return !!image | 117 | return !!image |
116 | } | 118 | } |
117 | 119 | ||
118 | async function doesRedundancyExist (file: string) { | 120 | async function doesRedundancyExist (filePath: string) { |
119 | const uuid = getUUIDFromFilename(file) | 121 | const isPlaylist = (await stat(filePath)).isDirectory() |
120 | const video = await VideoModel.loadWithFiles(uuid) | ||
121 | |||
122 | if (!video) return false | ||
123 | |||
124 | const isPlaylist = file.includes('.') === false | ||
125 | 122 | ||
126 | if (isPlaylist) { | 123 | if (isPlaylist) { |
124 | const uuid = getUUIDFromFilename(filePath) | ||
125 | const video = await VideoModel.loadWithFiles(uuid) | ||
126 | if (!video) return false | ||
127 | |||
127 | const p = video.getHLSPlaylist() | 128 | const p = video.getHLSPlaylist() |
128 | if (!p) return false | 129 | if (!p) return false |
129 | 130 | ||
@@ -131,19 +132,10 @@ async function doesRedundancyExist (file: string) { | |||
131 | return !!redundancy | 132 | return !!redundancy |
132 | } | 133 | } |
133 | 134 | ||
134 | const resolution = parseInt(file.split('-')[5], 10) | 135 | const file = await VideoFileModel.loadByFilename(basename(filePath)) |
135 | if (isNaN(resolution)) { | 136 | if (!file) return false |
136 | console.error('Cannot prune %s because we cannot guess guess the resolution.', file) | ||
137 | return true | ||
138 | } | ||
139 | |||
140 | const videoFile = video.getWebTorrentFile(resolution) | ||
141 | if (!videoFile) { | ||
142 | console.error('Cannot find webtorrent file of video %s - %d', video.url, resolution) | ||
143 | return true | ||
144 | } | ||
145 | 137 | ||
146 | const redundancy = await VideoRedundancyModel.loadLocalByFileId(videoFile.id) | 138 | const redundancy = await VideoRedundancyModel.loadLocalByFileId(file.id) |
147 | return !!redundancy | 139 | return !!redundancy |
148 | } | 140 | } |
149 | 141 | ||