You are here

function s3_create_archive in Filebrowser 7.4

Same name and namespace in other branches
  1. 7.3 modules/s3_fb.module \s3_create_archive()

We cannot use regular PHP file functions to create the archive files so we use AWS SDK

Parameters

$target:

$node:

$fid:

$files_fid:

Return value

null|string

1 call to s3_create_archive()
filebrowser_page_download in ./filebrowser.pages.inc
Callback for filebrowser_download/%node menu.

File

modules/s3_fb.module, line 143

Code

function s3_create_archive($target, $node, $fid, $files_fid) {

  //debug($target, 'target');
  $client = awssdk_get_client('s3');
  $client
    ->registerStreamWrapper();
  $zip = new ZipArchive();
  $target = file_directory_temp() . "/filebrowser_" . _filebrowser_safe_basename($target) . ".zip";

  //debug($target, 'target');
  $cleanup = $target;
  if (!file_exists(_filebrowser_safe_dirname($target))) {
    mkdir(_filebrowser_safe_dirname($target), 0777, TRUE);
  }
  if (file_exists($target)) {
    unlink($target);
  }
  _filebrowser_load_files($node, $fid);
  if ($zip
    ->open($target, ZIPARCHIVE::CREATE) === TRUE) {

    //debug($target, 'The zip file');
    foreach ($node->file_listing as $file_name => $file_data) {
      if ($file_data['kind'] === 0 && (!$files_fid || in_array($file_data['fid'], $files_fid))) {
        $fs_filename = _filebrowser_encoding_to_fs($node, _filebrowser_get_node_root($node) . $file_data['relative-path']);
        try {
          $result = $client
            ->getObject(s3_object_info($fs_filename));
          $body = $result
            ->get('Body');
          $body
            ->rewind();
          $content = $body
            ->read($result['ContentLength']);
        } catch (Aws\S3\Exception\S3Exception $e) {

          // todo: wrap in drupal message
          echo "Request failed.<br />";
        }
        $zip
          ->addFromString($file_name, $content);
      }
    }
    $zip
      ->close();
  }
  else {
    return t("Unable to create temporary zip file '@file'", array(
      '@file' => $target,
    ));
  }
  return $target;
}