Browse Source

HUE-6099 [fb] Prompt for a filename when compressing as batch job

krish 8 years ago
parent
commit
4b8abb8

+ 1 - 1
apps/filebrowser/src/filebrowser/templates/listdir.mako

@@ -100,7 +100,7 @@ ${ fb_components.menubar() }
                 </a>
               </li>
               % if ENABLE_EXTRACT_UPLOADED_ARCHIVE.get():
-                <li><a href="javascript: void(0)" title="${_('Compress selection into a single archive')}" data-bind="click: confirmCompressFiles, visible: !isS3() && (selectedFiles().length > 1 || !(selectedFiles().length===1 && isArchive()))">
+                <li><a href="javascript: void(0)" title="${_('Compress selection into a single archive')}" data-bind="click: function() { setCompressArchiveDefault(); confirmCompressFiles();}, visible: showCompressButton">
                   <i class="fa fa-fw fa-file-archive-o"></i> ${_('Compress')}</a>
                 </li>
                 <li><a href="javascript: void(0)" title="${_('Extract selected archive')}" data-bind="visible: selectedFiles().length == 1 && isArchive() && !isS3(), click: confirmExtractArchive">

+ 70 - 6
apps/filebrowser/src/filebrowser/templates/listdir_components.mako

@@ -128,15 +128,42 @@ from filebrowser.conf import ENABLE_EXTRACT_UPLOADED_ARCHIVE
     </div>
     <!-- ko if: selectedFiles -->
     <div class="modal-body">
-      <p>${_('Start a task to compress the selected file(s).')}</p>
-      <ul data-bind="foreach: selectedFiles()">
+      <div class="control-group">
+        <label class="control-label">${_('Archive Name')} </label>
+        <div class="controls">
+          <input data-bind="textInput: compressArchiveName" type="text" class="input-large">
+        </div>
+        <p>${_('Start a task to compress the selected file(s).')}</p>
+        <ul data-bind="foreach: selectedFiles()">
+          <li>
+            <span data-bind="text: $data.name"> </span>
+          </li>
+        </ul>
+      </div>
+    </div>
+    <div class="modal-footer">
+      <a class="btn" data-dismiss="modal">${_('No')}</a>
+      <input type="submit" value="${_('Yes')}" class="btn btn-primary" data-bind="click: archiveOverrideWarning, enable: compressArchiveName().length > 0"/>
+    </div>
+    <!-- /ko -->
+  </div>
+
+  <!-- compress warning modal -->
+  <div id="compressWarningModal" class="modal hide fade">
+    <div class="modal-header">
+      <button type="button" class="close" data-dismiss="modal" aria-label="${ _('Close') }"><span aria-hidden="true">&times;</span></button>
+      <h2 class="modal-title">${ _('Are you sure you want to override the existing archive?') }</h2>
+    </div>
+    <!-- ko if: compressArchiveName -->
+    <div class="modal-body">
+      <ul>
         <li>
-          <span data-bind="text: $data.name"> </span>
+          <span data-bind="text: compressArchiveName"> </span>
         </li>
       </ul>
     </div>
     <div class="modal-footer">
-      <a class="btn" data-dismiss="modal">${_('No')}</a>
+      <a class="btn" data-dismiss="modal" data-bind="click: confirmCompressFiles">${_('No')}</a>
       <input type="submit" value="${_('Yes')}" class="btn btn-primary" data-bind="click: compressSelectedFiles"/>
     </div>
     <!-- /ko -->
@@ -589,7 +616,7 @@ from filebrowser.conf import ENABLE_EXTRACT_UPLOADED_ARCHIVE
       </a>
     </li>
     % if ENABLE_EXTRACT_UPLOADED_ARCHIVE.get():
-      <li><a href="javascript: void(0)" title="${_('Compress selection into a single archive')}" data-bind="click: confirmCompressFiles, visible: !isS3() && (selectedFiles().length > 1 || !(selectedFiles().length===1 && isArchive()))">
+      <li><a href="javascript: void(0)" title="${_('Compress selection into a single archive')}" data-bind="click: function() { setCompressArchiveDefault(); confirmCompressFiles();}, visible: showCompressButton">
         <i class="fa fa-fw fa-file-archive-o"></i> ${_('Compress')}</a>
       </li>
       <li><a href="javascript: void(0)" title="${_('Extract selected archive')}" data-bind="visible: selectedFiles().length == 1 && isArchive() && !isS3(), click: confirmExtractArchive">
@@ -1082,6 +1109,8 @@ from filebrowser.conf import ENABLE_EXTRACT_UPLOADED_ARCHIVE
 
       self.allSelected = ko.observable(false);
 
+      self.compressArchiveName = ko.observable('');
+
       self.selectedFiles = ko.computed(function () {
         return ko.utils.arrayFilter(self.files(), function (file) {
           return file.selected();
@@ -1696,6 +1725,29 @@ from filebrowser.conf import ENABLE_EXTRACT_UPLOADED_ARCHIVE
         });
       };
 
+      self.showCompressButton = ko.computed(function() {
+        var fileNames = self.selectedFiles().map(function(file) {
+          return file.name;
+        });
+        if (fileNames.indexOf('.') !== -1) {
+          return false;
+        }
+        return !self.isS3() && (self.selectedFiles().length > 1 || !(self.selectedFiles().length === 1 && self.isArchive()));
+      });
+
+      self.setCompressArchiveDefault = function() {
+        if (self.selectedFiles().length == 1) {
+          self.compressArchiveName(self.selectedFile().name + '.zip');
+        } else {
+          if (self.breadcrumbs().length === 1) {
+            self.compressArchiveName('root.zip'); // When compressing multiple files in root directory
+          }
+          else {
+            self.compressArchiveName(self.breadcrumbs()[self.breadcrumbs().length - 1].label + '.zip'); // Setting to parent directory name
+          }
+        }
+      };
+
       self.confirmCompressFiles = function() {
         $("#confirmCompressModal").modal({
           keyboard:true,
@@ -1720,9 +1772,20 @@ from filebrowser.conf import ENABLE_EXTRACT_UPLOADED_ARCHIVE
         });
       };
 
-      self.compressSelectedFiles = function() {
+      self.archiveOverrideWarning = function() {
         $("#confirmCompressModal").modal("hide");
+        var fileNames = self.files().map(function(file) {
+          return file.name;
+        });
+        if (fileNames.indexOf(self.compressArchiveName()) !== -1) {
+          $("#compressWarningModal").modal("show");
+        } else {
+          self.compressSelectedFiles();
+        }
+      }
 
+      self.compressSelectedFiles = function() {
+        $("#compressWarningModal").modal("hide");
         var fileNames = [];
         $(self.selectedFiles()).each(function (index, file) {
           fileNames.push(file.name);
@@ -1731,6 +1794,7 @@ from filebrowser.conf import ENABLE_EXTRACT_UPLOADED_ARCHIVE
         $.post("/filebrowser/compress_files", {
           "files": fileNames,
           "upload_path": self.currentPath(),
+          "archive_name": self.compressArchiveName()
         }, function (data) {
           if (data.status == 0) {
             $.jHueNotify.info("${ _('Task ') }" + data.history_uuid + "${_(' submitted.') }");

+ 5 - 2
apps/filebrowser/src/filebrowser/views.py

@@ -1390,13 +1390,16 @@ def compress_files_using_batch_job(request):
   response = {'status': -1, 'data': ''}
   if ENABLE_EXTRACT_UPLOADED_ARCHIVE.get():
     upload_path = request.POST.get('upload_path', None)
+    archive_name = request.POST.get('archive_name', None)
     file_names = request.POST.getlist('files[]')
 
-    if upload_path and file_names:
+    if upload_path and file_names and archive_name:
       try:
-        response = compress_files_in_hdfs(request, file_names, upload_path)
+        response = compress_files_in_hdfs(request, file_names, upload_path, archive_name)
       except Exception, e:
         response['message'] = _('Exception occurred while compressing files: %s' % e)
+    else:
+      response['message'] = _('Error: Output directory is not set.');
   else:
     response['message'] = _('ERROR: Configuration parameter enable_extract_uploaded_archive ' +
                             'has to be enabled before calling this method.')

+ 1 - 1
apps/filebrowser/src/filebrowser/views_test.py

@@ -960,7 +960,7 @@ alert("XSS")
     self.cluster.fs.chmod(test_dir2, 0700)
 
     try:
-      resp = self.c.post('/filebrowser/compress_files', {'upload_path': prefix, 'files[]': ['test_dir1','test_dir2']})
+      resp = self.c.post('/filebrowser/compress_files', {'upload_path': prefix, 'files[]': ['test_dir1','test_dir2'], 'archive_name': 'test_compress.zip'})
       response = json.loads(resp.content)
       assert_equal(0, response['status'], response)
       assert_true('handle' in response and response['handle']['id'], response)

+ 11 - 11
desktop/core/src/desktop/lib/tasks/compress_files/compress_in_hdfs.sh

@@ -17,16 +17,16 @@
 
 UPLOAD_PATH=
 FILE_NAMES=
-OUTPUT_PATH=
+ARCHIVE_NAME=
 
 function usage()
 {
-    echo "Arguments '-u' and '-f' are mandatory."
+    echo "Arguments '-u', '-f' and '-n' are mandatory."
     echo "Usage:"
     echo "\t-h --help"
     echo "\t[-u | --upload-path]=<PATH_IN_HDFS>"
     echo "\t[-f | --file-names]=<FILE_NAMES>"
-    echo "\t[-o | --output-path]=<PATH_IN_HDFS>"
+    echo "\t[-n | --archive_name]=<ARCHIVE_NAME>"
     echo ""
 }
 
@@ -44,8 +44,8 @@ while [ "$1" != "" ]; do
         -f | --file-names)
             FILE_NAMES=$VALUE
             ;;
-        -o | --output-path)
-            OUTPUT_PATH=$VALUE
+        -n | --archive-name)
+            ARCHIVE_NAME=$VALUE
             ;;
         *)
             echo "ERROR: unknown parameter \"$PARAM\""
@@ -56,7 +56,7 @@ while [ "$1" != "" ]; do
     shift
 done
 
-if [ -z $UPLOAD_PATH ] || [ -z $FILE_NAMES ] || [ -z $OUTPUT_PATH ]
+if [ -z $UPLOAD_PATH ] || [ -z $FILE_NAMES ] || [ -z $ARCHIVE_NAME ]
 then
 	echo "ERROR: Missing Arguments"
 	usage
@@ -70,20 +70,20 @@ temp_output_dir=`mktemp -d 2>/dev/null || mktemp -d -t 'mytmpdir'`
 echo 'Created temporary output directory: '$temp_output_dir
 
 set -x
-zip -r $temp_output_dir/hue_compressed.zip ${FILE_NAMES[@]}
+zip -r $temp_output_dir/$ARCHIVE_NAME ${FILE_NAMES[@]}
 exit_status=$(echo $?)
 
 set +x
 if [ $exit_status == 0 ]
 then
-	echo "Copying hue_compressed.zip to '$OUTPUT_PATH' in HDFS"
-	hadoop fs -put -f $temp_output_dir/hue_compressed.zip $OUTPUT_PATH
+	echo "Copying hue_compressed.zip to '$UPLOAD_PATH' in HDFS"
+	hadoop fs -put -f $temp_output_dir/$ARCHIVE_NAME $UPLOAD_PATH
 	exit_status=$(echo $?)
 	if [ $exit_status == 0 ]
 	then
-	    echo "Copy to HDFS directory '$OUTPUT_PATH' complete!!!"
+	    echo "Copy to HDFS directory '$UPLOAD_PATH' complete!!!"
 	else
-	    echo "ERROR: Copy to HDFS directory '$OUTPUT_PATH' FAILED!!!"
+	    echo "ERROR: Copy to HDFS directory '$UPLOAD_PATH' FAILED!!!"
 	fi
 else
 	exit_status=1

+ 3 - 5
desktop/core/src/desktop/lib/tasks/compress_files/compress_utils.py

@@ -23,24 +23,22 @@ from desktop.lib.paths import get_desktop_root
 
 from notebook.connectors.base import Notebook
 
-def compress_files_in_hdfs(request, file_names, upload_path):
+def compress_files_in_hdfs(request, file_names, upload_path, archive_name):
 
   _upload_compress_files_script_to_hdfs(request.fs)
 
-  output_path = upload_path
-
   files = [{"value": upload_path + '/' + file_name} for file_name in file_names]
   files.append({'value': '/user/' + DEFAULT_USER.get() + '/common/compress_files_in_hdfs.sh'})
 
   shell_notebook = Notebook(
     description=_('HDFS Compression to %(upload_path)s/hue_compressed.zip') % {'upload_path': upload_path},
     isManaged=True,
-    onSuccessUrl=reverse('filebrowser.views.view', kwargs={'path': output_path})
+    onSuccessUrl=reverse('filebrowser.views.view', kwargs={'path': upload_path})
   )
 
   shell_notebook.add_shell_snippet(
       shell_command='compress_files_in_hdfs.sh',
-      arguments=[{'value': '-u=' + upload_path}, {'value': '-f=' + ','.join(file_names)}, {'value': '-o=' + output_path}],
+      arguments=[{'value': '-u=' + upload_path}, {'value': '-f=' + ','.join(file_names)}, {'value': '-n=' + archive_name}],
       archives=[],
       files=files,
       env_var=[{'value': 'HADOOP_USER_NAME=${wf:user()}'}]