Skip to content

Commit

Permalink
fix: lots of little cleanups here & there
Browse files Browse the repository at this point in the history
  • Loading branch information
b5 committed Nov 9, 2017
1 parent c8e5a57 commit b44c749
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 21 deletions.
58 changes: 42 additions & 16 deletions api/handlers/datasets.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ func (h *DatasetHandlers) DatasetHandler(w http.ResponseWriter, r *http.Request)
}
}

func (h *DatasetHandlers) InitDatasetHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "OPTIONS":
util.EmptyOkHandler(w, r)
case "POST":
h.initDatasetHandler(w, r)
default:
util.NotFoundHandler(w, r)
}
}

func (h *DatasetHandlers) StructuredDataHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "OPTIONS":
Expand Down Expand Up @@ -134,7 +145,7 @@ func (h *DatasetHandlers) saveDatasetHandler(w http.ResponseWriter, r *http.Requ
case "application/json":
h.saveStructureHandler(w, r)
default:
h.initDatasetFileHandler(w, r)
h.initDatasetHandler(w, r)
}
}

Expand Down Expand Up @@ -178,21 +189,28 @@ func (h *DatasetHandlers) saveStructureHandler(w http.ResponseWriter, r *http.Re
util.WriteResponse(w, res)
}

func (h *DatasetHandlers) initDatasetFileHandler(w http.ResponseWriter, r *http.Request) {
var f cafs.File
infile, header, err := r.FormFile("file")
if err != nil && err != http.ErrMissingFile {
util.WriteErrResponse(w, http.StatusBadRequest, err)
return
func (h *DatasetHandlers) initDatasetHandler(w http.ResponseWriter, r *http.Request) {
p := &core.InitDatasetParams{}
if r.Header.Get("Content-Type") == "application/json" {
json.NewDecoder(r.Body).Decode(p)
} else {
f = memfs.NewMemfileReader(header.Filename, infile)
}
var f cafs.File
infile, header, err := r.FormFile("file")
if err != nil && err != http.ErrMissingFile {
util.WriteErrResponse(w, http.StatusBadRequest, err)
return
} else {
f = memfs.NewMemfileReader(header.Filename, infile)
}

p := &core.InitDatasetParams{
Url: r.FormValue("url"),
Name: r.FormValue("name"),
Data: f,
p = &core.InitDatasetParams{
Url: r.FormValue("url"),
Name: r.FormValue("name"),
DataFilename: header.Filename,
Data: f,
}
}

res := &repo.DatasetRef{}
if err := h.InitDataset(p, res); err != nil {
h.log.Infof("error initializing dataset: %s", err.Error())
Expand Down Expand Up @@ -254,9 +272,17 @@ func (h *DatasetHandlers) getStructuredDataHandler(w http.ResponseWriter, r *htt
}

func (h *DatasetHandlers) addDatasetHandler(w http.ResponseWriter, r *http.Request) {
p := &core.AddParams{
Name: r.URL.Query().Get("name"),
Hash: r.URL.Path[len("/add/"):],
p := &core.AddParams{}
if r.Header.Get("Content-Type") == "application/json" {
if err := json.NewDecoder(r.Body).Decode(p); err != nil {
util.WriteErrResponse(w, http.StatusBadRequest, err)
return
}
} else {
p = &core.AddParams{
Name: r.URL.Query().Get("name"),
Hash: r.URL.Path[len("/add/"):],
}
}

res := &repo.DatasetRef{}
Expand Down
1 change: 1 addition & 0 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ func NewServerRoutes(s *Server) *http.ServeMux {
m.Handle("/datasets", s.middleware(dsh.DatasetsHandler))
m.Handle("/datasets/", s.middleware(dsh.DatasetHandler))
m.Handle("/add/", s.middleware(dsh.AddDatasetHandler))
m.Handle("/init/", s.middleware(dsh.InitDatasetHandler))
m.Handle("/data/ipfs/", s.middleware(dsh.StructuredDataHandler))
m.Handle("/download/", s.middleware(dsh.ZipDatasetHandler))

Expand Down
5 changes: 1 addition & 4 deletions cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package cmd

import (
"fmt"
"github.com/ipfs/go-datastore"
"io"
"os"

"github.com/ipfs/go-datastore"
"github.com/qri-io/dataset/dsfs"
"github.com/qri-io/dataset/dsutil"
"github.com/qri-io/qri/core"
Expand All @@ -31,7 +31,6 @@ var exportCmd = &cobra.Command{

r := GetRepo(false)
store := GetIpfsFilestore(false)

req := core.NewDatasetRequests(store, r)

p := &core.GetDatasetParams{
Expand All @@ -44,8 +43,6 @@ var exportCmd = &cobra.Command{

ds := res.Dataset

// ds, err := FindDataset(r, store, args[0])

if cmd.Flag("data-only").Value.String() == "true" {
src, err := dsfs.LoadData(store, ds)
ExitIfErr(err)
Expand Down
13 changes: 13 additions & 0 deletions core/dataset_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,20 @@ func (r *DatasetRequests) Update(commit *Commit, ref *repo.DatasetRef) error {
return fmt.Errorf("error saving dataset: %s", err.Error())
}

name, err := r.repo.GetName(commit.Prev)
if err != nil {
return err
}

if err := r.repo.DeleteName(name); err != nil {
return err
}
if err := r.repo.PutName(name, dspath); err != nil {
return err
}

*ref = repo.DatasetRef{
Name: name,
Path: dspath,
Dataset: ds,
}
Expand Down
1 change: 0 additions & 1 deletion repo/fs/namestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ func (n Namestore) Namespace(limit, offset int) ([]*repo.DatasetRef, error) {

i := -1
added := 0
limit = len(names)
res := make([]*repo.DatasetRef, limit)
for name, path := range names {
i++
Expand Down

0 comments on commit b44c749

Please sign in to comment.