Example #1
0
 /**
  * Returns a {@link WriteMode} for forcing a file to be at a certain path.  If there's already
  * a file at that path, the existing file will be overwritten.  If there's a folder at that
  * path, however, it will not be overwritten and the API call will fail.
  *
  * @return Dropbox_WriteMode
  */
 public static function force()
 {
     if (self::$forceInstance === null) {
         self::$forceInstance = new Dropbox_WriteMode(array("overwrite" => "true"));
     }
     return self::$forceInstance;
 }
Example #2
0
 /**
  * Uploads backup file from server to Dropbox.
  *
  * @param array $args arguments passed to the function
  *                    [consumer_key] -> consumer key of ManageWP Dropbox application
  *                    [consumer_secret] -> consumer secret of ManageWP Dropbox application
  *                    [oauth_token] -> oauth token of user on ManageWP Dropbox application
  *                    [oauth_token_secret] -> oauth token secret of user on ManageWP Dropbox application
  *                    [dropbox_destination] -> folder on user's Dropbox account which backup file should be upload to
  *                    [dropbox_site_folder] -> subfolder with site name in dropbox_destination which backup file should be upload to
  *                    [backup_file] -> absolute path of backup file on local server
  *
  * @return bool|array true is successful, array with error message if not
  */
 public function dropbox_backup($args)
 {
     mwp_logger()->info('Acquiring Dropbox token to start uploading the backup file');
     try {
         $dropbox = mwp_dropbox_oauth_factory($args['consumer_key'], $args['consumer_secret'], $args['oauth_token'], $args['oauth_token_secret']);
     } catch (Exception $e) {
         mwp_logger()->error('Error while acquiring Dropbox token', array('exception' => $e));
         return array('error' => $e->getMessage(), 'partial' => 1);
     }
     $args['dropbox_destination'] = '/' . ltrim($args['dropbox_destination'], '/');
     if ($args['dropbox_site_folder'] == true) {
         $args['dropbox_destination'] .= '/' . $this->site_name . '/' . basename($args['backup_file']);
     } else {
         $args['dropbox_destination'] .= '/' . basename($args['backup_file']);
     }
     $fileSize = filesize($args['backup_file']);
     $start = microtime(true);
     try {
         mwp_logger()->info('Uploading backup file to Dropbox; file size is {backup_size} (progress support: {progress_support})', array('backup_file' => $args['backup_file'], 'backup_size' => mwp_format_bytes($fileSize), 'directory' => $args['dropbox_destination'], 'progress_support' => version_compare(PHP_VERSION, '5.3', '>=') ? 'enabled' : 'disabled'));
         $callback = null;
         if (version_compare(PHP_VERSION, '5.3', '>=')) {
             $progress = new MWP_Progress_Upload($fileSize, 3, mwp_logger());
             $callback = $progress->getCallback();
         }
         $dropbox->uploadFile($args['dropbox_destination'], Dropbox_WriteMode::force(), fopen($args['backup_file'], 'r'), $fileSize, $callback);
     } catch (Exception $e) {
         mwp_logger()->error('Error while uploading the file to Dropbox', array('exception' => $e));
         return array('error' => $e->getMessage(), 'partial' => 1);
     }
     mwp_logger()->info('Backup to Dropbox completed; average speed is {speed}/s', array('speed' => mwp_format_bytes($fileSize / (microtime(true) - $start))));
     return true;
 }
Example #3
0
 /**
  * Creates a file on Dropbox using the accumulated contents of the given chunked upload session.
  *
  * See <a href="https://www.dropbox.com/developers/core/docs#commit-chunked-upload">/commit_chunked_upload</a>.
  *
  * @param string $uploadId
  *                         The unique identifier for the chunked upload session.  This is obtained via
  *                         {@link chunkedUploadStart}.
  *
  * @param string $path
  *                     The Dropbox path to save the file to ($path).
  *
  * @param Dropbox_WriteMode $writeMode
  *                                     What to do if there's already a file at the given path.
  *
  * @return array|null
  *                    If <code>null</code>, it means the Dropbox server wasn't aware of the
  *                    <code>$uploadId</code> you gave it.
  *                    Otherwise, you get back the
  *                    <a href="https://www.dropbox.com/developers/core/docs#metadata-details">metadata object</a>
  *                    for the newly-created file.
  *
  * @throws Dropbox_Exception
  */
 public function chunkedUploadFinish($uploadId, $path, $writeMode)
 {
     Dropbox_Checker::argStringNonEmpty("uploadId", $uploadId);
     Dropbox_Path::checkArgNonRoot("path", $path);
     Dropbox_WriteMode::checkArg("writeMode", $writeMode);
     $params = array_merge(array("upload_id" => $uploadId), $writeMode->getExtraParams());
     $response = $this->doPost($this->contentHost, $this->appendFilePath("1/commit_chunked_upload", $path), $params);
     if ($response->statusCode === 404) {
         return null;
     }
     if ($response->statusCode !== 200) {
         throw Dropbox_RequestUtil::unexpectedStatus($response);
     }
     return Dropbox_RequestUtil::parseResponseJson($response->body);
 }