-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.php
More file actions
103 lines (98 loc) · 3.19 KB
/
Copy pathmain.php
File metadata and controls
103 lines (98 loc) · 3.19 KB
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
<?php
ini_set('memory_limit', -1);
$path = "/data";
$exists = [];
$list = [];
scanner($path, $list, $exists);
$surfix = 'json';
switch (getenv('OUTPUT_FORMAT')) {
case 'sql':
$surfix = 'sql';
$tableName = getenv('OUTPUT_TABLE');
if (empty($tableName)) {
$tableName = 'synology_file_duplicates';
}
$output = "CREATE TABLE IF NOT EXISTS `{$tableName}` (
`id` int NOT NULL AUTO_INCREMENT,
`path` text CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
`size` bigint NOT NULL,
`md5` char(32) NOT NULL,
PRIMARY KEY (`id`),
KEY `duplicateID` (`size`,`md5`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;\n";
if (!empty($list)) {
foreach ($list as $i => $r) {
if ($i % 1000 == 0) {
if ($i != 0) {
$output .= ";\n";
}
$output .= "INSERT INTO `{$tableName}`(`path`, `size`, `md5`) VALUES\n";
} else {
$output .= ",\n";
}
$sqlPath = addslashes($r['path']);
$output .= "('{$sqlPath}',{$r['size']},'{$r['md5']}')";
}
$output .= ';';
}
break;
case 'csv':
$surfix = 'csv';
$output = "path,size,md5\n" . implode("\n", array_map(function($r) {
$csvPath = '"' . str_replace('"', '""', $r['path']) . '"';
return "{$csvPath},{$r['size']},{$r['md5']}";
}, $list));
break;
case 'json':
default:
$surfix = 'json';
$output = json_encode($list);
}
$outputFile = getenv('OUTPUT_FILE');
if (empty($outputFile)) {
$outputFile = 'duplicates.' . date('YmdHis') . '.' . $surfix;
}
$outputPath = "/output/{$outputFile}";
file_put_contents($outputPath, $output);
echo "The result has been written to the path:{$outputPath}";
function scanner($path, &$list, &$exists) {
if (is_dir($path)) {
$dir = dir($path);
while (false !== ($file = $dir->read())) {
if (in_array($file, ['.', '..'])) {
continue;
}
scanner($path . '/' . $file, $list, $exists);
}
} else {
$size = filesize($path);
if (!isset($exists[$size])) {
$exists[$size] = $path;
} else {
if (is_string($exists[$size])) {
$lastPath = $exists[$size];
$lastMd5 = md5_file($lastPath);
$exists[$size] = [$lastMd5 => $lastPath];
}
$md5 = md5_file($path);
if (!isset($exists[$size][$md5])) {
$exists[$size][$md5] = $path;
} else {
$lastPath = $exists[$size][$md5];
if (true !== $lastPath) {
$exists[$size][$md5] = true;
$list[] = [
'path' => substr($lastPath, 5),
'size' => $size,
'md5' => $md5,
];
}
$list[] = [
'path' => substr($path, 5),
'size' => $size,
'md5' => $md5,
];
}
}
}
}