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

import: don't create file if source uri is not supported at

parent c99f9bcc
No related branches found
No related tags found
No related merge requests found
......@@ -28,7 +28,6 @@ import (
"context"
"encoding/json"
"net/http"
"net/url"
"time"
"github.com/gin-gonic/gin"
......@@ -68,7 +67,7 @@ func (api *API) CreateFileForShow(c *gin.Context) {
c.JSON(http.StatusBadRequest, ErrorResponse{Error: "source-uri is mandatory"})
return
}
srcURI, err := url.Parse(request.SourceURI)
srcURI, err := api.importer.ParseAndVerifySourceURI(request.SourceURI)
if err != nil {
c.JSON(http.StatusBadRequest, ErrorResponse{Error: "source-uri is invalid: " + err.Error()})
return
......@@ -93,10 +92,9 @@ func (api *API) CreateFileForShow(c *gin.Context) {
// file or at least return the id of the created file so the UI can deal with it.
refID := "" // TODO: get this from query paremeter
job, err := api.importer.CreateJob(showID, file.ID, *srcURI, sess.Username, refID)
job, err := api.importer.CreateJob(showID, file.ID, srcURI, sess.Username, refID)
if err != nil {
// possible errors:
// - importer.ErrSourceNotSupported -> delete file
// - any error returned by store.GetFile():
// possible reasons for this:
// * connection problem to the store database
......
......@@ -52,7 +52,12 @@ func (im *Importer) ListJobs(show string) (Jobs, error) {
return im.jobs.ListJobs(show), nil // for now error is always nil but this might change later
}
func (im *Importer) CreateJob(show string, id uint64, src url.URL, user, refID string) (*Job, error) {
func (im *Importer) ParseAndVerifySourceURI(uri string) (*SourceURL, error) {
src, err := url.Parse(uri)
if err != nil {
return nil, err
}
switch src.Scheme {
case SourceSchemeUpload:
case SourceSchemeFake:
......@@ -62,6 +67,10 @@ func (im *Importer) CreateJob(show string, id uint64, src url.URL, user, refID s
return nil, ErrSourceNotSupported
}
return (*SourceURL)(src), nil
}
func (im *Importer) CreateJob(show string, id uint64, src *SourceURL, user, refID string) (*Job, error) {
file, err := im.store.GetFile(show, id)
if err != nil {
return nil, err
......
......@@ -28,7 +28,6 @@ import (
"context"
"io"
"io/ioutil"
"net/url"
"os"
"sync/atomic"
"time"
......@@ -220,8 +219,8 @@ func (job *Job) cleanup() {
}
}
func newJob(im *Importer, show string, id uint64, src url.URL, user, refID string) *Job {
job := &Job{im: im, Show: show, ID: id, Source: SourceURL(src), User: user, RefID: refID}
func newJob(im *Importer, show string, id uint64, src *SourceURL, user, refID string) *Job {
job := &Job{im: im, Show: show, ID: id, Source: *src, User: user, RefID: refID}
job.State = JobNew
job.CreatedAt.set(time.Now())
job.subC.sourceAttached = make(chan struct{})
......
......@@ -57,6 +57,8 @@ func (job *Job) initializeSource() (err error) {
case SourceSchemeFake:
job.source = newJobSourceFake(job.Source)
default:
// Naughty you! You should have used importer.ParseAndVerifySourceURI which would have
// told you right away that this uri scheme is not supported
return ErrSourceNotSupported
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment