[pve-devel] r4918 - in pve-manager/pve2: lib/PVE lib/PVE/API2 www/css www/manager

svn-commits at proxmox.com svn-commits at proxmox.com
Tue Jul 20 14:23:55 CEST 2010


Author: dietmar
Date: 2010-07-20 12:23:55 +0000 (Tue, 20 Jul 2010)
New Revision: 4918

Modified:
   pve-manager/pve2/lib/PVE/API2/Storage.pm
   pve-manager/pve2/lib/PVE/REST.pm
   pve-manager/pve2/www/css/ext-pve.css
   pve-manager/pve2/www/manager/Makefile.am
   pve-manager/pve2/www/manager/StorageBrowser.js
Log:
implement file upload - first try ;-)


Modified: pve-manager/pve2/lib/PVE/API2/Storage.pm
===================================================================
--- pve-manager/pve2/lib/PVE/API2/Storage.pm	2010-07-20 06:14:10 UTC (rev 4917)
+++ pve-manager/pve2/lib/PVE/API2/Storage.pm	2010-07-20 12:23:55 UTC (rev 4918)
@@ -434,6 +434,63 @@
     }});
 
 __PACKAGE__->register_method ({
+    name => 'upload_content', 
+    # /storage/content/{nodeid}/{storeid}
+    path => 'content/{node}/{storage}',
+    method => 'POST',
+    description => "Upload content.",
+    parameters => {
+    	additionalProperties => 0,
+	properties => { 
+	    node => {
+		type => 'string',		
+	    },
+	    storage => {
+		type => 'string',		
+	    },
+	    upload => {
+		type => 'string',		
+	    },
+	},
+    },
+    returns => { type => 'null' },
+    code => sub {
+	my ($conn, $resp, $param) = @_;
+
+	my $cts = $param->{content} ? [ $param->{content} ] : [ @ctypes ];
+
+	my $node = $param->{node};
+	my $storeid = $param->{storage};
+	my $filename = $param->{upload};
+	my $fh = CGI::upload('upload') || die "unable to get file handle\n";
+
+	syslog ('info', "UPLOAD $filename to $node $storeid");
+	
+	die "upload not implemented\n";
+
+	my $buffer = "";
+	my $tmpname = "/tmp/proxmox_upload-$$.bin";
+
+	eval {
+	    open FILE, ">$tmpname" || die "can't open temporary file '$tmpname' - $!\n";
+	    while (read($fh, $buffer, 32768)) {
+		die "write failed - $!" unless print FILE $buffer;
+	    }
+	    close FILE || die " can't close temporary file '$tmpname' - $!\n";
+	};
+	my $err = $@;
+
+	if ($err) {
+	    unlink $tmpname;
+	    die $err;
+	}
+
+	unlink $tmpname; # fixme: proxy to local host import
+
+	return undef;
+    }});
+
+__PACKAGE__->register_method ({
     name => 'list_status', 
     protected => 1,
     # /storage/status/{nodeid}

Modified: pve-manager/pve2/lib/PVE/REST.pm
===================================================================
--- pve-manager/pve2/lib/PVE/REST.pm	2010-07-20 06:14:10 UTC (rev 4917)
+++ pve-manager/pve2/lib/PVE/REST.pm	2010-07-20 12:23:55 UTC (rev 4918)
@@ -116,6 +116,10 @@
     } elsif ($format eq 'extjs') {
 	$ct = 'application/json';
 	$raw = to_json($data, {utf8 => 1, allow_nonref => 1});
+    } elsif ($format eq 'htmljs') {
+	# we use this for extjs file upload forms
+	$ct = 'text/html';
+	$raw =  encode_entities(to_json($data, {utf8 => 1, allow_nonref => 1}));
     } else {
 	$ct = 'text/plain';
 	$raw = to_json($data, {utf8 => 1, allow_nonref => 1, pretty => 1});
@@ -136,7 +140,7 @@
 	$new->{errors} = $res->{errors};
     };
 
-    if ($format eq 'extjs') {
+    if ($format eq 'extjs' || $format eq 'htmljs') {
 	# HACK: extjs wants 'success' property instead of useful HTTP status codes
 	if (is_error($res->{status})) {
 	    $success = 0;
@@ -274,7 +278,7 @@
 
     return { status => HTTP_UNAUTHORIZED } if !($ticket && $username);
 
-    syslog ('info', "GOT VALID TICKET $ticket");
+    syslog ('info', "GOT $method $abs_uri VALID TICKET $ticket");
     
     my $stack = [ grep { length($_) > 0 }  split('\/+' , $rel_uri)] ; # skip empty fragments
     my $uri_param = {};
@@ -321,7 +325,7 @@
 sub split_abs_uri {
     my ($abs_uri) = @_;
 
-    my ($format, $rel_uri) = $abs_uri =~ m/^\Q$baseuri\E\/+(html|json|extjs)(\/.*)?$/;
+    my ($format, $rel_uri) = $abs_uri =~ m/^\Q$baseuri\E\/+(html|json|extjs|htmljs)(\/.*)?$/;
     $rel_uri = '/' if !$rel_uri;
  
     return wantarray ? ($rel_uri, $format) : $rel_uri;

Modified: pve-manager/pve2/www/css/ext-pve.css
===================================================================
--- pve-manager/pve2/www/css/ext-pve.css	2010-07-20 06:14:10 UTC (rev 4917)
+++ pve-manager/pve2/www/css/ext-pve.css	2010-07-20 12:23:55 UTC (rev 4918)
@@ -52,3 +52,38 @@
     border:0px;
     height:6px;
 }
+
+
+/*!
+ * Ext JS Library 3.2.1
+ * Copyright(c) 2006-2010 Ext JS, Inc.
+ * licensing at extjs.com
+ * http://www.extjs.com/license
+ */
+/*
+ * FileUploadField component styles
+ */
+.x-form-file-wrap {
+    position: relative;
+    height: 22px;
+}
+.x-form-file-wrap .x-form-file {
+	position: absolute;
+	right: 0;
+	-moz-opacity: 0;
+	filter:alpha(opacity: 0);
+	opacity: 0;
+	z-index: 2;
+    height: 22px;
+}
+.x-form-file-wrap .x-form-file-btn {
+	position: absolute;
+	right: 0;
+	z-index: 1;
+}
+.x-form-file-wrap .x-form-file-text {
+    position: absolute;
+    left: 0;
+    z-index: 3;
+    color: #777;
+}
\ No newline at end of file

Modified: pve-manager/pve2/www/manager/Makefile.am
===================================================================
--- pve-manager/pve2/www/manager/Makefile.am	2010-07-20 06:14:10 UTC (rev 4917)
+++ pve-manager/pve2/www/manager/Makefile.am	2010-07-20 12:23:55 UTC (rev 4918)
@@ -3,6 +3,7 @@
 JSSRC= 				                 	\
 	../ext/extjs/examples/ux/MultiSelect.js	 	\
 	../ext/extjs/examples/ux/BufferView.js 		\
+	../ext/extjs/examples/ux/fileuploadfield/FileUploadField.js \
 	PVEUtils.js 					\
 	util/base64.js					\
 	data/ObjectReader.js				\

Modified: pve-manager/pve2/www/manager/StorageBrowser.js
===================================================================
--- pve-manager/pve2/www/manager/StorageBrowser.js	2010-07-20 06:14:10 UTC (rev 4917)
+++ pve-manager/pve2/www/manager/StorageBrowser.js	2010-07-20 12:23:55 UTC (rev 4918)
@@ -27,7 +27,46 @@
 	Ext.apply(self, {
 	    stateful: false,
 	    store: store,
-	    //enableHdMenu: false,
+	    tbar: [
+		'->',
+		{
+		    xtype: 'form',
+		    url: '/api2/htmljs/storage/content/' + node + '/' + storeid,
+		    fileUpload: true,
+		    height: 22,
+		    border: false,
+		    baseCls: 'plain', 
+		    items: {
+			xtype: 'fileuploadfield',
+			name: 'upload',
+			buttonOnly: true,
+			buttonText: 'Upload',
+			listeners: {
+			    fileselected: function(field, v) {
+				self.el.mask('Please wait...', 'x-mask-loading');
+				var form = field.ownerCt.getForm();
+				//alert("selected " +  v);
+				form.submit({
+				    failure: function(f, resp){
+					self.el.unmask();
+					f.reset();
+					var msg = "Please try again";
+					if (resp.result && resp.result.message) {
+					    msg = resp.result.message;
+					} 
+					Ext.MessageBox.alert('Failure', "Upload failed. " + msg);
+				    },
+				    success: function(f, resp){
+					self.el.unmask();
+					f.reset();
+					Ext.MessageBox.alert('Failure', "Upload succesful");
+				    }
+				});
+			    }
+			}
+ 		    }
+		}
+	    ],	    
 	    colModel: new Ext.grid.ColumnModel({
 		columns: [
 		    {




More information about the pve-devel mailing list