diff --git a/.profile b/.profile index ac9d1f9..75ea98d 100644 --- a/.profile +++ b/.profile @@ -2,3 +2,4 @@ export PS1='\e[1m\e[31m[\h] \e[32m($(docker-prompt)) \e[34m\u@$(hostname -i)\e[3 alias vi='vim' export PATH=$PATH:/root/go/bin cat /etc/motd +echo $BASHPID > /var/run/cwd diff --git a/handlers/file_upload.go b/handlers/file_upload.go index f261d9e..0420241 100644 --- a/handlers/file_upload.go +++ b/handlers/file_upload.go @@ -35,6 +35,8 @@ func FileUpload(rw http.ResponseWriter, req *http.Request) { rw.WriteHeader(http.StatusBadRequest) return } + r := req.URL.Query().Get("relative") + for { p, err := red.NextPart() if err == io.EOF { @@ -48,12 +50,23 @@ func FileUpload(rw http.ResponseWriter, req *http.Request) { if p.FileName() == "" { continue } - err = core.InstanceUploadFromReader(i, p.FileName(), p) - if err != nil { - log.Println(err) - rw.WriteHeader(http.StatusInternalServerError) - return + + 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 + } } + log.Printf("Uploaded [%s] to [%s]\n", p.FileName(), i.Name) } rw.WriteHeader(http.StatusOK) diff --git a/pwd/instance.go b/pwd/instance.go index 5af7544..c160628 100644 --- a/pwd/instance.go +++ b/pwd/instance.go @@ -1,6 +1,7 @@ package pwd import ( + "bytes" "fmt" "io" "log" @@ -82,10 +83,41 @@ func (p *pwd) InstanceUploadFromUrl(instance *types.Instance, url string) error return nil } -func (p *pwd) InstanceUploadFromReader(instance *types.Instance, fileName string, reader io.Reader) error { +func (p *pwd) getInstanceCWD(instance *types.Instance) (string, error) { + b := bytes.NewBufferString("") + + if c, err := p.docker.ExecAttach(instance.Name, []string{"bash", "-c", `pwdx $( 0 { + log.Println(b.String()) + return "", fmt.Errorf("Error %d trying to get CWD", c) + } else if err != nil { + return "", err + } + + cwd := strings.TrimSpace(strings.Split(b.String(), ":")[1]) + + 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, "/var/run/pwd/uploads", fileName, reader) + copyErr := p.docker.CopyToContainer(instance.Name, dest, fileName, reader) if copyErr != nil { return fmt.Errorf("Error while uploading file [%s]. Error: %s\n", fileName, copyErr) diff --git a/pwd/pwd.go b/pwd/pwd.go index 1dcc8fa..2aa1e7b 100644 --- a/pwd/pwd.go +++ b/pwd/pwd.go @@ -61,7 +61,8 @@ type PWDApi interface { 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 string, reader io.Reader) error + InstanceUploadFromReader(instance *types.Instance, filename, dest string, reader io.Reader) error + InstanceUploadToCWDFromReader(instance *types.Instance, fileName string, reader io.Reader) error InstanceGet(session *types.Session, name string) *types.Instance InstanceFindByIP(ip string) *types.Instance InstanceFindByAlias(sessionPrefix, alias string) *types.Instance diff --git a/www/assets/app.js b/www/assets/app.js index 75d735b..ef34dc8 100644 --- a/www/assets/app.js +++ b/www/assets/app.js @@ -1,7 +1,7 @@ (function() { 'use strict'; - var app = angular.module('DockerPlay', ['ngMaterial']); + var app = angular.module('DockerPlay', ['ngMaterial', 'ngFileUpload']); // Automatically redirects user to a new session when bypassing captcha. // Controller keeps code/logic separate from the HTML @@ -19,7 +19,7 @@ } } - app.controller('PlayController', ['$scope', '$log', '$http', '$location', '$timeout', '$mdDialog', '$window', 'TerminalService', 'KeyboardShortcutService', 'InstanceService', 'SessionService', function($scope, $log, $http, $location, $timeout, $mdDialog, $window, TerminalService, KeyboardShortcutService, InstanceService, SessionService) { + app.controller('PlayController', ['$scope', '$log', '$http', '$location', '$timeout', '$mdDialog', '$window', 'TerminalService', 'KeyboardShortcutService', 'InstanceService', 'SessionService', 'Upload', function($scope, $log, $http, $location, $timeout, $mdDialog, $window, TerminalService, KeyboardShortcutService, InstanceService, SessionService, Upload) { $scope.sessionId = SessionService.getCurrentSessionId(); $scope.instances = []; $scope.idx = {}; @@ -32,6 +32,14 @@ $scope.deleteInstanceBtnText = 'Delete'; $scope.isInstanceBeingDeleted = false; + $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'}); + } + } + } + var selectedKeyboardShortcuts = KeyboardShortcutService.getCurrentShortcuts(); angular.element($window).bind('resize', function() { diff --git a/www/assets/style.css b/www/assets/style.css index 0d429f0..9feb2d0 100644 --- a/www/assets/style.css +++ b/www/assets/style.css @@ -62,3 +62,7 @@ md-input-container .md-errors-spacer { .md-mini { min-width: 24px; } + +.dragover { + opacity: 0.5; +} diff --git a/www/index.html b/www/index.html index 7576672..0b926ed 100644 --- a/www/index.html +++ b/www/index.html @@ -72,7 +72,7 @@
- + @@ -272,6 +272,9 @@ + + +