-
Notifications
You must be signed in to change notification settings - Fork 1
/
adminchem.html
224 lines (182 loc) · 7.55 KB
/
adminchem.html
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<html>
<head>
<title>UniGrade</title>
<style>
img{
height: 200px;
width: 200px;
border: 2px solid black;
}
a{
text-decoration: none;
font-size: 20px;
color: white;
}
.atag{
background-color: black;
align-items: center;
text-align: center;
width: 200px;
margin: 6px;
}
p{
font-size: 20px;
}
</style>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<link rel="stylesheet" href="adminfileupload.css">
<link rel="icon" type="image/x-icon" href="logo.png">
</head>
<body>
<div class="text-center">
<img src="logo.png">
<h1>UniGrade</h1>
<label style="font-weight: bold; margin-top: 3%;">You can change the FileName Here after Selecting:</label><br>
<input type="text" class="text-center" placeholder="FileName" id="namebox">
<label id="extlab"></label>
<br>
<button id="selbtn" class="b1" style="margin-top: 2%; background-color: black; color: white;">Select File</button><br><br>
<button id="upbtn" class="b2" style="background-color: black; color: white;">Upload File</button><label id="upprogress"></label><br>
<button onclick="logout()" style="margin-top: 2%; background-color: black; color: white;">LogOut</button><br><br>
</div>
<div class="container mt-3">
<table class="table table-dark">
<thead>
<th style="font-size: 150%;">FileUploaded</th>
</thead>
<tbody id="tbody1"></tbody>
</table>
</div>
</body>
<script>
function logout(){
window.location.replace("index.html");
}
</script>
<script type="module">
// Import the functions you need from the SDKs you need
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.10.0/firebase-app.js";
import { getAnalytics } from "https://www.gstatic.com/firebasejs/9.10.0/firebase-analytics.js";
import { getStorage, ref as sRef, uploadBytesResumable, getDownloadURL } from "https://www.gstatic.com/firebasejs/9.10.0/firebase-storage.js";
import { getFirestore, doc, getDoc, getDocs, setDoc, collection, addDoc, onSnapshot } from "https://www.gstatic.com/firebasejs/9.10.0/firebase-firestore.js";
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries
// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
apiKey: "AIzaSyAXK1OqdT_w-87Y2DFJhhe0EWQdl-E3fgg",
authDomain: "student-connect-cdafe.firebaseapp.com",
projectId: "student-connect-cdafe",
storageBucket: "student-connect-cdafe.appspot.com",
messagingSenderId: "696405092513",
appId: "1:696405092513:web:8ef843e289626db7a968e0",
measurementId: "G-6KZW84VQQ3",
databaseURL: "https://student-connect-cdafe-default-rtdb.firebaseio.com/"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);
const clouddb = getFirestore();
var files = [];
var reader = new FileReader();
var namebox = document.getElementById('namebox');
var extlab = document.getElementById('extlab');
var myimg = document.getElementById('myimg');
var proglab = document.getElementById('upprogress');
var SelBtn = document.getElementById('selbtn');
var UpBtn = document.getElementById('upbtn');
var DownBtn = document.getElementById('downbtn');
var input = document.createElement('input');
input.type = 'file';
input.onchange = e => {
files = e.target.files;
var extension = GetFileExt(files[0]);
var name = GetFileName(files[0]);
namebox.value = name;
extlab.innerHTML = extension;
reader.readAsDataURL(files[0]);
}
reader.onload = function(){
myimg.src = reader.result;
}
SelBtn.onclick = function(){
input.click();
}
function GetFileExt(file){
var temp = file.name.split('.');
var ext = temp.slice((temp.length-1),(temp.length));
return '.'+ext[0];
}
function GetFileName(file){
var temp = file.name.split('.');
var fname = temp.slice(0,-1).join('.');
return fname;
}
async function UploadProcess(){
var ImgToUpload = files[0];
var ImgName = namebox.value + extlab.innerHTML;
const metadata = {
contentType: ImgToUpload.type
}
const storage = getStorage();
const storageRef = sRef(storage, "Chemistry/"+ImgName);
const UploadTask = uploadBytesResumable(storageRef, ImgToUpload, metadata);
UploadTask.on('state-changed', (snapshot)=>{
var progress = (snapshot.bytesTransferred / snapshot.totalBytes)*100;
proglab.innerHTML = "Upload "+progress + "%";
},
(error)=>{
alert("Image Not Uploaded");
},
()=>{
getDownloadURL(UploadTask.snapshot.ref).then((downloadURL)=>{
SaveURLtoFirestore(downloadURL);
});
}
);
}
async function SaveURLtoFirestore(url){
var name = namebox.value;
var ext = extlab.innerHTML;
var ref = doc(clouddb, "ChemFiles/"+name);
await setDoc(ref, {
ImageName: (name+ext),
ImageURL: url
})
}
var tbody = document.getElementById("tbody1");
function AddItemToTable(link){
let trow = document.createElement("tr");
let td1 = document.createElement("td");
td1.innerHTML = link;
trow.appendChild(td1);
tbody.appendChild(trow);
}
function AddAllItemsToTable(linkslist){
tbody.innerHTML = "";
linkslist.forEach(element =>{
AddItemToTable(element.ImageName)
});
}
async function GetAllDataOnce(){
const querySnapshot = await getDocs(collection(clouddb, "ChemFiles"));
var students = [];
querySnapshot.forEach(doc => {
students.push(doc.data());
});
AddAllItemsToTable(students);
}
async function GetAllDataRealtime(){
const dbRef = collection(clouddb, "ChemFiles");
onSnapshot(dbRef, (querySnapshot)=>{
var fileName = [];
querySnapshot.forEach(doc => {
fileName.push(doc.data());
});
AddAllItemsToTable(fileName);
})
}
UpBtn.onclick = UploadProcess;
window.onload = GetAllDataRealtime;
</script>
</html>