Upload on 404 and attachment response header

master
Caj Larsson 3 years ago
parent 630f9220f3
commit 53d5794c96

@ -62,7 +62,19 @@ func (b *Bog) fileHandler(w http.ResponseWriter, r *http.Request) {
swamp_file, err := b.file_service.OpenOutFile(ref) swamp_file, err := b.file_service.OpenOutFile(ref)
if err == swampfile.ErrNotExists { if err == swampfile.ErrNotExists {
http.NotFound(w, r) templ, err := template.ParseFiles("server/views/upload.html")
if err != nil {
panic(err)
}
w.WriteHeader(404)
err = templ.Execute(w, nil)
if err != nil {
panic(err)
}
return return
} }
@ -70,6 +82,7 @@ func (b *Bog) fileHandler(w http.ResponseWriter, r *http.Request) {
panic(err) panic(err)
} }
b.logger.Info("Serving file '%s' of size %d from '%s'", ref.Path, swamp_file.Size(), ref.UserAgent) b.logger.Info("Serving file '%s' of size %d from '%s'", ref.Path, swamp_file.Size(), ref.UserAgent)
w.Header().Set("Content-Disposition", "attachment")
http.ServeContent(w, r, swamp_file.Path(), swamp_file.Modified(), swamp_file) http.ServeContent(w, r, swamp_file.Path(), swamp_file.Modified(), swamp_file)
case "POST": case "POST":
fallthrough fallthrough

@ -0,0 +1,25 @@
<!DOCTYPE html>
<html>
<head>
<title>bog</title>
</head>
<body>
<script>
function upload() {
var file = document.getElementById('file').files[0];
var ajax = new XMLHttpRequest;
const reader = new FileReader();
ajax.open('POST', '', true);
ajax.overrideMimeType('text/plain; charset=x-user-defined-binary');
reader.onload = function(evt) {
ajax.send(evt.target.result);
};
reader.readAsBinaryString(file);
}
</script>
<input id="file" type="file" />
<input type="button" onClick="upload()" value="Upload">
</body>
</html>
Loading…
Cancel
Save