-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathduplicate_remover.ts
62 lines (49 loc) · 1.59 KB
/
duplicate_remover.ts
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
// import { } from 'https://deno.land/std/hash/mod.ts';
import { exists, WalkEntry, walkSync } from "https://deno.land/std/fs/mod.ts";
import FileHasher from "./file_hasher.ts";
import { asyncForEach } from "./async_for_each.ts";
/**
* Removes duplicate files within a directory
*/
class DuplicateRemover {
directory: string;
constructor(directory: string) {
this.directory = directory;
}
async removeDuplicates() {
console.log(`removing duplicates from ${this.directory} ...`);
const duplicateSet: Set<string> = new Set();
const paths = await this.paths();
await asyncForEach(paths, async (path: string) => {
const hash = await FileHasher.md5(path);
if (duplicateSet.has(hash)) {
console.log(`Found duplicate at ${path}, removing ...`);
if (await exists(path)) {
await Deno.remove(path);
} else {
console.log(`Unable to remove ${path}`);
}
} else {
duplicateSet.add(hash);
}
});
}
private async paths(): Promise<string[]> {
const output: string[] = [];
const entries = Array.from(walkSync(this.directory));
await asyncForEach(entries, async (entry: WalkEntry) => {
const isDir = await this.isDirectory(entry.path);
if (!isDir) {
output.push(entry.path);
}
});
return output;
}
private async isDirectory(path: string): Promise<boolean> {
const f = await Deno.open(path, { read: true, write: false });
const fileinfo = await Deno.fstat(f.rid);
f.close();
return fileinfo.isDirectory;
}
}
export default DuplicateRemover;