Skip to content

Commit

Permalink
Add multipart upload to instances
Browse files Browse the repository at this point in the history
  • Loading branch information
xetorthio committed Jun 21, 2017
1 parent 49ad6cd commit eee7695
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
31 changes: 24 additions & 7 deletions handlers/file_upload.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handlers

import (
"io"
"log"
"net/http"

Expand Down Expand Up @@ -28,18 +29,34 @@ func FileUpload(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(http.StatusOK)
return
} else {
// This is for multipart upload
log.Println("Not implemented yet")
red, err := req.MultipartReader()
if err != nil {
log.Println(err)
rw.WriteHeader(http.StatusBadRequest)
return
}
for {
p, err := red.NextPart()
if err == io.EOF {
break
}
if err != nil {
log.Println(err)
continue
}

/*
err := req.ParseMultipartForm(32 << 20)
if p.FileName() == "" {
continue
}
err = core.InstanceUploadFromReader(i, p.FileName(), p)
if err != nil {
log.Println(err)
rw.WriteHeader(http.StatusBadRequest)
rw.WriteHeader(http.StatusInternalServerError)
return
}
*/
rw.WriteHeader(http.StatusInternalServerError)
log.Printf("Uploaded [%s] to [%s]\n", p.FileName(), i.Name)
}
rw.WriteHeader(http.StatusOK)
return
}

Expand Down
12 changes: 12 additions & 0 deletions pwd/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,18 @@ func (p *pwd) InstanceUploadFromUrl(instance *Instance, url string) error {
return nil
}

func (p *pwd) InstanceUploadFromReader(instance *Instance, fileName string, reader io.Reader) error {
defer observeAction("InstanceUploadFromReader", time.Now())

copyErr := p.docker.CopyToContainer(instance.Name, "/var/run/pwd/uploads", fileName, reader)

if copyErr != nil {
return fmt.Errorf("Error while uploading file [%s]. Error: %s\n", fileName, copyErr)
}

return nil
}

func (p *pwd) InstanceGet(session *Session, name string) *Instance {
defer observeAction("InstanceGet", time.Now())
return session.Instances[name]
Expand Down
2 changes: 2 additions & 0 deletions pwd/pwd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pwd

import (
"io"
"sync"
"time"

Expand Down Expand Up @@ -65,6 +66,7 @@ type PWDApi interface {
InstanceResizeTerminal(instance *Instance, cols, rows uint) error
InstanceAttachTerminal(instance *Instance) error
InstanceUploadFromUrl(instance *Instance, url string) error
InstanceUploadFromReader(instance *Instance, filename string, reader io.Reader) error
InstanceGet(session *Session, name string) *Instance
InstanceFindByIP(ip string) *Instance
InstanceFindByAlias(sessionPrefix, alias string) *Instance
Expand Down

0 comments on commit eee7695

Please sign in to comment.