-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathscript.js
150 lines (138 loc) · 4.79 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
const firebaseConfig = {
// Add your firebase config here
apiKey: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
authDomain: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
projectId: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
storageBucket: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
messagingSenderId: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
appId: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
measurementId: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
// Reference messages collection
var messagesRef = firebase.database().ref("image");
// Listen for form submit
function uploadImage() {
if (document.getElementById("file").value != "") {
var uploadtext = document.getElementById("upload").innerHTML;
document.getElementById("upload").innerHTML = "Uploading...";
var file = document.getElementById("file").files[0];
var storageRef = firebase.storage().ref("images/" + file.name);
var uploadTask = storageRef.put(file);
uploadTask.on(
"state_changed",
function (snapshot) {
var progress = (snapshot.bytesTransferred / snapshot.totalBytes * 100).toFixed(2);
console.log("Upload is " + progress + "% done");
document.getElementById("upload").innerHTML = "Uploading"+" "+progress+"%...";
},
function (error) {
console.log(error.message);
document.getElementById("upload").innerHTML = "Upload Failed";
},
function () {
uploadTask.snapshot.ref.getDownloadURL().then(function (downloadURL) {
console.log("File available at", downloadURL);
saveMessage(downloadURL);
});
}
);
} else {
var uploadtext = document.getElementById("upload").innerHTML;
document.getElementById("upload").innerHTML = "Please select a file";
// After 2 sec make it empty
setTimeout(function () {
document.getElementById("upload").innerHTML = uploadtext;
}, 2000);
}
}
// Save message to firebase
function saveMessage(downloadURL) {
var newMessageRef = messagesRef.push();
var unique = createUniquenumber();
// Hidding recive file div
var x = document.getElementById("downloadiv");
x.style.display = "none";
var showUnique = document.getElementById("ShowUniqueID");
var shU = document.getElementById("showunique");
shU.value = unique;
showUnique.style.display = "block";
// showUnique.value = unique;
newMessageRef.set({
url: downloadURL,
number: unique,
});
document.getElementById("upload").innerHTML = "Upload Successful";
//Make file input empty
document.getElementById("file").value = "";
}
function createUniquenumber() {
// Create a unique 5 digit number for each image which is not in the database field number yet
var number = Math.floor(10000 + Math.random() * 90000);
var ref = firebase.database().ref("image");
ref.on("value", function (snapshot) {
snapshot.forEach(function (childSnapshot) {
var childData = childSnapshot.val();
if (childData.number == number) {
createUniquenumber();
}
});
});
return number;
}
function showimage() {
var uniqueId = document.getElementById("unique").value;
if (uniqueId == "") {
alert("Unique Id is empty\n Please enter a Unique Id");
return;
}
var ref = firebase.database().ref("image");
var flag = 0;
ref.on("value", function (snapshot) {
snapshot.forEach(function (childSnapshot) {
var childData = childSnapshot.val();
if (childData.number == uniqueId) {
flag = 1;
window.open(childData.url, "_blank");
// AFter this delete the image from the database
ref.child(childSnapshot.key).remove();
// Remove file from storage
//Run this with 5sec delay
setTimeout(function () {
var storageRef = firebase.storage().refFromURL(childData.url);
storageRef
.delete()
.then(function () {
ref.child(childSnapshot.key).remove();
// File deleted successfully
})
.catch(function (error) {});
}, 15000);
}
});
});
// After some time if flag is still 0 then show alert
// setTimeout(function(){
// if(flag == 0){
// alert("File not found Check the Unique ID");
// }
// }, 5000);
}
function flesize() {
var file = document.getElementById("file").files[0];
// Dont allow file size greater than 100MB
if (file.size > 100000000) {
alert(
"File size is greater than 100MB\n Please select a file less than 100MB"
);
document.getElementById("file").value = "";
}
}
// Click on download button when enter is pressed
document.getElementById("unique").addEventListener("keyup", function (event) {
event.preventDefault();
if (event.keyCode === 13) {
document.getElementById("show").click();
}
});