Browse Source

Fixed bugs in quota settings. Uploader now enforced quota on file uploads

Stephen Ficklin 7 years ago
parent
commit
ca0b4d9847

+ 0 - 1
legacy/tripal_phylogeny/includes/tripal_phylogeny.admin.inc

@@ -314,7 +314,6 @@ function theme_tripal_phylogeny_admin_org_color_tables($variables){
  * @param $form_state
  */
 function tripal_phylogeny_default_plots_form_ajax_callback($form, $form_state) {
- // drupal_debug($form['tree_settings']['org_table']['num_orgs']);
 
   return $form['node_settings']['org_table'];
 }

+ 40 - 0
tripal/api/tripal.upload.api.inc

@@ -0,0 +1,40 @@
+<?php
+/**
+ * Allows a module to interact with the Tripal file uploader during upload.
+ *
+ * This function is called prior to an 'action' aoccuring and allows the
+ * module that is responsible for the file upload to halt an upload if
+ * needed.
+ *
+ * @param $action
+ *   The current action that is being peformed during the upload process. The
+ *   actions are:  'save', 'check' and 'merge'.
+ * @param $details
+ *   An associative array containing details about the upload process. Valid
+ *   keys include:
+ *     - filename:  The name of the file.
+ *     - file_size:  The total size of the file.
+ *     - chunk_size:  The size of the chunk.
+ *     - fid:  The file ID of the file.
+ * @param $message
+ *   An error message to report to the user if the function returns FALSE.
+ *
+ * @return
+ *   TRUE if the upload should continue. FALSE if a problem occurs and the
+ *   upload should be terminated.
+ */
+function hook_file_upload_check($action, $details, &$message){
+  switch ($action) {
+    case 'save':
+      // Place code here when chunks are being saved.
+      break;
+    case 'check':
+      // Place code here when a chunck is being checked if the upload
+      // completed successfully.
+      break;
+    case 'merge':
+      // Place code here when all chunks will be merged.
+      break;
+  }
+  return TRUE;
+}

+ 0 - 1
tripal/includes/tripal.entity.inc

@@ -427,7 +427,6 @@ function tripal_entity_access($op, $entity = NULL, $account = NULL, $entity_type
 
   // See if other modules want to adust permissions.
   $results = module_invoke_all($entity_type . '_access', $entity, $op, $account);
-  drupal_debug($results);
   if (in_array(TRUE, $results)) {
     return TRUE;
   }

+ 25 - 16
tripal/includes/tripal.upload.inc

@@ -3,6 +3,10 @@
 function tripal_file_upload($type, $filename, $action = NULL, $chunk = 0) {
   global $user;
 
+  $module = array_key_exists('module', $_GET) ? $_GET['module'] : '';
+  $file_size = array_key_exists('file_size', $_GET) ? $_GET['file_size'] : '';
+  $chunk_size = array_key_exists('chunk_size', $_GET) ? $_GET['chunk_size'] : '';
+
   $user_dir = 'public://tripal/users/' . $user->uid;
 
   if (!file_prepare_directory($user_dir, FILE_CREATE_DIRECTORY)) {
@@ -16,20 +20,26 @@ function tripal_file_upload($type, $filename, $action = NULL, $chunk = 0) {
     return;
   }
 
-  // Check to see if this file is uploaded already. If it is
-  // we don't want to allow the file to be uploaded again.
-//   $merge_file = $user_dir . '/' . $filename;
-//   if (file_exists($merge_file)) {
-//     $message = 'This file aready exists on the server and will not be ' .
-//       'uploaded again.',
-//     watchdog('tripal', $message, array(), WATCHDOG_ERROR);
-//     drupal_json_output(array(
-//       'status'  => 'failed',
-//       'message' => $message,
-//       'file_id' => '',
-//     ));
-//     return;
-//   }
+  // Allow the module that will own the file to make some checks. The module
+  // is allowed to stop the upload as needed.
+  $hook_name = $module . '_file_upload_check';
+  if (function_exists($hook_name)) {
+    $details = array(
+      'filename' => $filename,
+      'file_size' => $file_size,
+      'chunk_size' => $chunk_size,
+    );
+    $message = '';
+    $status = $hook_name($action, $details, $message);
+    if ($status === FALSE) {
+      drupal_json_output(array(
+        'status' => 'failed',
+        'message' => $message,
+        'file_id' => '',
+      ));
+      return;
+    }
+  }
 
   switch ($action) {
     // If the action is 'put' then the callee is sending a chunk of the file
@@ -155,7 +165,7 @@ function tripal_file_upload_merge($filename, $type, $user_dir) {
   ));
 }
 /**
- * Checks the size of a chunk to see if is fully uploded.
+ * Checks the size of a chunk to see if is fully uploaded.
  *
  * @return
  *   returns a JSON array with a status, message and the
@@ -188,7 +198,6 @@ function tripal_file_upload_check($filename, $chunk, $user_dir) {
     }
   }
 
-
   drupal_json_output(array(
     'status' => 'success',
     'message' => '',

+ 3 - 0
tripal/theme/js/TripalUploadFile.js

@@ -106,7 +106,9 @@
       $.ajax({
         url : url,
         data : {
+          'module' : this.options['module'],
           'chunk_size' : this.chunk_size,
+          'file_size' : this.file_size,
         },
         success : function(data, textStatus, jqXHR) {
           if (data['status'] == 'failed') {
@@ -140,6 +142,7 @@
           url : url,
           data : {
             'module' : this.options['module'],
+            'file_size' : this.file_size,
           },
           success : function(data, textStatus, jqXHR) {
             if (data['status'] == 'completed') {

+ 5 - 3
tripal/theme/js/TripalUploader.js

@@ -5,7 +5,7 @@
  * To use the TripalUploader Object the following must be performed:
  * 
  * 1) Add a Drupal form to your code that contains the following:
- *   * A Drupal-style table with 4 or 8 columns.  See the addUploadTable
+ *   * A Drupal-style table with 4 or 8 columns.  See the initialize
  *     function in this class for a description of the columns.
  *   * A button for submitting a file for upload.
  * 
@@ -259,7 +259,7 @@
     }
     
     /**
-     * Adds support for an upload table for a specific category.
+     * Initializes the loader for a given HTML table.
      * 
      * The TripalUploader supports two types of tables, a table for
      * uploading paired data (e.g. RNA-seq) and single files.  This function
@@ -288,7 +288,7 @@
      *     where the file ID will be written to this field. This only 
      *     works if cardinality is set to 1.
      *   allowed_types: (optional). An array of allowed file extensions (e.g.
-     *     fasta, fastq, fna, gff3, etc.). 
+     *     fasta, fastq, fna, gff3, etc.).
      */
     this.addUploadTable = function(tname, options) {
       var table_id = options['table_id'];
@@ -297,6 +297,7 @@
       var target_id = options['target_id'];
       var cardinality = options['cardinality'];
       var module = options['module'];
+      var allowed_types = options['allowed_types'];
       
       // Save the table ID for this category
       if (!(tname in this.tables)) {
@@ -308,6 +309,7 @@
       this.tables[tname]['target_id'] = target_id;
       this.tables[tname]['cardinality'] = cardinality;
       this.tables[tname]['module'] = module;
+      this.tables[tname]['allowed_types'] = allowed_types;
       this.updateTable(categories[0]);
       this.enableSubmit(submit_id);
     }

+ 13 - 11
tripal/theme/js/tripal.file.js

@@ -2,21 +2,23 @@
   Drupal.behaviors.TripalFile = {
     attach: function (context, settings) {
 
+      // Initialize the TripalUploader object.
       var tripal_files = new TripalUploader();
       
+      // All tables that belong to the html5-file form element should
+      // be enabled for uploading.
       $(".tripal-html5-file-upload-table-key").each(function(index) {
-        var id = $(this).val()
+        
+        // The settings for this uploader are provided in a custom variable
+        // specific to the table. We can get the variable name by piecing
+        // together parts of the table ID.
+        var id = $(this).val();
         var details = id.split("-");
-        var form_key = details[0] + '-' + details[1];
-        var module = details[2];
-        tripal_files.addUploadTable(form_key, {
-          'table_id' : '#tripal-html5-file-upload-table-' + id,
-          'submit_id': '#tripal-html5-file-upload-submit-' + id,
-          'category' : [form_key],
-          'cardinality' : 1,
-          'target_id' : 'tripal-html5-upload-fid-' + id,
-          'module' : module,
-        });
+        var settings_var_name = "Drupal.settings.uploader_" + details[0] + '_' + details[1] + "_" + details[2];
+        var settings = eval(settings_var_name);
+
+        // Initialize the table for uploads.
+        tripal_files.addUploadTable(details[0] + '-' + details[1], settings);
       });
     }
   }

+ 23 - 5
tripal/tripal.module

@@ -75,7 +75,6 @@ function tripal_init() {
 }
 
 function tripal_menu_alter(&$items) {
-  //drupal_debug($items);
 }
 /**
  * Implements hook_menu().
@@ -626,6 +625,7 @@ function tripal_import_api() {
   module_load_include('inc', 'tripal', 'api/tripal.jobs.api');
   module_load_include('inc', 'tripal', 'api/tripal.notice.api');
   module_load_include('inc', 'tripal', 'api/tripal.variables.api');
+  module_load_include('inc', 'tripal', 'api/tripal.upload.api');
   module_load_include('inc', 'tripal', 'api/tripal.DEPRECATED.api');
 }
 
@@ -950,11 +950,15 @@ function tripal_element_info() {
   //   - #title:  The title that will appear above the element.
   //   - #description:  The description that will appear below the element.
   //   - #usage_type:  Required.  The type of file.  This will be stored in
-  //     the 'type' column of the file_usage table.
+  //       the 'type' column of the file_usage table.
   //   - #usage_id: Required. A unique numeric ID representing an entity, node
-  //     or some other record identifier.  This can be any identifier that
-  //     makes sense to the module that implements a form that uses this
-  //     element.
+  //       or some other record identifier.  This can be any identifier that
+  //       makes sense to the module that implements a form that uses this
+  //       element.
+  //    -#usage_module: The module that will be using the file. This will be
+  //       stored in the 'module' column of the file_usage table.
+  //    -#allowed_types:  An array of file extensions that are allowed for
+  //       to be uploaded.
   $elements['html5_file'] = array(
     '#input' => 'TRUE',
     '#process' => array('tripal_html5_file_process'),
@@ -971,8 +975,10 @@ function tripal_html5_file_process($element, $form_state, $complete_form) {
 
   $module = array_key_exists('#usage_module', $element) ? $element['#usage_module'] : 'tripal';
   $type = $element['#usage_id'] . '-' . $element['#usage_type'] . '-' . $module;
+  $type_var_name = 'uploader_' . $element['#usage_id'] . '_' . $element['#usage_type'] . '_' . $module;
   $name = $element['#name'];
   $name = preg_replace('/[^\w]/', '_', $name);
+  $allowed_types = array_key_exists('#allowed_types', $element) ? $element['#allowed_types'] : array();
 
   $headers = array(
     array('data' => 'File'),
@@ -1023,6 +1029,18 @@ function tripal_html5_file_process($element, $form_state, $complete_form) {
     )
   );
 
+  $uploader_settings = array(
+    'table_id' => '#tripal-html5-file-upload-table-' . $type,
+    'submit_id' => '#tripal-html5-file-upload-submit-' . $type,
+    'category' => array($element['#usage_id'] . '-' . $element['#usage_type']),
+    'cardinality' => 1,
+    'target_id' => 'tripal-html5-upload-fid-' . $type,
+    'module' => $module,
+    'allowed_types' => $allowed_types,
+  );
+
+
+  drupal_add_js(array($type_var_name => $uploader_settings), 'setting');
   drupal_add_js(drupal_get_path ('module', 'tripal') . '/theme/js/TripalUploader.js');
   drupal_add_js(drupal_get_path ('module', 'tripal') . '/theme/js/TripalUploadFile.js');
   drupal_add_js(drupal_get_path ('module', 'tripal') . '/theme/js/tripal.file.js');

+ 0 - 1
tripal_chado/includes/tripal_chado.install.inc

@@ -126,7 +126,6 @@ function tripal_chado_load_form($form, $form_state) {
  * @param $form_state
  */
 function tripal_chado_load_form_ajax_callback($form, $form_state) {
-  // drupal_debug($form['tree_settings']['org_table']['num_orgs']);
 
   return $form;
 }