You are here

public function BackupMigrateDropboxAPI::file_upload in Backup and Migrate Dropbox 7

Same name and namespace in other branches
  1. 7.3 backup_migrate_dropbox.dropbox_api.inc \BackupMigrateDropboxAPI::file_upload()
  2. 7.2 backup_migrate_dropbox.dropbox_api.inc \BackupMigrateDropboxAPI::file_upload()

File

./backup_migrate_dropbox.dropbox_api.inc, line 19
backup_migrate_dropbox.api.inc

Class

BackupMigrateDropboxAPI

Code

public function file_upload($file, $path) {

  // Cut PHP memory limit by 10% to allow for other in memory data.
  $php_memory_limit = intval(backup_migrate_dropbox_byte_size(ini_get('memory_limit')) * 0.9);

  // Dropbox currently has a 150M upload limit per transaction.
  $dropbox_upload_limit = backup_migrate_dropbox_byte_size('150M');

  // For testing or in case the 10% leeway isn't enough allow a smaller upload
  // limit as an advanced setting. This variable has no ui but can be set with
  // drush or through the variable module.
  $manual_upload_limit = backup_migrate_dropbox_byte_size(variable_get('backup_migrate_dropbox_upload_limit', '150M'));

  // Use the smallest value for the max file size.
  $max_file_size = min($php_memory_limit, $dropbox_upload_limit, $manual_upload_limit);

  // File.
  $file_size = filesize($file);

  // If the file size is greater than
  if ($file_size > $max_file_size) {

    // Open file.
    $file_handle = fopen($file, 'rb');
    if (!$file_handle) {
      throw new ErrorException('Cannot open backup file (1).');
    }

    // Start.
    $content = fread($file_handle, $max_file_size);
    if (!$content) {
      throw new ErrorException('Cannot read backup file (2).');
    }
    $this
      ->_file_upload_session_start($content);

    // Append.
    while (!feof($file_handle)) {

      // Get content.
      $content = fread($file_handle, $max_file_size);
      if (!$content) {
        throw new ErrorException('Cannot read backup file (3).');
      }
      $this
        ->_file_upload_session_append($content);
    }

    // Finish.
    $this
      ->_file_upload_session_finish($path);
  }
  else {
    $content = file_get_contents($file);
    if (!$content) {
      throw new ErrorException('Cannot open backup file (4).');
    }
    $this
      ->_file_upload_upload($path, $content);
  }
}