-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.ts
56 lines (46 loc) · 1.71 KB
/
upload.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
import fs from "fs";
import PocketBase from "pocketbase";
const pb = new PocketBase("https://agios-calendar.pockethost.io");
async function uploadImagesToPocketBase() {
const allLocalImages = fs.readdirSync("images");
for (const image of allLocalImages) {
const formData = new FormData();
const file = fs.readFileSync(`images/${image}`);
// Extract caption from the filename at the last period
const lastPeriodIndex = image.lastIndexOf(".");
const caption = image.slice(0, lastPeriodIndex);
let existsInPB = await pb
.collection("icons")
.getFirstListItem(`caption="${caption}"`, {
expand: "iconographer"
})
.catch((err) => {
return;
});
if (existsInPB) {
console.log(`Image ${caption} already exists in PocketBase. Skipping...`);
continue;
}
// Append the image file to the FormData
formData.append("image", new Blob([file]), image);
// Append other fields if needed
formData.append("caption", caption);
try {
// Upload image using PocketBase
const createdRecord = await pb.collection("icons").create(formData);
console.log(`Image ${caption} uploaded successfully!`);
console.log("Created Record:", createdRecord);
} catch (error) {
console.error(`Error uploading image ${caption}: ${error}`);
}
}
}
uploadImagesToPocketBase();
// let collection = "copticDate"
// let recs = await pb.collection(collection).getFullList();
// for (let i = 0; i < recs.length; i++) {
// let rec = recs[i];
// console.log(`${i+1}/${recs.length}: Deleting ${rec.month+' '+rec.day}...`);
// await pb.collection(collection).delete(rec.id);
// await new Promise((resolve) => setTimeout(resolve, 1000));
// }