Skip to content

Commit 88a1674

Browse files
committed
Bunch of updates
1 parent 88839ce commit 88a1674

File tree

8 files changed

+341
-1399
lines changed

8 files changed

+341
-1399
lines changed

bin/intersects-checksums.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
'use strict';
2+
3+
const {program} = require('commander');
14
const {promisify} = require('util');
25
const pcloudSdk = require('pcloud-sdk-js');
36
const pMap = require('p-map');
@@ -43,9 +46,9 @@ async function getChecksum(file) {
4346
return [file.name, checksum];
4447
}
4548

46-
async function run() {
47-
const folderId = Number.parseInt(process.env.FOLDER_ID, 10);
48-
const allFiles = await pClient.listfolder(folderId, {recursive: true});
49+
async function run(path) {
50+
const response = await pClient.api('listfolder', {params: {path, recursive: 1}});
51+
const allFiles = response.metadata;
4952
const goodFolders = allFiles.contents.filter(file => file.isfolder);
5053

5154
const hashesPerName = await pMap(goodFolders, async folder => {
@@ -67,8 +70,14 @@ async function run() {
6770
console.log(onlyNames);
6871
}
6972

70-
run().catch(error => {
71-
console.error(error);
72-
}).finally(() => {
73-
rClient.quit();
74-
});
73+
program
74+
.arguments('<path>')
75+
.action(path => {
76+
run(path).catch(error => {
77+
console.error(error);
78+
}).finally(() => {
79+
rClient.quit();
80+
});
81+
});
82+
83+
program.parse(process.argv);

bin/intersects-hashes.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict';
2+
13
const pcloudSdk = require('pcloud-sdk-js');
24
const pMap = require('p-map');
35

bin/merge-folder.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
'use strict';
2+
3+
const pcloudSdk = require('pcloud-sdk-js');
4+
const pMap = require('p-map');
5+
const delay = require('delay');
6+
const {join} = require('path');
7+
8+
const pClient = pcloudSdk.createClient(process.env.ACCESS_TOKEN);
9+
10+
const {program} = require('commander');
11+
12+
async function run(folderPaths) {
13+
const folders = await pMap(folderPaths, async path => {
14+
const {metadata} = await pClient.api('listfolder', {params: {path}});
15+
return metadata;
16+
});
17+
18+
const fileNamesToFiles = new Map();
19+
for (const folder of folders) {
20+
for (const file of folder.contents) {
21+
const fileName = file.name;
22+
const entry = fileNamesToFiles.get(fileName) || [];
23+
entry.push(file);
24+
fileNamesToFiles.set(fileName, entry);
25+
}
26+
}
27+
28+
// Order by oldest to newest
29+
for (const files of fileNamesToFiles.values()) {
30+
files.sort((a, b) => a.created - b.created);
31+
}
32+
33+
const [destinationFolder, ...otherFolders] = folders;
34+
35+
await pMap(fileNamesToFiles.values(), async files => {
36+
const file = files[0];
37+
if (file.parentfolderid === destinationFolder.folderid) {
38+
return;
39+
}
40+
41+
console.log(`Moving "${file.path}" to "${destinationFolder.path}"`);
42+
await pClient.movefile(Number.parseInt(file.fileid), destinationFolder.folderid);
43+
}, {concurrency: 1});
44+
45+
await pMap(otherFolders, async folder => {
46+
console.log(`Deleting ${folder.path}`);
47+
await pClient.deletefolder(folder.folderid);
48+
});
49+
}
50+
51+
// First folder is destination
52+
53+
program
54+
.option('-p, --prefix <prefix>')
55+
.arguments('<folders...>')
56+
.action((folders, {prefix}) => {
57+
if (prefix) {
58+
folders = folders.map(f => join(prefix, f));
59+
}
60+
61+
run(folders).catch(error => console.error(error));
62+
});
63+
64+
program.parse(process.argv);

bin/rm-dups.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict';
2+
13
// Finds duplicates within folders, recursively
24

35
const pcloudSdk = require('pcloud-sdk-js');

bin/rm-empty.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1+
'use strict';
2+
13
// Remove empty folders
24

35
const pcloudSdk = require('pcloud-sdk-js');
46
const pMap = require('p-map');
57
const pFilter = require('p-filter');
68
const delay = require('delay');
9+
const {getFoldersRecursive} = require('../lib/iter');
10+
11+
const {program} = require('commander');
712

813
const client = pcloudSdk.createClient(process.env.ACCESS_TOKEN);
914

10-
async function run() {
11-
const folderId = Number.parseInt(process.env.FOLDER_ID, 10);
12-
const allFiles = await client.listfolder(folderId, {recursive: true});
13-
const foldersToRemove = await pFilter(allFiles.contents, f => {
15+
async function run(path) {
16+
const response = await client.api('listfolder', {params: {path, recursive: 1}});
17+
const root = getFoldersRecursive(response.metadata);
18+
const foldersToRemove = await pFilter(root, f => {
1419
return f.isfolder && f.contents.length === 0;
1520
});
1621
console.log(foldersToRemove);
@@ -21,4 +26,12 @@ async function run() {
2126
}, {concurrency: 10});
2227
}
2328

24-
run().catch(error => console.error('Error', error));
29+
program
30+
.arguments('<path>')
31+
.action(path => {
32+
run(path).catch(error => {
33+
console.error(error);
34+
});
35+
});
36+
37+
program.parse(process.argv);

lib/redis-cacher.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class RedisCacher {
4646
await pMap(files, async file => {
4747
const checksum = await this._cacheChecksum(file);
4848
console.log('✓', {file: file.name, checksum});
49-
}, {concurrency: 10});
49+
}, {concurrency: 100});
5050
}
5151

5252
async quit() {

0 commit comments

Comments
 (0)