-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfiler.php
More file actions
88 lines (74 loc) · 2.09 KB
/
filer.php
File metadata and controls
88 lines (74 loc) · 2.09 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
<?php //filer.php
//Dedupe\Filer namespace
namespace Dedupe\Filer {
function scan(string $cd) : array {
$files = scandir($cd);
$filehashes = array();
$count = 0;
$dupes_raw = 0;
$dupes_fix = 0;
$dupes_arr = array();
$out = array("err" => 0, "msg" => "");
$times = array("start" => time());
$dupes_dir = ($cd . "dupes-" . $times["start"] . "/");
if(!mkdir($dupes_dir)){
$out["err"] = 1;
$out["msg"] = "Failed to generate dupes directory\n";
return $out;
}
//Hash all the files and sort them into an array
foreach($files as $file){
$count++;
if($count % 50 == 0){print("Handled {$count} files...\n");}
$hash = hash_file("sha512", $file);
if(filetype($file) == "file"){
if(!isset($filehashes[$hash])){
$filehashes[$hash] = array($file);
}else{
array_push($filehashes[$hash], $file);
}
}
}
$times["after_hash"] = time();
//Check each array item for dupes
foreach($filehashes as $key => $isdupe){
$ct = count($isdupe);
if($ct > 1){
$dupes_raw += $ct;
$dupes_fix += ($ct - 1);
$dupes_arr[$key] = $isdupe;
}
}
//Move the duplicates
$out = move_dupes($cd, $dupes_dir, $dupes_arr);
if($out["err"] != 0){
return $out;
}
$times["after_finish"] = time();
$out["msg"] = sprintf("There are %d duplicates (%d copies) of files in this folder, which contains %d non-directory files.\n", $dupes_raw, $dupes_fix, count($filehashes));
return $out;
}
function move_dupes(string $in_dir, string $out_dir, array $arr) : array {
$out = array("err" => 0, "msg" => "");
$count = 1;
foreach($arr as $key => $val){
$count = 1;
foreach($val as $v){
//Skip the index file
if($count == 1){
$count++;
continue;
}
$ext = pathinfo("{$in_dir}{$v}", PATHINFO_EXTENSION);
if(!rename("{$in_dir}{$v}", "{$out_dir}{$key}-{$count}.{$ext}")){
$out["err"] = 1;
$out["msg"] = ("Failed to move one or more files");
return $out;
}
$count++;
}
}
return $out;
}
}
?>