Allow to drag & drop file uploads to instances

Upload file to relative session dir from terminal
This commit is contained in:
Jonathan Leibiusky @xetorthio
2017-07-07 18:00:11 -03:00
committed by Marcos Lilljedahl
parent 2053ecd7f9
commit e6b089f9be
7 changed files with 73 additions and 11 deletions

View File

@@ -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

View File

@@ -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)

View File

@@ -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 $(</var/run/cwd)`}, b); c > 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)

View File

@@ -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

View File

@@ -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() {

View File

@@ -62,3 +62,7 @@ md-input-container .md-errors-spacer {
.md-mini {
min-width: 24px;
}
.dragover {
opacity: 0.5;
}

View File

@@ -72,7 +72,7 @@
<div flex></div>
</md-content>
<md-content flex layout="column" ng-repeat="instance in instances" ng-show="instance.name == selectedInstance.name">
<md-content flex layout="column" ng-repeat="instance in instances" ng-show="instance.name == selectedInstance.name" ngf-drop="uploadFiles($files)" class="drop-box" ngf-drag-over-class="'dragover'" ngf-multiple="true">
<md-card class="stats" md-theme="default" md-theme-watch>
<md-card-title>
<md-card-title-text>
@@ -272,6 +272,9 @@
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-messages.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angular_material/1.1.0/angular-material.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/danialfarid-angular-file-upload/12.2.13/ng-file-upload-all.min.js" integrity="sha256-LrZq3efIkFX0BooX7x/rjWyYDvMKfFV2HJpy6HBw7cE=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.7.3/socket.io.js"></script>
<script src="/assets/app.js"></script>
<script src="/assets/xterm.js"></script>