Skip to content
Snippets Groups Projects
Commit b861db61 authored by Christian Pointner's avatar Christian Pointner
Browse files

some refactoring

parent 8470e586
No related branches found
No related tags found
No related merge requests found
...@@ -130,6 +130,8 @@ func (ch *FlowJSFileChunk) Cleanup() (err error) { ...@@ -130,6 +130,8 @@ func (ch *FlowJSFileChunk) Cleanup() (err error) {
return os.Remove(filename) return os.Remove(filename)
} }
//***
type FlowJSFile struct { type FlowJSFile struct {
id string id string
size uint64 size uint64
...@@ -192,6 +194,45 @@ func (f *FlowJSFile) Read(p []byte) (n int, err error) { ...@@ -192,6 +194,45 @@ func (f *FlowJSFile) Read(p []byte) (n int, err error) {
} }
} }
//***
func getOrNewFlowJSFile(job *importer.Job, flowId string, chunk, totalChunks, totalSize uint64) (file *FlowJSFile, err error) {
var src io.Reader
if src, err = job.GetAttachedUploader(); err != nil && err != importer.ErrSourceNotYetAttached {
return
}
if err == importer.ErrSourceNotYetAttached {
if file, err = newFlowJSFile(flowId, totalChunks, totalSize, job); err != nil {
return
}
_, err = job.AttachUploader(file.size, file)
switch err {
case nil:
case importer.ErrSourceAlreadyAttached:
// there has been a race and the other thread won!
file = nil
if src, err = job.GetAttachedUploader(); err != nil {
return
}
default:
return
}
}
if file == nil {
var ok bool
if file, ok = src.(*FlowJSFile); !ok {
err = ErrNotFlowJSUpload
return
}
}
return
}
//***
func (api *API) UploadFileFlowJS() http.Handler { func (api *API) UploadFileFlowJS() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// TODO: implement this // TODO: implement this
...@@ -240,40 +281,10 @@ func (api *API) TestFileFlowJS() http.Handler { ...@@ -240,40 +281,10 @@ func (api *API) TestFileFlowJS() http.Handler {
sendError(w, err) sendError(w, err)
return return
} }
src, err := job.GetAttachedUploader()
if err != nil && err != importer.ErrSourceNotYetAttached {
sendError(w, err)
return
}
var file *FlowJSFile
if err == importer.ErrSourceNotYetAttached {
if file, err = newFlowJSFile(flowId, totalChunks, totalSize, job); err != nil {
sendError(w, err)
return
}
_, err = job.AttachUploader(file.size, file) file, err := getOrNewFlowJSFile(job, flowId, chunk, totalChunks, totalSize)
switch err {
case nil:
case importer.ErrSourceAlreadyAttached:
// there has been a race and the other thread won!
file = nil
if src, err = job.GetAttachedUploader(); err != nil {
sendError(w, err)
return
}
default:
sendError(w, err)
return
}
}
if file == nil { if file == nil {
var ok bool sendError(w, err)
if file, ok = src.(*FlowJSFile); !ok {
sendWebResponse(w, http.StatusConflict, ErrorResponse{Error: "this is not a flow.js upload"})
return
}
} }
if chunk > uint64(len(file.chunks)) { if chunk > uint64(len(file.chunks)) {
......
...@@ -26,6 +26,7 @@ package v1 ...@@ -26,6 +26,7 @@ package v1
import ( import (
"encoding/json" "encoding/json"
"errors"
"net/http" "net/http"
"strconv" "strconv"
...@@ -33,6 +34,10 @@ import ( ...@@ -33,6 +34,10 @@ import (
"gitlab.servus.at/autoradio/tank/store" "gitlab.servus.at/autoradio/tank/store"
) )
var (
ErrNotFlowJSUpload = errors.New("this is not a flow.js upload")
)
func idFromString(s string) (uint64, error) { func idFromString(s string) (uint64, error) {
return strconv.ParseUint(s, 10, 64) return strconv.ParseUint(s, 10, 64)
} }
...@@ -48,6 +53,9 @@ func sendError(w http.ResponseWriter, err error) { ...@@ -48,6 +53,9 @@ func sendError(w http.ResponseWriter, err error) {
response.Details = err.(*importer.JobSourceResult).Log response.Details = err.(*importer.JobSourceResult).Log
default: default:
switch err { switch err {
case ErrNotFlowJSUpload:
code = http.StatusConflict
case store.ErrNotImplemented: case store.ErrNotImplemented:
code = http.StatusNotImplemented code = http.StatusNotImplemented
case store.ErrNotFound: case store.ErrNotFound:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment