-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnode.js
More file actions
146 lines (121 loc) · 4.73 KB
/
node.js
File metadata and controls
146 lines (121 loc) · 4.73 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
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 { mkdirSync, readFileSync, readdirSync, writeFileSync } from 'fs';
import { initializeApp } from "firebase/app";
import { uploadBytes, getStorage, ref} from "firebase/storage";
import { updateDoc, setDoc, getFirestore, doc, } from "firebase/firestore";
const firebaseConfig = {
apiKey: "AIzaSyBOxoXPHcPNS0sLU2knK2_tNHOQm-uC3Pc",
authDomain: "mindsprout.firebaseapp.com",
projectId: "mindsprout",
storageBucket: "mindsprout.appspot.com",
messagingSenderId: "1047811960255",
appId: "1:1047811960255:web:5f554dc75e9e800f08450a",
measurementId: "G-LSZ17SMXG9"
};
// const firebaseConfig = {
// apiKey: "AIzaSyBFUbDPen4sc72iJ3VTA2LpRkxzipqTPoE",
// authDomain: "practice-helper.firebaseapp.com",
// projectId: "practice-helper",
// storageBucket: "practice-helper.appspot.com",
// messagingSenderId: "337534902662",
// appId: "1:337534902662:web:02b813750adaeda6e88c07",
// };
const app = initializeApp(firebaseConfig);
const storage = getStorage(app);
const db = getFirestore(app);
const physics = {
"subjectName": "Physics",
"1st" : ["2nd", "4th", "5th", "6th", "7th", "8th", "10th"],
"2nd" : ["1st", "2nd", "3rd", "7th", "8th", "9th", "10th"],
}
const chemistry = {
"subjectName": "Chemistry",
"1st" : ["2nd", "3rd", "4th", "5th"],
"2nd" : ["1st", "2nd", "3rd", "4th"],
}
const higherMath = {
"subjectName": "Higher math",
"1st" : ["1st", "3rd", "4th", "7th", "9th", "10th"],
"2nd" : ["3rd", "4th", "6th", "7th", "8th", "9th"],
}
export const subjectRef = [physics, chemistry, higherMath];
export const questionTypes = 4;
const path = "./images/Physics/1st/4th/cq";
function copyFilesFromStagingToPath (path){
const questionPath = path + "/question";
const solutionPath = path + "/solution";
const files = readdirSync("./staging");
files.forEach(fileName => {
const filePath = `./staging/${fileName}`
const fileIndex = fileName.match(/\d+/)[0];
const file = readFileSync(filePath);
if(fileName.includes("(1)")){
writeFileSync(`${questionPath}/${fileIndex}.jpg`, file)
}
else{
const bracketIndex = +fileName.match(/\d+\)/)[0].replace(")", "") - 1
const dir = `${solutionPath}/${fileIndex}`;
mkdirSync(dir, {recursive : true})
writeFileSync(`${dir}/${bracketIndex}.jpg`, file)
}
})
}
function uploadToFirebase(link){
const array = readdirSync(link);
array.forEach(item => {
if(item.includes(".")) {
const path = link + "/" + item;
const pathForRef = path.replace("./images/", "questionsAndSolutions/");
const imageRef = ref(storage, pathForRef);
const image = new Uint8Array(readFileSync(path));
uploadBytes(imageRef, image).then(() => {
console.log("uploaded!");
}).catch(error => {
console.log(error);
});
return
}
uploadToFirebase(link + `/${item}`)
})
}
const link = "./images/Physics"
uploadToFirebase(link)
function updateQuestionIndex (link){
const array = readdirSync(link);
if(link.endsWith("question")) {
const [,, subject, paper, chapter] = link.match(/[^\/]+/g)
const ref = doc(db, "questionIndex", "Physics");
updateDoc(ref, {
[`${paper}.${chapter}`] : array.length,
});
return
};
array.forEach(item => {
if(item.includes(".")) {
return
};
updateQuestionIndex(link + `/${item}`)
})
}
function createLocalDirectories(boiler = "./images") {
subjectRef.forEach(subjectObj => {
const { subjectName } = subjectObj;
// mkdirSync(`${boiler}/${subjectName}`, {recursive : true})
for (const paperName in subjectObj) {
const chapterArray = subjectObj[paperName];
if(chapterArray == subjectName) continue;
// mkdirSync(`${boiler}/${subjectName}/${paperName}`, {recursive : true})
chapterArray.forEach(chapterName => {
// mkdirSync(`${boiler}/${subjectName}/${paperName}/${chapterName}`, {recursive : true})
const questionTypeArray = ["cq"];
questionTypeArray.forEach( questionType => {
// mkdirSync(`${boiler}/${subjectName}/${paperName}/${chapterName}/${questionType}`, {recursive : true})
const photoTypes = ["question", "solution"]
photoTypes.forEach(photoType => {
mkdirSync(`${boiler}/${subjectName}/${paperName}/${chapterName}/${questionType}/${photoType}`, { recursive: true })
})
})
})
}
})
}
// createLocalDirectories()