Skip to content

Adding delete to ParseFile. #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 24, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea

vendor/
composer.lock
2 changes: 1 addition & 1 deletion src/Parse/ParseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,4 +357,4 @@ public static function getProperDateFormat($value)
return $date;
}

}
}
32 changes: 31 additions & 1 deletion src/Parse/ParseFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -412,4 +442,4 @@ private function getMimeTypeForExtension($extension)
return 'unknown/unknown';
}

}
}
16 changes: 16 additions & 0 deletions tests/ParseFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

}