From 77da01d1eea32985cc87a85ce30225265b460519 Mon Sep 17 00:00:00 2001 From: ibuler Date: Wed, 2 Dec 2015 15:41:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=B8=8A=E4=BC=A0=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- connect.py | 6 ++++- jumpserver/views.py | 33 ++++++++++++++++++++++-- templates/download.html | 57 ++++++++++++++++++++++++++++++++++++----- 3 files changed, 86 insertions(+), 10 deletions(-) diff --git a/connect.py b/connect.py index a522c6446..b998bf21c 100644 --- a/connect.py +++ b/connect.py @@ -694,6 +694,10 @@ class Nav(object): logger.debug('Upload tmp dir: %s' % tmp_dir) os.chdir(tmp_dir) bash('rz') + check_notempty = os.listdir(tmp_dir) + if not check_notempty: + print color_print("上传文件为空") + continue runner = MyRunner(res) runner.run('copy', module_args='src=%s dest=%s directory_mode' % (tmp_dir, tmp_dir), pattern=pattern) @@ -742,7 +746,7 @@ class Nav(object): ret = runner.get_result() os.chdir('/tmp') tmp_dir_name = os.path.basename(tmp_dir) - bash('tar czf %s.tar.gz %s && sz %s.tar.gz' % (tmp_dir, tmp_dir_name, tmp_dir)) + bash('tar czf %s.tar.gz %s ' % (tmp_dir, tmp_dir_name)) if ret.get('failed'): print ret diff --git a/jumpserver/views.py b/jumpserver/views.py index 1e79c20e6..04e906b2d 100644 --- a/jumpserver/views.py +++ b/jumpserver/views.py @@ -293,8 +293,7 @@ def upload(request): asset_ids = request.POST.getlist('asset_ids', '') upload_files = request.FILES.getlist('file[]', None) date_now = datetime.datetime.now().strftime("%Y%m%d%H%M%S") - upload_dir = "/tmp/%s/%s" % (user.username, date_now) - mkdir(upload_dir, mode=0777) + upload_dir = get_tmp_dir() filenames = {} for asset_id in asset_ids: asset_select.append(get_object(Asset, id=asset_id)) @@ -328,6 +327,36 @@ def upload(request): @login_required(login_url='/login') def download(request): + user = request.user + assets = get_group_user_perm(user).get('asset').keys() + + asset_select = [] + if request.method == 'POST': + asset_ids = request.POST.getlist('asset_ids', '') + file_path = request.POST.get('file_path') + date_now = datetime.datetime.now().strftime("%Y%m%d%H%M%S") + upload_dir = get_tmp_dir() + for asset_id in asset_ids: + asset_select.append(get_object(Asset, id=asset_id)) + + if not set(asset_select).issubset(set(assets)): + illegal_asset = set(asset_select).issubset(set(assets)) + return HttpResponse('没有权限的服务器 %s' % ','.join([asset.hostname for asset in illegal_asset])) + res = gen_resource({'user': user, 'asset': asset_select}) + runner = MyRunner(res) + runner.run('fetch', module_args='src=%s dest=%s' % (file_path, upload_dir), pattern='*') + logger.debug(runner.get_result()) + os.chdir('/tmp') + tmp_dir_name = os.path.basename(upload_dir) + tar_file = '%s.tar.gz' % upload_dir + bash('tar czf %s %s && sz %s.tar.gz' % (tar_file, tmp_dir_name, upload_dir)) + f = open(tar_file) + data = f.read() + f.close() + response = HttpResponse(data, content_type='application/octet-stream') + response['Content-Disposition'] = 'attachment; filename=%s' % os.path.basename(tar_file) + return response + return render_to_response('download.html', locals(), context_instance=RequestContext(request)) diff --git a/templates/download.html b/templates/download.html index fb85a1ec7..0c001d8d6 100644 --- a/templates/download.html +++ b/templates/download.html @@ -1,5 +1,10 @@ {% extends 'base.html' %} {% load mytags %} +{% block self_head_css_js %} + + + +{% endblock %} {% block content %} {% include 'nav_cat_bar.html' %} @@ -22,18 +27,56 @@
-

下载文件可联系管理员在服务器安装lrzsz,使用sz命令下载。

-
- {% for document in documents %} - {{ document }} - {% endfor %} -
- +
+ {% if error %} +
{{ error }}
+ {% endif %} + {% if msg %} +
{{ msg }}
+ {% endif %} +
+ +
+ +
+
+
+
+ +
+ +
+
+
+
+ + +
+
+
+{% endblock %} +{% block self_footer_js %} + {% endblock %} \ No newline at end of file