diff --git a/.gitignore b/.gitignore index c6ef2182..157ff0c5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .idea - +vendor/ +composer.lock diff --git a/src/Parse/ParseClient.php b/src/Parse/ParseClient.php index a405bbad..bf4a9e93 100755 --- a/src/Parse/ParseClient.php +++ b/src/Parse/ParseClient.php @@ -357,4 +357,4 @@ public static function getProperDateFormat($value) return $date; } -} \ No newline at end of file +} diff --git a/src/Parse/ParseFile.php b/src/Parse/ParseFile.php index a9313188..806153ab 100755 --- a/src/Parse/ParseFile.php +++ b/src/Parse/ParseFile.php @@ -68,6 +68,32 @@ public function getName() return $this->name; } + /** + * Send a REST request to delete the ParseFile + * + * @throws ParseException + */ + public function delete() + { + if (!$this->url) { + throw new ParseException("Cannot delete file that has not been saved."); + } + + $headers = ParseClient::_getRequestHeaders(null, true); + $url = ParseClient::HOST_NAME . '/1/files/' . $this->getName(); + $rest = curl_init(); + curl_setopt($rest, CURLOPT_URL, $url); + curl_setopt($rest, CURLOPT_CUSTOMREQUEST, "DELETE"); + curl_setopt($rest, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($rest, CURLOPT_HTTPHEADER, $headers); + $response = curl_exec($rest); + $contentType = curl_getinfo($rest, CURLINFO_CONTENT_TYPE); + if (curl_errno($rest)) { + throw new ParseException(curl_error($rest), curl_errno($rest)); + } + curl_close($rest); + } + /** * Return the mimeType for the file, if set. * @@ -207,6 +233,10 @@ private function download() if (curl_errno($rest)) { throw new ParseException(curl_error($rest), curl_errno($rest)); } + $httpStatus = curl_getinfo($rest, CURLINFO_HTTP_CODE); + if ($httpStatus > 399) { + throw new ParseException("Download failed, file may have been deleted.", $httpStatus); + } $this->mimeType = curl_getinfo($rest, CURLINFO_CONTENT_TYPE); $this->data = $response; curl_close($rest); @@ -412,4 +442,4 @@ private function getMimeTypeForExtension($extension) return 'unknown/unknown'; } -} \ No newline at end of file +} diff --git a/tests/ParseFileTest.php b/tests/ParseFileTest.php index 53720dba..00869c0f 100644 --- a/tests/ParseFileTest.php +++ b/tests/ParseFileTest.php @@ -123,4 +123,20 @@ public function testUnsavedFileOnObjectSave() $this->assertEquals($contents, $contentsAgain); } + public function testFileDelete() + { + $data = "c-c-c-combo breaker"; + $name = "php.txt"; + $file = ParseFile::createFromData($data, $name); + $file->save(); + $url = $file->getURL(); + $fileAgain = ParseFile::_createFromServer($name, $url); + $contents = $fileAgain->getData(); + $this->assertEquals($data, $contents); + $file->delete(); + $fileAgain = ParseFile::_createFromServer($name, $url); + $this->setExpectedException('Parse\ParseException', 'Download failed'); + $contents = $fileAgain->getData(); + } + }