From 6e3cbad09976755ccb387b2cf94fb99b439cf990 Mon Sep 17 00:00:00 2001 From: Emerson Maningo Date: Thu, 7 Feb 2019 21:14:44 +0800 Subject: [PATCH 1/2] Removed unnecessary use. --- sdk/Dropbox/Files.php | 1 - 1 file changed, 1 deletion(-) diff --git a/sdk/Dropbox/Files.php b/sdk/Dropbox/Files.php index ae6b7f2..add1b59 100644 --- a/sdk/Dropbox/Files.php +++ b/sdk/Dropbox/Files.php @@ -1,7 +1,6 @@ Date: Fri, 8 Feb 2019 13:44:54 +0800 Subject: [PATCH 2/2] Bug fixes on upload sessions. --- sdk/Dropbox.php | 1 + sdk/Dropbox/Files.php | 62 +++++++++++++++++++++++++++---------------- 2 files changed, 40 insertions(+), 23 deletions(-) diff --git a/sdk/Dropbox.php b/sdk/Dropbox.php index ed2dad3..1b356b4 100644 --- a/sdk/Dropbox.php +++ b/sdk/Dropbox.php @@ -45,6 +45,7 @@ public static function postRequest($endpoint, $headers, $data, $json = TRUE) { curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); + $r = curl_exec($ch); curl_close($ch); diff --git a/sdk/Dropbox/Files.php b/sdk/Dropbox/Files.php index add1b59..809bbd6 100644 --- a/sdk/Dropbox/Files.php +++ b/sdk/Dropbox/Files.php @@ -627,6 +627,9 @@ public function search($path, $query, $start = 0, $max_results = 100, $mode = "f /* * $file_data can be either raw string data or a path to a file * @parameter $mode: "add", "update", "overwrite" + * Note this is not memory efficient when uploading very large files + * As it attempts to read the file into the entire string using file_get_contents + * Use upload sessions in this case and broke down the uploads by "chunks" */ public function upload($target_path, $file_data, $mode = "add") { $endpoint = "https://content.dropboxapi.com/2/files/upload"; @@ -673,34 +676,42 @@ public function upload_session_append($session_id, $offset, $file_path) { /** * append more data to an upload session. * a single request should not upload more than 150 MB. + * @postdata is a chunked content resource e.g. outputted by fread */ - public function upload_session_append_v2($session_id, $offset, $file_path, $close = FALSE) { + public function upload_session_append_v2($session_id, $offset, $postdata, $close = false) { $endpoint = "https://content.dropboxapi.com/2/files/upload_session/append_v2"; + $close = 'false'; + if (true === $close) { + $close = 'true'; + } $headers = array( "Content-Type: application/octet-stream", - "Dropbox-API-Arg: {\"cursor\": {\"session_id\": \"$session_id\", \"offset\": \"$offset\"}, \"close\": $close}" + "Dropbox-API-Arg: {\"cursor\": {\"session_id\": \"$session_id\", \"offset\": $offset}, \"close\": $close}" ); - $postdata = file_get_contents($file_path); - $returnData = Dropbox::postRequest($endpoint, $headers, $postdata); - if (isset($returnData["error"])) { - return $returnData["error_summary"]; - } - else { + $returnData = Dropbox::postRequest($endpoint, $headers, $postdata, false); + + if ('null' === $returnData) { return true; + } elseif (isset($returnData["error"])) { + return $returnData["error_summary"]; + } else { + return false; } } - /* - * Entry must be an instanceof Entry (Dropbox\Entry) - */ - public function upload_session_finish(Entry $entry) { - $endpoint = "https://content.dropboxapi.com/2/files/upload_session/finish"; + /** + * + * @param Entry $entry + * @param $postdata (This is a chunked content resource e.g. outputted by fread) + * @return mixed + */ + public function upload_session_finish(Entry $entry, $postdata) { + $endpoint = "https://content.dropboxapi.com/2/files/upload_session/finish"; + $entry = $entry->toJson(); $headers = array( - "Content-Type" => 'application/octet-stream', - "Dropbox-API-Arg" => $entry.toJson() + "Content-Type: application/octet-stream", + "Dropbox-API-Arg: $entry" ); - $headers = json_encode($headers); - $postdata = file_get_contents($file_path); $returnData = Dropbox::postRequest($endpoint, $headers, $postdata); if (isset($returnData["error"])) { return $returnData["error_summary"]; @@ -748,17 +759,22 @@ public function finish_batch_check($async_job_id) { } } - /* - * starts an upload session, needed where the size of a file is greater than 150 MB - * can last up to 48 hours - */ - public function upload_session_start($file_path, $close = false) { + /** + * + * @param $postdata (This is a chunked content resource e.g. outputted by fread) + * @param boolean $close + * @return mixed + */ + public function upload_session_start($postdata, $close = false) { $endpoint = "https://content.dropboxapi.com/2/files/upload_session/start"; + $close = 'false'; + if (true === $close) { + $close = 'true'; + } $headers = array( "Content-Type: application/octet-stream", "Dropbox-API-Arg: {\"close\": $close}" ); - $postdata = file_get_contents($file_path); $returnData = Dropbox::postRequest($endpoint, $headers, $postdata); if (isset($returnData["error"])) { return $returnData["error_summary"];