This repository was archived by the owner on Feb 27, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbox-stuff.js
More file actions
121 lines (103 loc) · 3.42 KB
/
Copy pathbox-stuff.js
File metadata and controls
121 lines (103 loc) · 3.42 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
var BoxSDK = require('box-node-sdk');
const fs = require("fs");
const { getBoxClient } = require("./box-credentials-load");
// let date = new Date();
// let dd = String(date.getDate());
// let mm = String(date.getMonth() + 1);
// let yy = date.getFullYear() - 2000;
// let today = mm + '.' + dd + '.' + yy;
let today = getToday();
function getToday() {
let date = new Date();
let dd = String(date.getDate());
let mm = String(date.getMonth() + 1);
let yy = date.getFullYear() - 2000;
return mm + '.' + dd + '.' + yy;
}
const parentFolderId = {
"3D Print" : "138623403868",
"Laser Cut" : "54496771414",
"Poster" : "137998902121"
}
let client;
setInterval(() => {
getBoxClient().then(c => { client = c; });
}, 1000 * 60 * 30);
async function getFolder(name, type) {
let firstletterfolder = await client.search.query(`"${name.substring(0, 1)}"`, {
type: 'folder',
limit: 1,
fields: 'name',
ancestor_folder_ids: parentFolderId[type]
});
let folder = await client.search.query(`"${name}"`, {
type: 'folder',
limit: 1,
fields: 'name',
ancestor_folder_ids: firstletterfolder.entries[0].id
})
let myFolderId;
if (folder.total_count == 0) {
folder = await client.folders.create(firstletterfolder.entries[0].id, `${name}`);
myFolderId = await folder.id;
} else {
myFolderId = folder.entries[0].id;
}
let todayFolder = await client.search.query(`"${today}"`, {
type: 'folder',
limit: 1,
fields: 'name',
ancestor_folder_ids: myFolderId
})
let myTodayFolderId;
if (todayFolder.total_count == 0) {
let i = 1;
while (true) {
try {
if (i == 1) {
todayFolder = await client.folders.create(myFolderId, today);
myTodayFolderId = await todayFolder.id;
} else {
console.log("trying " + i);
todayFolder = await client.folders.create(myFolderId, today +" #" + i);
myTodayFolderId = await todayFolder.id;
}
} catch (err) {
i++;
continue;
}
break;
}
} else {
myTodayFolderId = todayFolder.entries[0].id;
}
return myTodayFolderId;
}
async function uploadFile(folderId, filePaths) {
for (let i = 0; i < filePaths.length; i++) {
let stream = fs.createReadStream(filePaths[i]);
let fileSize = fs.statSync(filePaths[i]).size;
if (fileSize > 100000000) {
let file = await client.files.getChunkedUploader(folderId, fileSize, filePaths[i].substring(filePaths[i].lastIndexOf("/") + 1), stream);
file.start();
} else {
let file = await client.files.uploadFile(folderId, filePaths[i].substring(filePaths[i].lastIndexOf("/") + 1), stream);
}
}
}
async function getFolderLink(folderId) {
let url;
await client.folders.update(folderId, { shared_link: { access: "open" } }).then(folder => {
url = folder.shared_link.url;
});
return await url;
}
async function boxUpload(name, type, filePaths) {
today = getToday();
client = await getBoxClient();
let folderId = await getFolder(name, type);
let link = await getFolderLink(folderId);
await uploadFile(folderId, filePaths);
return link;
}
module.exports = { boxUpload }