Пример #1
0
 /**
  * Uploads large files to Dropbox in mulitple chunks
  * Note: This method is subject to change and, as such, should not be used in production
  * @param string $file Absolute path to the file to be uploaded
  * @param string|bool $filename The destination filename of the uploaded file
  * @param string $path Path to upload the file to, relative to root
  * @param boolean $overwrite Should the file be overwritten? (Default: true)
  * @return stdClass
  */
 public function chunkedUpload($file, $filename = false, $path = '', $overwrite = true)
 {
     if (file_exists($file)) {
         if ($handle = @fopen($file, 'r')) {
             // Set initial upload ID and offset
             $uploadID = null;
             $offset = 0;
             // Read from the file handle until EOF, uploading each chunk
             while ($data = fread($handle, $this->chunkSize)) {
                 $chunkHandle = fopen('php://temp', 'rw');
                 fwrite($chunkHandle, $data);
                 $this->OAuth->setInFile($chunkHandle);
                 // On subsequent chunks, use the upload ID returned by the previous request
                 if (isset($response['body']->upload_id)) {
                     $uploadID = $response['body']->upload_id;
                 }
                 $params = array('upload_id' => $uploadID, 'offset' => $offset);
                 $response = $this->fetch('PUT', self::CONTENT_URL, 'chunked_upload', $params);
                 $offset += mb_strlen($data, '8bit');
                 fclose($chunkHandle);
             }
             // Complete the chunked upload
             $filename = is_string($filename) ? $filename : basename($file);
             $call = 'commit_chunked_upload/' . $this->root . '/' . $this->encodePath($path . $filename);
             $params = array('overwrite' => (int) $overwrite, 'upload_id' => $uploadID);
             $response = $this->fetch('POST', self::CONTENT_URL, $call, $params);
             return $response;
         } else {
             throw new Exception('Could not open ' . $file . ' for reading');
         }
     }
     // Throw an Exception if the file does not exist
     throw new Exception('Local file ' . $file . ' does not exist');
 }
Пример #2
0
 /**
  * Uploads file data from a stream
  * Note: This function is experimental and requires further testing
  * @todo Add filesize check
  * @param resource $stream A readable stream created using fopen()
  * @param string $filename The destination filename, including path
  * @param boolean $overwrite Should the file be overwritten? (Default: true)
  * @return array
  */
 public function putStream($stream, $filename, $overwrite = true)
 {
     $this->OAuth->setInFile($stream);
     $path = $this->encodePath($filename);
     $call = 'files_put/' . $this->root . '/' . $path;
     $params = array('overwrite' => (int) $overwrite);
     $response = $this->fetch('PUT', self::CONTENT_URL, $call, $params);
     return $response;
 }
Пример #3
0
 /**
  * Uploads large files to Dropbox in mulitple chunks
  * @param string $file Absolute path to the file to be uploaded
  * @param string|bool $filename The destination filename of the uploaded file
  * @param string $path Path to upload the file to, relative to root
  * @param boolean $overwrite Should the file be overwritten? (Default: true)
  * @return stdClass
  */
 public function chunkedUpload($file, $filename = false, $path = '', $overwrite = true, $offset = 0, $uploadID = null)
 {
     if (file_exists($file)) {
         if ($handle = @fopen($file, 'r')) {
             // Seek to the correct position on the file pointer
             fseek($handle, $offset);
             // Read from the file handle until EOF, uploading each chunk
             while ($data = fread($handle, $this->chunkSize)) {
                 // Open a temporary file handle and write a chunk of data to it
                 $chunkHandle = fopen('php://temp', 'rw');
                 fwrite($chunkHandle, $data);
                 // Set the file, request parameters and send the request
                 $this->OAuth->setInFile($chunkHandle);
                 $params = array('upload_id' => $uploadID, 'offset' => $offset);
                 try {
                     // Attempt to upload the current chunk
                     $response = $this->fetch('PUT', self::CONTENT_URL, 'chunked_upload', $params);
                 } catch (Exception $e) {
                     $response = $this->OAuth->getLastResponse();
                     if ($response['code'] == 400) {
                         // Incorrect offset supplied, return expected offset and upload ID
                         $uploadID = $response['body']->upload_id;
                         $offset = $response['body']->offset;
                         return array('uploadID' => $uploadID, 'offset' => $offset);
                     } else {
                         // Re-throw the caught Exception
                         throw $e;
                     }
                 }
                 // On subsequent chunks, use the upload ID returned by the previous request
                 if (isset($response['body']->upload_id)) {
                     $uploadID = $response['body']->upload_id;
                 }
                 // Set the data offset
                 if (isset($response['body']->offset)) {
                     $offset = $response['body']->offset;
                 }
                 // Close the file handle for this chunk
                 fclose($chunkHandle);
             }
             // Complete the chunked upload
             $filename = is_string($filename) ? $filename : basename($file);
             $call = 'commit_chunked_upload/' . $this->root . '/' . $this->encodePath(rtrim($path, '/') . '/' . $filename);
             $params = array('overwrite' => (int) $overwrite, 'upload_id' => $uploadID);
             $response = $this->fetch('POST', self::CONTENT_URL, $call, $params);
             return $response;
         } else {
             throw new Exception('Could not open ' . $file . ' for reading');
         }
     }
     // Throw an Exception if the file does not exist
     throw new Exception('Local file ' . $file . ' does not exist');
 }
Пример #4
0
 /**
  * Uploads large files to Dropbox in mulitple chunks
  * @param string $file Absolute path to the file to be uploaded
  * @param string|bool $filename The destination filename of the uploaded file
  * @param string $path Path to upload the file to, relative to root
  * @param boolean $overwrite Should the file be overwritten? (Default: true)
  * @param integer $offset position to seek to when opening the file
  * @param string $uploadID existing upload_id to resume an upload
  * @param string|array function to call back to upon each chunk
  * @return stdClass
  */
 public function chunkedUpload($file, $filename = false, $path = '', $overwrite = true, $offset = 0, $uploadID = null, $callback = null)
 {
     if (file_exists($file)) {
         if ($handle = @fopen($file, 'r')) {
             // Set initial upload ID and offset
             if ($offset > 0) {
                 fseek($handle, $offset);
             }
             /*
                Set firstCommit to true so that the upload session start endpoint is called.
             */
             $firstCommit = true;
             // Read from the file handle until EOF, uploading each chunk
             while ($data = fread($handle, $this->chunkSize)) {
                 // Set the file, request parameters and send the request
                 $this->OAuth->setInFile($data);
                 if ($firstCommit) {
                     $params = array('close' => false, 'api_v2' => true, 'content_upload' => true);
                     $response = $this->fetch('POST', self::CONTENT_URL_V2, 'files/upload_session/start', $params);
                     $firstCommit = false;
                 } else {
                     $params = array('cursor' => array('session_id' => $uploadID, 'offset' => $offset), 'api_v2' => true, 'content_upload' => true);
                     $response = $this->append_upload($params, false);
                 }
                 // On subsequent chunks, use the upload ID returned by the previous request
                 if (isset($response['body']->session_id)) {
                     $uploadID = $response['body']->session_id;
                 }
                 /*
                    API v2 no longer returns the offset, we need to manually work this out. So check that there are no errors and update the offset as well as calling the callback method.
                 */
                 if (!isset($response['body']->error)) {
                     $offset = ftell($handle);
                     if ($callback) {
                         call_user_func($callback, $offset, $uploadID, $file);
                     }
                     $this->OAuth->setInFile(null);
                 }
             }
             // Complete the chunked upload
             $filename = is_string($filename) ? $filename : basename($file);
             $params = array('cursor' => array('session_id' => $uploadID, 'offset' => $offset), 'commit' => array('path' => '/' . $this->encodePath($path . $filename), 'mode' => 'add'), 'api_v2' => true, 'content_upload' => true);
             $response = $this->append_upload($params, true);
             return $response;
         } else {
             throw new Exception('Could not open ' . $file . ' for reading');
         }
     }
     // Throw an Exception if the file does not exist
     throw new Exception('Local file ' . $file . ' does not exist');
 }