Browse Source

Fix to auto download of remote file on TripalImporter class

Stephen Ficklin 8 years ago
parent
commit
5d2b55d9b1

+ 5 - 0
tripal/api/tripal.importer.api.inc

@@ -90,6 +90,7 @@ function tripal_load_include_importer_class($class) {
  */
 function tripal_run_importer($import_id, TripalJob $job = NULL) {
 
+  $loader = NULL;
   try {
     // begin the transaction
     $transaction = db_transaction();
@@ -101,6 +102,7 @@ function tripal_run_importer($import_id, TripalJob $job = NULL) {
     $loader->setJob($job);
     $loader->prepareFile();
     $loader->run();
+    $loader->cleanFile();
 
     if ($job) {
       $job->logMessage("Done");
@@ -123,6 +125,9 @@ function tripal_run_importer($import_id, TripalJob $job = NULL) {
     if ($job) {
       $job->logMessage($e->getMessage(), array(), TRIPAL_ERROR);
     }
+    if ($loader) {
+      $loader->cleanFile();
+    }
   }
 }
 

+ 30 - 5
tripal/includes/TripalImporter.inc

@@ -383,21 +383,25 @@ class TripalImporter {
 
     try {
       if (!empty($this->arguments['file']['file_remote'])) {
+        $file_remote = $this->arguments['file']['file_remote'];
+        $this->logMessage('Download file: !file_remote...', array('!file_remote' => $file_remote));
+
         // Create a temporary file.
         $temp = tempnam("temporary://", 'import_');
-        $url_fh = fopen($this->arguments['remote_file'], "r");
+        $url_fh = fopen($file_remote, "r");
         $tmp_fh = fopen($temp, "w");
         if (!$url_fh) {
-          throw new Exception(t("Unable to download the remote file at !url. Could a firewall be blocking outgoing connections?",
-              array('!url', $details->arguments['file_url'])));
+          throw new Exception(t("Unable to download the remote file at %url. Could a firewall be blocking outgoing connections?",
+              array('%url', $file_remote)));
         }
+
         // Write the contents of the remote file to the temp file.
         while (!feof($url_fh)) {
           fwrite($tmp_fh, fread($url_fh, 255), 255);
         }
-        // Set the path to the file for the importer to use.
-        $details->arguments['file_path'] = $temp;
 
+        // Set the path to the file for the importer to use.
+        $this->arguments['file']['file_path'] = $temp;
         $this->is_prepared = TRUE;
       }
     }
@@ -406,6 +410,27 @@ class TripalImporter {
     }
   }
 
+  /**
+   * Cleans up any temporary files that were created by the prepareFile().
+   *
+   * This function should be called after a run() to remove any temporary
+   * files and keep them from building up on the server.
+   */
+  public function cleanFile() {
+    try {
+      // If a remote file was downloaded then remove it.
+      if (!empty($this->arguments['file']['file_remote']) and
+          file_exists($this->arguments['file']['file_path'])) {
+        $this->logMessage('Removing downloaded file...');
+        unlink($this->arguments['file']['file_path']);
+        $this->is_prepared = FALSE;
+      }
+    }
+    catch (Exception $e){
+      throw new Exception('Cannot prepare the importer: ' .  $e->getMessage());
+    }
+  }
+
   // --------------------------------------------------------------------------
   //                     OVERRIDEABLE FUNCTIONS
   // --------------------------------------------------------------------------

+ 3 - 2
tripal_chado/includes/TripalImporter/FASTAImporter.inc

@@ -547,10 +547,12 @@ class FASTAImporter extends TripalImporter {
 
     // Now that we know where the sequences are in the file we need to add them.
     $this->logMessage("Step 2: Importing sequences...");
+    $this->logMessage("Found !num_seqs sequence(s).", array('!num_seqs' => $num_seqs));
     $this->setTotalItems($num_seqs);
+    $this->setItemsHandled(0);
     for ($i = 0; $i < $num_seqs; $i++) {
       $seq = $seqs[$i];
-      //$this->logMessage("  importing: @name", array('@name' => $seq['name']));
+      //$this->logMessage("Importing !seqname.", array('!seqname' => $seq['name']));
       $this->loadFastaFeature($fh, $seq['name'], $seq['uname'], $db_id,
           $seq['accession'], $seq['subject'], $rel_type, $parent_type,
           $analysis_id, $organism_id, $cvterm, $source, $method, $re_name,
@@ -863,7 +865,6 @@ class FASTAImporter extends TripalImporter {
     while ($line = fgets($fh)) {
       $num_read += strlen($line) + 1;
       $chunk_intv_read += strlen($line) + 1;
-      $this->addItemsHandled($num_read);
       $chunk .= trim($line);
 
       // If we've read in enough of the sequence then append it to the database.