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) {
/////////////////////////////////
//////////// routes /////////////
/////////////////////////////////
func AllShares(w http.ResponseWriter, _ *http.Request) *HTTPError {
fmt.Println("AllShares")
db, err := GetDatabase()
......@@ -248,11 +247,11 @@ func UploadAttachment(w http.ResponseWriter, r *http.Request) *HTTPError {
// Parse file from body
err = r.ParseMultipartForm(config.chunkSize) // Maximum 10 MB in RAM
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 {
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
......
......@@ -262,7 +262,7 @@ func TestUploadAttachment(t *testing.T) {
t.Run("happy path", func(t *testing.T) {
body := &bytes.Buffer{}
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"))
writer.Close()
......
......@@ -7,47 +7,67 @@
</div>
<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">
<input class="custom-file-input" id="files" multiple name="files" required type="file">
<label class="custom-file-label truncate" for="files">Dateien hochladen</label>
</div>
<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="row form-group">
<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>
<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="custom-file mb-3">
<input class="custom-file-input" id="files" multiple name="files" required type="file">
<label class="custom-file-label truncate" for="files">Dateien hochladen</label>
</div>
<div class="row form-group">
<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 class="row form-group">
<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>
<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="progress-bar" role="progressbar" aria-valuemin="0" aria-valuemax="100"></div>
<div class="row form-group">
<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 class="form-group">
<input class="btn btn-lg btn-block btn-primary" id="submit" name="submit" type="button" value="Hochladen" onclick="openShare()">
</div>
<div class="progress form-group d-none">
<div class="progress-bar" role="progressbar" aria-valuemin="0" aria-valuemax="100"></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>
</form>
</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>
.truncate {
width: 100%;
......@@ -57,8 +77,8 @@
}
</style>
<script>
// to let the name of the file appear on select
$(".custom-file-input").on("change", function() {
// To show filenames in the file selector.
$(".custom-file-input").on("change", function () {
let fns = $(this)[0].files;
let label = "";
for (let i = 0; i < fns.length; i++) {
......@@ -72,6 +92,8 @@
let share = {};
function openShare() {
console.log("openShare");
$.ajax({
"url": "http://localhost:6969/shares",
"method": "POST",
......@@ -82,34 +104,47 @@
"data": JSON.stringify({}),
}).done((response) => {
share = response;
console.log(share);
disableAll();
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 request = new XMLHttpRequest();
formData.set('files', file);
request.open("POST", `http://localhost:6969/share/${ share.id }/attachments`);
request.send(formData);
formData.set('file', file);
$.ajax({
url: `http://localhost:6969/share/${share.id}/attachments`,
type: 'post',
data: formData,
contentType: false,
processData: false,
success: function (response) {
console.log(response);
},
});
}
closeShare();
}
function closeShare() {
console.log("closeShare");
$.ajax({
"url": `http://localhost:6969/share/${ share.id }`,
"method": "POST",
"timeout": 0,
"headers": {
url: `http://localhost:6969/share/${share.id}`,
method: "POST",
timeout: 0,
headers: {
"Content-Type": "application/json"
},
"data": JSON.stringify({}),
}).done((response) => {
console.log(response);
share = response;
console.log(share);
{#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