-
Notifications
You must be signed in to change notification settings - Fork 978
/
Copy pathdeleteOrphanFiles.php
83 lines (76 loc) · 2.78 KB
/
deleteOrphanFiles.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
//streamer config
require_once '../videos/configuration.php';
if (!isCommandLineInterface()) {
return die('Command Line only');
}
$doNotDeleteFilesList = ['configuration.php', 'favicon.ico', 'favicon.png', 'avideo.log', 'PayPal.log', 'socketPID.log', 'logo.png', 'logoOverlay.png'];
$lockFilename = '.move_v1.lock';
$path = getVideosDir();
$files = array_diff(scandir($path), ['.', '..']);
echo "*** Total filenames " . count($files) . "\n";
foreach ($files as $key => $value) {
$dir = "{$path}{$value}";
if (!is_dir($dir)) {
$bname = basename($dir);
if (in_array($bname, $doNotDeleteFilesList) || preg_match('/configuration\./i', $bname)) {
unset($files[$key]);
} else {
$filename = Video::getCleanFilenameFromFile($dir);
$video = Video::getVideoFromFileName($filename, true);
if (!empty($video)) {
//echo "+++ Video FOUND for filename {$filename} ".PHP_EOL;
unset($files[$key]);
} else {
$files[$key] = [$value, $dir];
//echo "*** Video NOT found for filename {$filename} ".PHP_EOL;
}
}
continue;
}
$file = "{$dir}" . DIRECTORY_SEPARATOR . "{$lockFilename}";
if (file_exists($file)) {
$filename = Video::getCleanFilenameFromFile($dir);
$video = Video::getVideoFromFileName($filename, true);
if (!empty($video)) {
//echo "+++ Video FOUND for filename {$filename} ".PHP_EOL;
unset($files[$key]);
} else {
$files[$key] = [$value, $dir];
//echo "*** Video NOT found for filename {$filename} ".PHP_EOL;
}
} else {
//echo "*** Lock file does not exists {$file} ".PHP_EOL;
unset($files[$key]);
}
}
$total = count($files);
echo "*** Total filenames " . $total . " Will be deleted\n";
if (empty($total)) {
exit;
}
$totalSize = 0;
foreach ($files as $key => $value) {
$size = getDirSize($value[1]);
$totalSize += $size;
echo "{$value[0]} => $value[1] " . (humanFileSize($size)) . " \n";
}
echo "*** Confirm Delete Them (" . humanFileSize($totalSize) . ")? y/n: ";
ob_flush();
$confirm = trim(readline(""));
if (!empty($confirm) && strtolower($confirm) === 'y') {
foreach ($files as $key => $value) {
if (is_dir($value[1])) {
rrmdir($value[1]);
if (is_dir($value[1])) {
echo "$value[1] Directory Deleted \n";
} else {
echo "$value[1] Directory Could Not be Deleted \n";
}
} elseif (unlink($value[1])) {
echo "$value[1] Deleted \n";
} else {
echo "$value[1] Could Not be Deleted \n";
}
}
}