Unify file upload strategies
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
@@ -20,7 +21,10 @@ func FileUpload(rw http.ResponseWriter, req *http.Request) {
|
||||
|
||||
// has a url query parameter, ignore body
|
||||
if url := req.URL.Query().Get("url"); url != "" {
|
||||
err := core.InstanceUploadFromUrl(i, req.URL.Query().Get("url"))
|
||||
|
||||
_, fileName := filepath.Split(url)
|
||||
|
||||
err := core.InstanceUploadFromUrl(i, fileName, "", req.URL.Query().Get("url"))
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
rw.WriteHeader(http.StatusInternalServerError)
|
||||
@@ -35,7 +39,7 @@ func FileUpload(rw http.ResponseWriter, req *http.Request) {
|
||||
rw.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
r := req.URL.Query().Get("relative")
|
||||
path := req.URL.Query().Get("path")
|
||||
|
||||
for {
|
||||
p, err := red.NextPart()
|
||||
@@ -50,21 +54,11 @@ func FileUpload(rw http.ResponseWriter, req *http.Request) {
|
||||
if p.FileName() == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
if r != "" {
|
||||
err = core.InstanceUploadToCWDFromReader(i, p.FileName(), p)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
rw.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
err = core.InstanceUploadFromReader(i, p.FileName(), "/var/run/pwd/uploads", p)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
rw.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
err = core.InstanceUploadFromReader(i, p.FileName(), path, p)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
rw.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("Uploaded [%s] to [%s]\n", p.FileName(), i.Name)
|
||||
|
||||
@@ -60,7 +60,7 @@ func (p *pwd) InstanceAttachTerminal(instance *types.Instance) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *pwd) InstanceUploadFromUrl(instance *types.Instance, url string) error {
|
||||
func (p *pwd) InstanceUploadFromUrl(instance *types.Instance, fileName, dest string, url string) error {
|
||||
defer observeAction("InstanceUploadFromUrl", time.Now())
|
||||
log.Printf("Downloading file [%s]\n", url)
|
||||
resp, err := http.Get(url)
|
||||
@@ -72,9 +72,7 @@ func (p *pwd) InstanceUploadFromUrl(instance *types.Instance, url string) error
|
||||
return fmt.Errorf("Could not download file [%s]. Status code: %d\n", url, resp.StatusCode)
|
||||
}
|
||||
|
||||
_, fileName := filepath.Split(url)
|
||||
|
||||
copyErr := p.docker.CopyToContainer(instance.Name, "/var/run/pwd/uploads", fileName, resp.Body)
|
||||
copyErr := p.docker.CopyToContainer(instance.Name, dest, fileName, resp.Body)
|
||||
|
||||
if copyErr != nil {
|
||||
return fmt.Errorf("Error while downloading file [%s]. Error: %s\n", url, copyErr)
|
||||
@@ -98,26 +96,21 @@ func (p *pwd) getInstanceCWD(instance *types.Instance) (string, error) {
|
||||
return cwd, nil
|
||||
}
|
||||
|
||||
func (p *pwd) InstanceUploadToCWDFromReader(instance *types.Instance, fileName string, reader io.Reader) error {
|
||||
defer observeAction("InstanceUploadToCWDFromReader", time.Now())
|
||||
|
||||
var cwd string
|
||||
var err error
|
||||
if cwd, err = p.getInstanceCWD(instance); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = p.InstanceUploadFromReader(instance, fileName, cwd, reader); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *pwd) InstanceUploadFromReader(instance *types.Instance, fileName, dest string, reader io.Reader) error {
|
||||
defer observeAction("InstanceUploadFromReader", time.Now())
|
||||
|
||||
copyErr := p.docker.CopyToContainer(instance.Name, dest, fileName, reader)
|
||||
var finalDest string
|
||||
if filepath.IsAbs(dest) {
|
||||
finalDest = dest
|
||||
} else {
|
||||
if cwd, err := p.getInstanceCWD(instance); err != nil {
|
||||
return err
|
||||
} else {
|
||||
finalDest = fmt.Sprintf("%s/%s", cwd, dest)
|
||||
}
|
||||
}
|
||||
|
||||
copyErr := p.docker.CopyToContainer(instance.Name, finalDest, fileName, reader)
|
||||
|
||||
if copyErr != nil {
|
||||
return fmt.Errorf("Error while uploading file [%s]. Error: %s\n", fileName, copyErr)
|
||||
|
||||
@@ -60,9 +60,8 @@ type PWDApi interface {
|
||||
InstanceNew(session *types.Session, conf InstanceConfig) (*types.Instance, error)
|
||||
InstanceResizeTerminal(instance *types.Instance, cols, rows uint) error
|
||||
InstanceAttachTerminal(instance *types.Instance) error
|
||||
InstanceUploadFromUrl(instance *types.Instance, url string) error
|
||||
InstanceUploadFromReader(instance *types.Instance, filename, dest string, reader io.Reader) error
|
||||
InstanceUploadToCWDFromReader(instance *types.Instance, fileName string, reader io.Reader) error
|
||||
InstanceUploadFromUrl(instance *types.Instance, fileName, dest, url string) error
|
||||
InstanceUploadFromReader(instance *types.Instance, fileName, dest string, reader io.Reader) error
|
||||
InstanceGet(session *types.Session, name string) *types.Instance
|
||||
InstanceFindByIP(ip string) *types.Instance
|
||||
InstanceFindByAlias(sessionPrefix, alias string) *types.Instance
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"log"
|
||||
"math"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -152,13 +153,15 @@ func (p *pwd) SessionDeployStack(s *types.Session) error {
|
||||
log.Printf("Error creating instance for stack [%s]: %s\n", s.Stack, err)
|
||||
return err
|
||||
}
|
||||
err = p.InstanceUploadFromUrl(i, s.Stack)
|
||||
|
||||
_, fileName := filepath.Split(s.Stack)
|
||||
err = p.InstanceUploadFromUrl(i, fileName, "", s.Stack)
|
||||
if err != nil {
|
||||
log.Printf("Error uploading stack file [%s]: %s\n", s.Stack, err)
|
||||
return err
|
||||
}
|
||||
|
||||
fileName := path.Base(s.Stack)
|
||||
fileName = path.Base(s.Stack)
|
||||
file := fmt.Sprintf("/var/run/pwd/uploads/%s", fileName)
|
||||
cmd := fmt.Sprintf("docker swarm init --advertise-addr eth0 && docker-compose -f %s pull && docker stack deploy -c %s %s", file, file, s.StackName)
|
||||
|
||||
|
||||
@@ -35,7 +35,7 @@
|
||||
$scope.uploadFiles = function (files) {
|
||||
if (files && files.length) {
|
||||
for (var i = 0; i < files.length; i++) {
|
||||
Upload.upload({url: '/sessions/' + $scope.sessionId + '/instances/' + $scope.selectedInstance.name + '/uploads?relative=true', data: {file: files[i]}, method: 'POST'});
|
||||
Upload.upload({url: '/sessions/' + $scope.sessionId + '/instances/' + $scope.selectedInstance.name + '/uploads', data: {file: files[i]}, method: 'POST'});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user