You are here

function zoomapi_save_file in Zoom API 7.2

Save a file into the database after validating it.

1 call to zoomapi_save_file()
zoomapi_api_download_recording in ./zoomapi.api.inc
Download Recording.

File

./zoomapi.module, line 1273
Main file for the Zoom API module.

Code

function zoomapi_save_file($filepath, $destination_directory, $replace = FILE_EXISTS_REPLACE) {

  // Begin building file object.
  $file = new stdClass();
  $file->status = 1;
  $file->display = 1;
  $file->filename = trim(basename($filepath), '.');
  $file->uri = $filepath;
  $file->filemime = file_get_mimetype($file->filename);
  $file->filesize = filesize($filepath);
  $extensions = 'mp4 mpa';

  // Munge the filename to protect against possible malicious extension hiding
  // within an unknown file type (ie: filename.html.foo).
  $file->filename = file_munge_filename($file->filename, $extensions);

  // Assert that the destination contains a valid stream.
  $destination_scheme = file_uri_scheme($destination_directory);
  if (!$destination_scheme || !file_stream_wrapper_valid_scheme($destination_scheme)) {
    watchdog(__FUNCTION__, 'The recording could not be saved, because the destination %destination is invalid.', [
      '%destination' => $destination_directory,
    ]);
    return FALSE;
  }

  // A URI may already have a trailing slash or look like "public://".
  if (substr($destination_directory, -1) != '/') {
    $destination_directory .= '/';
  }

  // Ensure the destination is writable.
  file_prepare_directory($destination_directory, FILE_CREATE_DIRECTORY);
  $file->destination = file_destination($destination_directory . $file->filename, $replace);

  // Move uploaded files from PHP's upload_tmp_dir to Drupal's temporary
  // directory. This overcomes open_basedir restrictions for future file
  // operations.
  $file->uri = $file->destination;
  if (!file_unmanaged_copy($filepath, $file->uri, $replace)) {
    watchdog(__FUNCTION__, 'Recording error. Could not move recording file %file to destination %destination.', [
      '%file' => $file->filename,
      '%destination' => $file->uri,
    ]);
    return FALSE;
  }

  // Set the permissions on the new file.
  drupal_chmod($file->uri);

  // If we are replacing an existing file re-use its database record.
  if ($replace == FILE_EXISTS_REPLACE) {
    $existing_files = file_load_multiple(array(), [
      'uri' => $file->uri,
    ]);
    if (count($existing_files)) {
      $existing = reset($existing_files);
      $file->fid = $existing->fid;
    }
  }

  // If we made it this far it's safe to record this file in the database.
  if ($file = file_save($file)) {
    return $file;
  }
  return FALSE;
}