-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
180 lines (136 loc) · 4.31 KB
/
Copy pathscript.js
File metadata and controls
180 lines (136 loc) · 4.31 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
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
const drop_zone=document.querySelector(".drop-zone")
const browse=document.querySelector("#browseBtn")
const filein=document.querySelector(".file-in")
const bgprogress=document.querySelector(".bg-progress")
const perdiv=document.getElementById("per")
const progressCon=document.querySelector(".progress-container")
const fileURL=document.getElementById("fileURL")
const sharingcon=document.querySelector(".sharing-con")
const emailcon=document.querySelector(".email-part")
const copybtn=document.getElementById("copybtn")
const emailform=document.querySelector("#emailform")
const nextshare=document.getElementById("nshare")
const toast=document.querySelector(".toast")
const host = "https://linkshare-api.onrender.com/"
const uploadURL =`${host}api/files`
const emailURL =`${host}api/files/send`
drop_zone.addEventListener("dragover",(e)=>{
e.preventDefault()
if(!drop_zone.classList.contains("dragged"))
drop_zone.classList.add("dragged")
})
drop_zone.addEventListener("dragleave",(r)=>{
drop_zone.classList.remove("dragged")
})
drop_zone.addEventListener("drop",(e)=>{
e.preventDefault()
drop_zone.classList.remove("dragged")
const filesget=e.dataTransfer.files
if(filesget.length)
{
filein.files= filesget
uploadfile()
}
})
filein.addEventListener("change",()=>{
uploadfile()
})
browse.addEventListener("click",(e)=>{
filein.click()
})
copybtn.addEventListener("click",(e)=>{
fileURL.select()
document.execCommand("copy")
show_toast("Link Copied")
})
function uploadfile(){
progressCon.style.display="block"
const files = filein.files;
const formData = new FormData();
formData.append("myfile", files[0]);
// upload file
show_toast(`Since backend is slow please wait.`)
const xhr = new XMLHttpRequest();
xhr.onreadystatechange=()=>{
if(xhr.readyState===XMLHttpRequest.DONE)
console.log(xhr.response)
showlink(JSON.parse(xhr.response))
}
xhr.upload.onprogress = updateProgress;
xhr.upload.onerror=()=>{
filein.value=""
show_toast(`Error in uploading ${xhr.statusText}`)
}
xhr.open("POST",uploadURL)
xhr.send(formData)
}
const updateProgress=(e)=>{
const percent = Math.round((e.loaded/e.total)*100)
bgprogress.style.width=`${percent}%`
perdiv.innerText = percent;
}
const showlink=({file:url})=>{
progressCon.style.display="none"
fileURL.value=url
sharingcon.style.display="block"
emailcon.style.display="block"
nextshare.style.display="block"
}
emailform.addEventListener("submit",(e)=>{
e.preventDefault()
const url=fileURL.value
const formdata={
uuid:url.split("/").splice(-1,1)[0],
emailTo: emailform.elements["to-email"].value,
emailFrom: emailform.elements["from-email"].value
}
fetch(emailURL,{
method:"POST",
headers:{
"Content-Type":"application/json"
},
body:JSON.stringify(formdata)
}).then((res)=>res.json()).then(({success})=> {
if(success)
{
emailcon.style.display="none"
show_toast("Email Sent")
}
})
})
nextshare.addEventListener("click",()=>{
emailcon.style.display="none"
sharingcon.style.display="none"
nextshare.style.display="none"
filein.value=""
})
let toasttime;
const show_toast=(msg)=>{
toast.innerText=msg
toast.style.transform="translateY(0)"
clearTimeout(toasttime)
toasttime= setTimeout(()=>{
toast.style.transform="translateY(-60px)"
},5000)
}
// -------------------------------------------------
function validate(){
const form1=document.getElementById("form1")
const form2=document.getElementById("form2")
const sender=document.getElementById("sender")
const reciever=document.getElementById("reciever")
const pattern=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
if(sender.value.match(pattern)){
form1.classList.add('valid')
}
else{
form1.classList.remove('valid')
}
if(reciever.value.match(pattern))
{
form2.classList.add('valid')
}
else{
form2.classList.remove('valid')
}
}