save
This commit is contained in:
@@ -25,21 +25,9 @@ type ValidateRequest struct {
|
|||||||
Token string
|
Token string
|
||||||
}
|
}
|
||||||
|
|
||||||
type headers map[string]string
|
|
||||||
|
|
||||||
var zipHeaders headers = headers{
|
|
||||||
"Content-Disposition": "attachment; filename=lash.zip",
|
|
||||||
"Content-Type": "application/octet-stream",
|
|
||||||
}
|
|
||||||
|
|
||||||
var sent int = 0
|
var sent int = 0
|
||||||
|
|
||||||
func (h FileHandler) APIHandler(w http.ResponseWriter, r *http.Request) {
|
func (h FileHandler) APIHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if h.hasHitMax() {
|
|
||||||
w.WriteHeader(http.StatusTooManyRequests)
|
|
||||||
os.Exit(0)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
decoder := json.NewDecoder(r.Body)
|
decoder := json.NewDecoder(r.Body)
|
||||||
|
|
||||||
var t ValidateRequest
|
var t ValidateRequest
|
||||||
@@ -54,25 +42,11 @@ func (h FileHandler) APIHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
http.Redirect(w, r, fmt.Sprintf("/%s", lash.ShareLinkToken), http.StatusTemporaryRedirect)
|
||||||
if len(h.FileData) == 1 {
|
|
||||||
headers := headers{
|
|
||||||
"Content-Disposition": fmt.Sprintf("attachment; filename=%s", h.FileData[0].FileName),
|
|
||||||
"Content-Type": "application/octet-stream",
|
|
||||||
"Content-Length": strconv.Itoa(len(h.FileData[0].FileName)),
|
|
||||||
}
|
|
||||||
headers.set(w)
|
|
||||||
|
|
||||||
w.Write(h.FileData[0].Contents)
|
|
||||||
} else {
|
|
||||||
zipHeaders.set(w)
|
|
||||||
h.writeZip(w)
|
|
||||||
}
|
|
||||||
sent++
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h FileHandler) FileHandler(w http.ResponseWriter, r *http.Request) {
|
func (h FileHandler) FileHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if h.hasHitMax() {
|
if sent >= h.Ctx.N && h.Ctx.N != -1 {
|
||||||
w.WriteHeader(http.StatusTooManyRequests)
|
w.WriteHeader(http.StatusTooManyRequests)
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
return
|
return
|
||||||
@@ -80,45 +54,29 @@ func (h FileHandler) FileHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
if len(h.FileData) == 1 {
|
if len(h.FileData) == 1 {
|
||||||
headers := headers{
|
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", h.FileData[0].FileName))
|
||||||
"Content-Disposition": fmt.Sprintf("attachment; filename=%s", h.FileData[0].FileName),
|
w.Header().Set("Content-Type", "application/octet-stream")
|
||||||
"Content-Type": "application/octet-stream",
|
w.Header().Set("Content-Length", strconv.Itoa(len(h.FileData[0].FileName)))
|
||||||
"Content-Length": strconv.Itoa(len(h.FileData[0].FileName)),
|
|
||||||
}
|
|
||||||
headers.set(w)
|
|
||||||
|
|
||||||
w.Write(h.FileData[0].Contents)
|
w.Write(h.FileData[0].Contents)
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
zipHeaders.set(w)
|
w.Header().Set("Content-Disposition", "attachment; filename=lash.zip")
|
||||||
h.writeZip(w)
|
w.Header().Set("Content-Type", "application/octet-stream")
|
||||||
|
zw := zip.NewWriter(w)
|
||||||
|
defer zw.Close()
|
||||||
|
|
||||||
|
for _, f := range h.FileData {
|
||||||
|
w, err := zw.Create(f.FileName)
|
||||||
|
if err != nil {
|
||||||
|
errx.FatalPerror(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = w.Write(f.Contents)
|
||||||
|
if err != nil {
|
||||||
|
errx.FatalPerror(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
sent++
|
sent++
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h FileHandler) writeZip(hw http.ResponseWriter) {
|
|
||||||
zw := zip.NewWriter(hw)
|
|
||||||
defer zw.Close()
|
|
||||||
|
|
||||||
for _, f := range h.FileData {
|
|
||||||
w, err := zw.Create(f.FileName)
|
|
||||||
if err != nil {
|
|
||||||
errx.FatalPerror(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = w.Write(f.Contents)
|
|
||||||
if err != nil {
|
|
||||||
errx.FatalPerror(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h headers) set(w http.ResponseWriter) {
|
|
||||||
for k, v := range h {
|
|
||||||
w.Header().Set(k, v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h FileHandler) hasHitMax() bool {
|
|
||||||
return sent >= h.Ctx.N && h.Ctx.N != -1
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -24,10 +24,11 @@
|
|||||||
<script>
|
<script>
|
||||||
const tokenInput = document.getElementById('tokenInput');
|
const tokenInput = document.getElementById('tokenInput');
|
||||||
const submitBtn = document.getElementById('submitBtn');
|
const submitBtn = document.getElementById('submitBtn');
|
||||||
|
|
||||||
const status = document.getElementById('status');
|
const status = document.getElementById('status');
|
||||||
|
|
||||||
submitBtn.addEventListener('click', async () => {
|
submitBtn.addEventListener('click', async () => {
|
||||||
const token = tokenInput.value.trim();
|
const token = tokenInput.value.trim();
|
||||||
|
|
||||||
if (!token) {
|
if (!token) {
|
||||||
status.textContent = 'Please enter a token.';
|
status.textContent = 'Please enter a token.';
|
||||||
return;
|
return;
|
||||||
@@ -41,6 +42,7 @@
|
|||||||
},
|
},
|
||||||
body: JSON.stringify({ token })
|
body: JSON.stringify({ token })
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
status.textContent = 'Download failed.';
|
status.textContent = 'Download failed.';
|
||||||
return;
|
return;
|
||||||
@@ -48,19 +50,19 @@
|
|||||||
|
|
||||||
const blob = await response.blob();
|
const blob = await response.blob();
|
||||||
const url = window.URL.createObjectURL(blob);
|
const url = window.URL.createObjectURL(blob);
|
||||||
a.href = url;
|
|
||||||
|
|
||||||
const a = document.createElement('a');
|
const a = document.createElement('a');
|
||||||
|
a.href = url;
|
||||||
const disposition = response.headers.get('Content-Disposition');
|
const disposition = response.headers.get('Content-Disposition');
|
||||||
|
|
||||||
let filename = 'download.bin';
|
let filename = 'download.bin';
|
||||||
|
|
||||||
if (disposition && disposition.includes('filename=')) {
|
if (disposition && disposition.includes('filename=')) {
|
||||||
filename = disposition
|
filename = disposition
|
||||||
.split('filename=')[1]
|
.split('filename=')[1]
|
||||||
.replace(/"/g, '');
|
.replace(/"/g, '');
|
||||||
}
|
}
|
||||||
a.download = filename;
|
|
||||||
|
|
||||||
|
a.download = filename;
|
||||||
document.body.appendChild(a);
|
document.body.appendChild(a);
|
||||||
a.click();
|
a.click();
|
||||||
a.remove();
|
a.remove();
|
||||||
|
|||||||
Reference in New Issue
Block a user