-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
executable file
·146 lines (104 loc) · 3.26 KB
/
index.mjs
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { readFileSync, writeFileSync } from "fs";
import * as csv from 'csv';
const FILE_ID_FROM_PREVIEW_PATH_REGEX = /^(?:.*\/)?(\d+)\/.+$/;
const NAME_FROM_PATH_REGEX = /^.*\/(.+\.jpg)$/;
/**
* To be called wit three arguments:
* 1. path to the file containing all available hashsums
* 2. path to the file containing the desired previews hashsums
* 3. path to the file contianing the CSV mapping: fileId -> path
*
* Result:
* A file `mapped-everything.awkcompatible` that contains the mapping previewFileName -> pathInNextcloud
*/
// parse args
const allPreviewHashesPath = process.argv[2],
desiredPreviewHashesPath = process.argv[3],
previewIdPathMappingPath = process.argv[4];
// read hashes
const sums = readHashes(allPreviewHashesPath),
querySums = readHashes(desiredPreviewHashesPath);
console.log(`Have read ${Object.keys(sums).length} available preview hashes`);
console.log(`Have read ${Object.keys(querySums).length} query hashes`);
// map (previewHash -> previewPath -> fileId -> filePath)
const fileIdNameMapping = mapFileIdToImageName(),
mappedEverything = await mapPreviewsToOriginals();
console.log(`Mapped ${mappedEverything.length} previews to their filepaths.`);
// write the result as (previewFileName --> filePath)
write(mappingToAwkCompatible(mappedEverything), 'mapped-everything.awkcompatible');
//
// Utility
//
async function mapPreviewsToOriginals() {
const mappedEverything = [];
const previewIdPathMapping = csv.parse(readFileSync(previewIdPathMappingPath), { bom: true, columns: true });
await previewIdPathMapping.forEach((data) => {
const fileId = data.fileid, path = data.path, name = fileIdNameMapping[fileId];
if (name) {
mappedEverything.push({
fileId,
path,
name
});
}
});
return mappedEverything;
}
function mapFileIdToImageName(desiredHashes, allPreviewHashes) {
const joined = join(desiredHashes, allPreviewHashes), fileIdNameMapping = {};
let matched = [], unmatched = [];
for (const key in joined) {
const previewId = joined[key];
if (previewId) {
matched.push(previewId);
fileIdNameMapping[getFileIdFromPreviewPath(previewId)] = getNameFromPath(key);
} else {
unmatched.push(key);
}
}
console.log(`Matched ${matched.length} hashes. ${unmatched.length} remain unmatched. Total ${matched.length + unmatched.length}`);
console.log(`Unmatched: `, unmatched);
return fileIdNameMapping;
}
function mappingToAwkCompatible(mappedEverything) {
let str = '';
mappedEverything.forEach(entry => {
str += `${entry.name}:${entry.path}\n`;
});
return str;
}
function getFileIdFromPreviewPath(path) {
const matches = FILE_ID_FROM_PREVIEW_PATH_REGEX.exec(path);
if (! matches) {
return null;
}
return matches[1];
}
function getNameFromPath(path) {
const matches = NAME_FROM_PATH_REGEX.exec(path);
if (! matches) {
return null;
}
return matches[1];
}
function join(query, data) {
const result = {};
for (const key in query) {
result[query[key]] = data[key];
}
return result;
}
function readHashes(path) {
const sums = {};
readFileSync(path)
.toString()
.split('\n')
.forEach(sumline => {
const split = sumline.split(' ');
sums[split[0]] = split[1];
});
return sums;
}
function write(text, fileName) {
writeFileSync(fileName, text);
}