-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
255 lines (195 loc) · 7.42 KB
/
script.js
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import { auth, signInAnonymously, signOut, storage, ref, uploadBytes, get,listAll, getDownloadURL, getMetadata,database, dbRef, push, onValue, deleteObject, remove, child } from "./app.js";
document.getElementById("anonymousSignInBtn").addEventListener("click", login);
function login(e) {
e.preventDefault();
const user = auth.currentUser;
signInAnonymously(auth)
.then(() => {
alert("Logged in anonymously");
if (user) {
console.log(user.uid);
window.location.href = "index.html";
}
// Update the UI to reflect a signed in user
})
.catch((error) => {
console.error("Error during anonymous sign-in:", error);
alert("An error occurred during anonymous sign-in. Check the console for details.");
});
}
document.addEventListener('DOMContentLoaded', () => {
auth.onAuthStateChanged(user => {
if (user) {
// User is signed in
console.log('User is signed in');
console.log(user.uid);
const logoutDiv = document.getElementById('logout');
document.getElementById('loginStatus').style.display = 'none';
document.getElementById('imageUpload').style.display = 'block';
document.getElementById('imageDisplay').style.display = 'block';
document.getElementById('userID').innerHTML = "User ID: " + user.uid;
if (logoutDiv) {
logoutDiv.style.display = 'block';
}
} else {
// No user is signed in, stay on the page
const loginStatusDiv = document.getElementById('loginStatus');
if (loginStatusDiv) {
loginStatusDiv.style.display = 'block';
}
}
});
document.getElementById('logoutBtn').addEventListener('click', () => {
signOut(auth)
.then(() => {
alert('Logged out');
window.location.href = 'index.html';
})
.catch((error) => {
console.error('Error during logout:', error);
alert('An error occurred during logout. Check the console for details.');
});
});
});
//Firebase storage image upload funtion :) 🥹
document.getElementById("uploadBtn").addEventListener("click", async function () {
var imageInput = document.getElementById("imageInput");
// var uploadedImagesList = document.getElementById("uploadedImagesList");
const user = await getCurrentUser(); // Asynchronously get the user ID
for (var i = 0; i < imageInput.files.length; i++) {
var selectedImage = imageInput.files[i];
if (selectedImage) {
// Generate a unique filename
var timestamp = Date.now();
var fileName = user + "_" + timestamp + "_" + selectedImage.name;
// Upload the image to Firebase Storage
var storageRef = ref(storage, "meme/" + fileName);
var uploadTask = uploadBytes(storageRef, selectedImage,timestamp);
uploadTask.then((snapshot) => {
alert("Image uploaded successfully!");
getDownloadURL(snapshot.ref).then((imageURL) => {
var imagesRef = dbRef(database, "images");
push(imagesRef, {
imageURL: imageURL,
userID: user,
timestamp: timestamp,
fileName: fileName,
});
});
}).catch((error) => {
console.error("Error during image upload:", error);
});
}
}
document.getElementById("imageDisplay").style.display = "block"; // Show the image display section
});
onValue(dbRef(database, "images"), (snapshot) => {
var imagesList = snapshot.val();
uploadedImagesList.innerHTML = ""; // Clear the list before adding new items
for (const key in imagesList) {
var imageInfo = imagesList[key];
var listItem = document.createElement("li");
var img = document.createElement("img");
img.src = imageInfo.imageURL;
img.alt = "Uploaded Image";
var userID = document.createElement("p");
userID.textContent = "User ID: " + imageInfo.userID;
listItem.appendChild(img);
listItem.appendChild(userID);
uploadedImagesList.appendChild(listItem);
}
});
async function getCurrentUser() {
return new Promise((resolve, reject) => {
auth.onAuthStateChanged(user => {
if (user) {
resolve(user.uid);
} else {
reject("User not authenticated");
}
});
});
}
// Function to delete images older than 2 hours
async function deleteOldImagesFromStorage() {
const imagesRef = ref(storage, "meme/");
const dbTimestampRef = dbRef(database, "images/");
try {
const snapshot = await get(dbTimestampRef);
const currentTime = Date.now();
if (snapshot.exists()) {
const data = snapshot.val();
// Loop through each child node
for (const imageKey in data) {
if (data.hasOwnProperty(imageKey)) {
const imageInfo = data[imageKey];
console.log("Image Key:", imageKey);
console.log("File Name:", imageInfo.fileName);
console.log("Timestamp:", imageInfo.timestamp);
// Calculate the time difference in milliseconds
const timeDifference = currentTime - imageInfo.timestamp;
if (timeDifference >= 20000) {
const imageRefToDelete = ref(imagesRef, imageInfo.fileName);
// Remove the image node if the condition is met
await deleteObject(imageRefToDelete);
console.log("Image deleted successfully from the Storage.", imageInfo.fileName);
} else {
console.log("Image is within the time limit.");
}
}
}
} else {
console.log("No data found in the 'images' node.");
}
}catch(error){
console.log(error);
}
}
const ct=Date.now();
console.log(ct)
// // Call the async function to delete images
setInterval(() => {
deleteOldImagesFromStorage();refreshPageAfterDelay(); //rel 2hrs
}, 20000);
// Function to delete images older than 2hrs and 30 sec hours
async function deleteImageFromDB() {
const imagesRef = dbRef(database, "images/");
try {
const snapshot = await get(imagesRef);
if (snapshot.exists()) {
const data = snapshot.val();
const currentTime = Date.now();
// Loop through each child node
for (const imageKey in data) {
if (data.hasOwnProperty(imageKey)) {
const imageInfo = data[imageKey];
console.log("Image Key:", imageKey);
console.log("File Name:", imageInfo.fileName);
// Calculate the time difference in milliseconds
const timeDifference = currentTime - imageInfo.timestamp;
if (timeDifference >= 20000 ) {
const imageRefToDelete = child(imagesRef, imageKey);
// Remove the image node if the condition is met
await remove(imageRefToDelete);
console.log("Image deleted successfully from the Realtime Database.", imageInfo.fileName);
} else {
console.log("Image is within the time limit.");
}
}
}
} else {
console.log("No data found in the 'images' node.");
}
} catch (error) {
console.error("Error retrieving data:", error);
}
}
setInterval(() => {
deleteImageFromDB(); refreshPageAfterDelay(); //rel 2hrs
}, 20000 );
function refreshPageAfterDelay() {
location.reload(false);
}
setInterval(() => {
refreshPageAfterDelay(); // Reload the page after 5 hours
},30000);