Commit 4f44b6f7 authored by Lukas Böhm's avatar Lukas Böhm 🎱
Browse files

file upload working

parent 15788281
...@@ -35,7 +35,6 @@ func (fn endpointREST) ServeHTTP(w http.ResponseWriter, r *http.Request) { ...@@ -35,7 +35,6 @@ func (fn endpointREST) ServeHTTP(w http.ResponseWriter, r *http.Request) {
///////////////////////////////// /////////////////////////////////
//////////// routes ///////////// //////////// routes /////////////
///////////////////////////////// /////////////////////////////////
func AllShares(w http.ResponseWriter, _ *http.Request) *HTTPError { func AllShares(w http.ResponseWriter, _ *http.Request) *HTTPError {
fmt.Println("AllShares") fmt.Println("AllShares")
db, err := GetDatabase() db, err := GetDatabase()
...@@ -248,11 +247,11 @@ func UploadAttachment(w http.ResponseWriter, r *http.Request) *HTTPError { ...@@ -248,11 +247,11 @@ func UploadAttachment(w http.ResponseWriter, r *http.Request) *HTTPError {
// Parse file from body // Parse file from body
err = r.ParseMultipartForm(config.chunkSize) // Maximum 10 MB in RAM err = r.ParseMultipartForm(config.chunkSize) // Maximum 10 MB in RAM
if err != nil { if err != nil {
return &HTTPError{err, "Request does not contain a valid body", 400} return &HTTPError{err, "Request does not contain a valid body (parsing form)", 400}
} }
file, handler, err := r.FormFile("files") file, handler, err := r.FormFile("file")
if err != nil { if err != nil {
return &HTTPError{err, "Request does not contain a valid body", 400} return &HTTPError{err, "Request does not contain a valid body (parsing file)", 400}
} }
defer file.Close() // TODO fehlerbehandlung defer file.Close() // TODO fehlerbehandlung
......
...@@ -262,7 +262,7 @@ func TestUploadAttachment(t *testing.T) { ...@@ -262,7 +262,7 @@ func TestUploadAttachment(t *testing.T) {
t.Run("happy path", func(t *testing.T) { t.Run("happy path", func(t *testing.T) {
body := &bytes.Buffer{} body := &bytes.Buffer{}
writer := multipart.NewWriter(body) writer := multipart.NewWriter(body)
fw, _ := writer.CreateFormFile("files", "poggers.txt") fw, _ := writer.CreateFormFile("file", "poggers.txt")
io.Copy(fw, strings.NewReader("POG POG POG POG")) io.Copy(fw, strings.NewReader("POG POG POG POG"))
writer.Close() writer.Close()
......
...@@ -7,47 +7,67 @@ ...@@ -7,47 +7,67 @@
</div> </div>
<div class="card mb-4 text-center shadow" id="app"> <div class="card mb-4 text-center shadow" id="app">
<form enctype="multipart/form-data" id="form">
<div class="card-header">
<h4 class="my-0 font-weight-normal">
<input autocomplete="off" class="form-control" id="name" maxlength="64" name="name" placeholder="Name vergeben (optional)" size="64" type="text" value="">
</h4>
</div>
<div class="card-body">
<div class="custom-file mb-3"> <div class="card-header">
<input class="custom-file-input" id="files" multiple name="files" required type="file"> <h4 class="my-0 font-weight-normal">
<label class="custom-file-label truncate" for="files">Dateien hochladen</label> <input autocomplete="off" class="form-control" id="name" maxlength="64" name="name"
</div> placeholder="Name vergeben (optional)" size="64" type="text" value="">
</h4>
</div>
<div class="card-body">
<div class="row form-group"> <div class="custom-file mb-3">
<select class="selectpicker col" id="timer" name="timer" required><option value="3">3 Minuten</option><option value="15">15 Minuten</option><option value="30">30 Minuten</option><option value="120">2 Stunden</option><option value="360">6 Stunden</option><option selected value="1440">1 Tag</option><option value="10080">7 Tage</option><option value="43200">1 Monat</option><option value="-1">Für immer</option></select> <input class="custom-file-input" id="files" multiple name="files" required type="file">
<select class="selectpicker col" id="download_limit" name="download_limit" required><option value="1">1 Download</option><option value="5">5 Downloads</option><option selected value="15">15 Downloads</option><option value="50">50 Downloads</option><option value="200">200 Downloads</option><option value="1000">1000 Downloads</option><option value="-1">Unbegrenzt</option></select> <label class="custom-file-label truncate" for="files">Dateien hochladen</label>
</div> </div>
<div class="row form-group"> <div class="row form-group">
<p class="custom-control custom-switch col"> <select class="selectpicker col" id="timer" name="timer" required>
<input class="custom-control-input" id="is_public" name="is_public" type="checkbox" value="y"> <option value="3">3 Minuten</option>
<label class="custom-control-label" for="is_public">Öffentlich</label> <option value="15">15 Minuten</option>
</p> <option value="30">30 Minuten</option>
<div class="col"> <option value="120">2 Stunden</option>
<input autocomplete="off" class="form-control" id="password" name="password" placeholder="Passwort ..." type="text" value=""> <option value="360">6 Stunden</option>
</div> <option selected value="1440">1 Tag</option>
</div> <option value="10080">7 Tage</option>
<option value="43200">1 Monat</option>
<option value="-1">Für immer</option>
</select>
<select class="selectpicker col" id="download_limit" name="download_limit" required>
<option value="1">1 Download</option>
<option value="5">5 Downloads</option>
<option selected value="15">15 Downloads</option>
<option value="50">50 Downloads</option>
<option value="200">200 Downloads</option>
<option value="1000">1000 Downloads</option>
<option value="-1">Unbegrenzt</option>
</select>
</div>
<div class="progress form-group d-none"> <div class="row form-group">
<div class="progress-bar" role="progressbar" aria-valuemin="0" aria-valuemax="100"></div> <p class="custom-control custom-switch col">
<input class="custom-control-input" id="is_public" name="is_public" type="checkbox" value="y">
<label class="custom-control-label" for="is_public">Öffentlich</label>
</p>
<div class="col">
<input autocomplete="off" class="form-control" id="password" name="password"
placeholder="Passwort ..." type="text" value="">
</div> </div>
</div>
<div class="form-group"> <div class="progress form-group d-none">
<input class="btn btn-lg btn-block btn-primary" id="submit" name="submit" type="button" value="Hochladen" onclick="openShare()"> <div class="progress-bar" role="progressbar" aria-valuemin="0" aria-valuemax="100"></div>
</div> </div>
<div class="form-group">
<input class="btn btn-lg btn-block btn-primary" id="submit" name="submit" type="button"
value="Hochladen" onclick="openShare()">
</div> </div>
</form>
</div>
</div> </div>
{# <script src="{{ url_for('static', filename='js/jquery.form.min.js') }}"></script>#} <script src="{{ url_for('static', filename='js/jquery.form.min.js') }}"></script>
<style> <style>
.truncate { .truncate {
width: 100%; width: 100%;
...@@ -57,8 +77,8 @@ ...@@ -57,8 +77,8 @@
} }
</style> </style>
<script> <script>
// to let the name of the file appear on select // To show filenames in the file selector.
$(".custom-file-input").on("change", function() { $(".custom-file-input").on("change", function () {
let fns = $(this)[0].files; let fns = $(this)[0].files;
let label = ""; let label = "";
for (let i = 0; i < fns.length; i++) { for (let i = 0; i < fns.length; i++) {
...@@ -72,6 +92,8 @@ ...@@ -72,6 +92,8 @@
let share = {}; let share = {};
function openShare() { function openShare() {
console.log("openShare");
$.ajax({ $.ajax({
"url": "http://localhost:6969/shares", "url": "http://localhost:6969/shares",
"method": "POST", "method": "POST",
...@@ -82,34 +104,47 @@ ...@@ -82,34 +104,47 @@
"data": JSON.stringify({}), "data": JSON.stringify({}),
}).done((response) => { }).done((response) => {
share = response; share = response;
console.log(share);
disableAll(); disableAll();
uploadFiles(); uploadFiles();
}); });
} }
function uploadFiles() { function uploadFiles() {
for (file in $(".custom-file-input")[0].files) { console.log("uploadFiles");
let files = $(".custom-file-input")[0].files;
for (let i = 0; i < files.length; i++) {
let file = files.item(i);
console.log(file);
var formData = new FormData(); var formData = new FormData();
var request = new XMLHttpRequest(); formData.set('file', file);
$.ajax({
formData.set('files', file); url: `http://localhost:6969/share/${share.id}/attachments`,
request.open("POST", `http://localhost:6969/share/${ share.id }/attachments`); type: 'post',
request.send(formData); data: formData,
contentType: false,
processData: false,
success: function (response) {
console.log(response);
},
});
} }
closeShare(); closeShare();
} }
function closeShare() { function closeShare() {
console.log("closeShare");
$.ajax({ $.ajax({
"url": `http://localhost:6969/share/${ share.id }`, url: `http://localhost:6969/share/${share.id}`,
"method": "POST", method: "POST",
"timeout": 0, timeout: 0,
"headers": { headers: {
"Content-Type": "application/json" "Content-Type": "application/json"
}, },
"data": JSON.stringify({}),
}).done((response) => { }).done((response) => {
console.log(response); share = response;
console.log(share);
{#window.location.href = "/d/" + share.id;#} {#window.location.href = "/d/" + share.id;#}
}); });
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment