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 { ...@@ -92,65 +92,50 @@ func pageUpload(ctx PageContext) http.Handler {
} }
} }
var images []Image file, header, err := r.FormFile("file")
var ids []string if err != nil {
if err = returnJson(w, []Result{{
m := r.MultipartForm Success: false,
files := m.File["file"] Errors: []string{err.Error()},
for _, header := range files { }}); err != nil {
file, err := header.Open() panic(err)
if err != nil {
if err = returnJson(w, []Result{{
Success: false,
Errors: []string{err.Error()},
}}); err != nil {
panic(err)
}
return
} }
image, err := createImage(ctx.Config, file, header) return
if err != nil { }
if err = returnJson(w, []Result{{ image, err := createImage(ctx.Config, file, header)
Success: false, if err != nil {
Errors: []string{err.Error()}, if err = returnJson(w, []Result{{
}}); err != nil { Success: false,
panic(err) Errors: []string{err.Error()},
} }}); err != nil {
return panic(err)
} }
return
images = append(images, image)
ids = append(ids, image.Id)
} }
pubsub := ctx.Redis.Subscribe(ctx.Config.ResultChannel) pubsub := ctx.Redis.Subscribe(ctx.Config.ResultChannel)
waiting := make(map[string]bool) _, 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)
for _, image := range images { if err != nil {
_, 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) panic(err)
if err != nil { }
panic(err)
}
data, err := json.Marshal(image) data, err := json.Marshal(image)
if err != nil { if err != nil {
if err = returnJson(w, []Result{{ if err = returnJson(w, []Result{{
Success: false, Success: false,
Errors: []string{err.Error()}, Errors: []string{err.Error()},
}}); err != nil { }}); err != nil {
panic(err) panic(err)
}
return
} }
return
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
} }
var results []Result fmt.Printf("Created task %s at %d\n", image.Id, time.Now().Unix())
for len(waiting) != 0 { 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() message, err := pubsub.ReceiveMessage()
if err != nil { if err != nil {
if err = returnJson(w, []Result{{ if err = returnJson(w, []Result{{
...@@ -176,16 +161,14 @@ func pageUpload(ctx PageContext) http.Handler { ...@@ -176,16 +161,14 @@ func pageUpload(ctx PageContext) http.Handler {
fmt.Printf("Returned task %s at %d\n", result.Id, time.Now().Unix()) fmt.Printf("Returned task %s at %d\n", result.Id, time.Now().Unix())
if _, ok := waiting[result.Id]; ok { if result.Id == image.Id {
delete(waiting, result.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 return
} else { } else {
user := parseUser(r) user := parseUser(r)
......
...@@ -30,6 +30,17 @@ ...@@ -30,6 +30,17 @@
<div class="uploading images"></div> <div class="uploading images"></div>
</div> </div>
<script> <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 form = document.querySelector("form.upload");
const element = document.querySelector("form.upload input[type=file]"); const element = document.querySelector("form.upload input[type=file]");
const results = document.querySelector(".uploading.images"); const results = document.querySelector(".uploading.images");
...@@ -46,15 +57,9 @@ ...@@ -46,15 +57,9 @@
results.appendChild(node); results.appendChild(node);
const data = new FormData(); const data = new FormData();
data.append("file", file); data.append("file", file, file.name);
fetch("/upload", { postData("/upload/", data).then((json) => {
method: "POST",
credentials: "same-origin",
body: data
}).then((response) => {
return response.json()
}).then((json) => {
const text = document.createElement("pre"); const text = document.createElement("pre");
text.innerText = JSON.stringify(json); text.innerText = JSON.stringify(json);
node.appendChild(text); 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