/
calc_hash.php
114 lines (87 loc) · 2.04 KB
/
calc_hash.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
require(__DIR__ . '/functions_inc.php');
if (!isset($argv) || !is_array($argv)) {
die("can't read command line arguments.\n");
}
function help()
{
print <<<EOT
move_all.php [-s SOURCE_DIR] [-o OUTPUT_DIR]
EOT;
}
$argc = count($argv);
$source_dir = CUR_DIR . 'Photos';
array_shift($argv);
while (count($argv)) {
$a = $argv[0];
if (!isset($argv[1])) {
print("invalid option {$a} without argument.\n\n");
help();
die();
}
$v = $argv[1];
array_shift($argv);
array_shift($argv);
switch ($a) {
case '-s':
$source_dir = $v;
break;
case '-o':
$output_dir = $v;
break;
default:
print("invalid option {$a}.\n\n");
help();
die();
}
}
if (!is_dir($source_dir)) {
$source_dir = realpath(CUR_DIR . $source_dir);
}
if (!isset($output_dir)) {
$output_dir = $source_dir;
}
if (!is_dir($output_dir)) {
$output_dir = realpath(CUR_DIR . $output_dir);
}
if (empty($source_dir) || !is_dir($source_dir)) {
print("invalid SOURCE_DIR {$source_dir}.\n\n");
help();
die();
}
if (empty($output_dir) || !is_dir($output_dir)) {
print("invalid OUTPUT_DIR {$output_dir}.\n\n");
help();
die();
}
$source_dir = rtrim(realpath($source_dir), '/\\') . DS;
$output_dir = rtrim(realpath($output_dir), '/\\') . DS;
print <<<EOT
SOURCE_DIR: {$source_dir}
OUTPUT_DIR: {$output_dir}
EOT;
$paths = array();
search_in_dir($source_dir, $paths);
date_default_timezone_set('Asia/Shanghai');
$files_hash = array();
$count = count($paths);
printf("Files count: %d\n", $count);
flush();
$current = 0;
$percent = -1;
$last_percent = -1;
foreach ($paths as $path) {
$percent = round($current / $count * 100);
if ($percent > $last_percent) {
printf("%d%%\n", $percent);
flush();
$last_percent = $percent;
}
$files_hash[$path] = md5_file($path);
$current++;
}
print("100%\n");
flush();
$hash_path = $output_dir . 'hash.json';
file_put_contents($hash_path, json_encode($files_hash));
print("DONE\n\n");