Commit 088d5d4c authored by Janne Koschinski's avatar Janne Koschinski

Improved upload API

parent 023dd900
......@@ -92,65 +92,50 @@ func pageUpload(ctx PageContext) http.Handler {
}
}
var images []Image
var ids []string
m := r.MultipartForm
files := m.File["file"]
for _, header := range files {
file, err := header.Open()
if err != nil {
if err = returnJson(w, []Result{{
Success: false,
Errors: []string{err.Error()},
}}); err != nil {
panic(err)
}
return
file, header, err := r.FormFile("file")
if err != nil {
if err = returnJson(w, []Result{{
Success: false,
Errors: []string{err.Error()},
}}); err != nil {
panic(err)
}
image, err := createImage(ctx.Config, file, header)
if err != nil {
if err = returnJson(w, []Result{{
Success: false,
Errors: []string{err.Error()},
}}); err != nil {
panic(err)
}
return
return
}
image, err := createImage(ctx.Config, file, header)
if err != nil {
if err = returnJson(w, []Result{{
Success: false,
Errors: []string{err.Error()},
}}); err != nil {
panic(err)
}
images = append(images, image)
ids = append(ids, image.Id)
return
}
pubsub := ctx.Redis.Subscribe(ctx.Config.ResultChannel)
waiting := make(map[string]bool)
for _, image := range images {
_, err = ctx.Database.Exec("INSERT INTO images (id, owner, created_at, original_name, type) VALUES ($1, $2, $3, $4, $5)", image.Id, user.Id, image.CreatedAt, image.OriginalName, image.MimeType)
if err != nil {
panic(err)
}
_, err = ctx.Database.Exec("INSERT INTO images (id, owner, created_at, original_name, type) VALUES ($1, $2, $3, $4, $5)", image.Id, user.Id, image.CreatedAt, image.OriginalName, image.MimeType)
if err != nil {
panic(err)
}
data, err := json.Marshal(image)
if err != nil {
if err = returnJson(w, []Result{{
Success: false,
Errors: []string{err.Error()},
}}); err != nil {
panic(err)
}
return
data, err := json.Marshal(image)
if err != nil {
if err = returnJson(w, []Result{{
Success: false,
Errors: []string{err.Error()},
}}); err != nil {
panic(err)
}
fmt.Printf("Created task %s at %d\n", image.Id, time.Now().Unix())
ctx.Redis.RPush(fmt.Sprintf("queue:%s", ctx.Config.ImageQueue), data)
fmt.Printf("Submitted task %s at %d\n", image.Id, time.Now().Unix())
waiting[image.Id] = true
return
}
var results []Result
for len(waiting) != 0 {
fmt.Printf("Created task %s at %d\n", image.Id, time.Now().Unix())
ctx.Redis.RPush(fmt.Sprintf("queue:%s", ctx.Config.ImageQueue), data)
fmt.Printf("Submitted task %s at %d\n", image.Id, time.Now().Unix())
waiting := true
for waiting {
message, err := pubsub.ReceiveMessage()
if err != nil {
if err = returnJson(w, []Result{{
......@@ -176,16 +161,14 @@ func pageUpload(ctx PageContext) http.Handler {
fmt.Printf("Returned task %s at %d\n", result.Id, time.Now().Unix())
if _, ok := waiting[result.Id]; ok {
delete(waiting, result.Id)
if result.Id == image.Id {
waiting = false
results = append(results, result)
if err = returnJson(w, result); err != nil {
panic(err)
}
}
}
if err = returnJson(w, results); err != nil {
panic(err)
}
return
} else {
user := parseUser(r)
......
......@@ -30,6 +30,17 @@
<div class="uploading images"></div>
</div>
<script>
function postData(url, data) {
return fetch(url, {
body: data,
cache: 'no-cache',
credentials: 'same-origin',
method: 'POST',
mode: 'cors',
redirect: 'follow'
}).then(response => response.json())
}
const form = document.querySelector("form.upload");
const element = document.querySelector("form.upload input[type=file]");
const results = document.querySelector(".uploading.images");
......@@ -46,15 +57,9 @@
results.appendChild(node);
const data = new FormData();
data.append("file", file);
data.append("file", file, file.name);
fetch("/upload", {
method: "POST",
credentials: "same-origin",
body: data
}).then((response) => {
return response.json()
}).then((json) => {
postData("/upload/", data).then((json) => {
const text = document.createElement("pre");
text.innerText = JSON.stringify(json);
node.appendChild(text);
......
Markdown is supported
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