You are here

public function filedepot_archiver::createArchive in filedepot 7

Same name in this branch
  1. 7 filedepot_archiver.class.php \filedepot_archiver::createArchive()
  2. 7 filedepot_archiver.zip.class.php \filedepot_archiver::createArchive()

Create the requested archive

File

./filedepot_archiver.class.php, line 288
filedepot_archiver.class.php Archiving class for filedepot

Class

filedepot_archiver
@file filedepot_archiver.class.php Archiving class for filedepot

Code

public function createArchive() {
  $checked_folders = array();

  /**
   * Sort the folder and file objects into checked and unchecked arrays
   *  Foreach folder, get all files inside - if any are listed in the unchecked_files array, discard
   *                  Get all folders, if any are listed in the unchecked_folders array, discard
   *                  If any are already listed, discard
   */
  foreach ($this->checkedFolderObjects as $folder_id => $folder_obj) {
    if ($folder_obj['checked'] === FALSE) {
      array_push($this->uncheckedFolderIds, $folder_obj['id']);
    }
    else {
      $checked_folders[] = $folder_obj['id'];
    }
  }
  foreach ($this->checkedFileObjects as $file_id => $file_obj) {
    if ($file_obj['checked'] === FALSE) {
      array_push($this->uncheckedFileIds, $file_obj['id']);
    }
    else {
      $this->filesToBeDownloaded[] = (int) $file_obj['id'];
    }
  }

  // @TIM fix this method
  foreach ($checked_folders as $cid) {
    $this
      ->generateAllFilesUnderCidRecursively($cid);
  }
  if (count($this->filesToBeDownloaded) > 0) {
    $this->filesToBeDownloaded = implode(',', $this->filesToBeDownloaded);
    $result = db_query("SELECT fid, fname, size, title, cid FROM {filedepot_files} WHERE fid IN ({$this->filesToBeDownloaded})");
    $file_count = 0;
    $f_array = array();
    while ($A = $result
      ->fetchAssoc()) {
      $f_array_tmp = $A['fname'] . " - s= " . $A['size'];
      if ($this
        ->hasViewPermission($A['cid']) === TRUE) {

        // Inode limit workaround [ set to 2 to introduce heavy load for profiling ]

        /* if ($file_count === 1) {
                    if ($this->saveAndReopen() !== TRUE) {
                    watchdog('filedepot', 'Failure when creating archive, could not close and reopen', array(), WATCHDOG_ERROR);
                    $f_array_tmp .= " -:save and reopen fail:  - ";
                    }
                    else {
                    $f_array_tmp .= " -:save and reopen success:- ";
                    }

                    $file_count = 0;
                    } */
        $sourcefile = $this->filedepotStoragePath . "/{$A['cid']}/{$A['fname']}";
        if (file_exists($sourcefile)) {
          $archive_path = $this
            ->getProcessedPath($A['cid']);

          // . $A['title'];
          $destination_dir = $this->archiveStorePath . $archive_path . "";

          // Make sure CID exists
          if (!file_exists($destination_dir)) {
            $cmd = "mkdir -p '{$destination_dir}'";
            shell_exec("mkdir -p '{$destination_dir}'");
            shell_exec("chmod 777 -R '{$destination_dir}'");

            //@mkdir($destination_dir, 0777, TRUE);
          }
          $this->permissionCount++;

          // copy file directory to fd directory
          $res = file_unmanaged_copy($sourcefile, $destination_dir . $A['title'], FILE_EXISTS_REPLACE);

          //$res = $this->addFile($sourcefile, $archive_path);
          $res_str = $res ? " TRUE " : "FALSE";
          $f_array_tmp .= " - file added to archive ({$res_str}): {$destination_dir}{$A['title']}";
          $file_count++;
        }
        else {
          watchdog("filedepot", "Missing file @file", array(
            '@file' => $sourcefile,
          ), WATCHDOG_WARNING);
        }
      }
      else {
        watchdog("filedepot_debug", "invalid perms for {$A['cid']}");
      }
      $f_array[] = $f_array_tmp;
    }
    watchdog("filedepot_debug", "file_download_listing <pre>" . print_r($f_array, TRUE) . "</pre>");
  }

  // create zip archive
  shell_exec("chmod 777 -R {$this->archiveStorePath}");
  $ret = shell_exec("cd '{$this->archiveStorePath}'; zip -r  {$this->zipFileName} *");

  // unlink folder
  shell_exec("chmod 777 {$this->zipFileName}");
  shell_exec("rm -rf {$this->archiveStorePath}");
  watchdog("filedepot_debug", "zip archive created at {$this->zipFileName} with return {$ret} ");
}