Skip to content

Commit 6fa1e87

Browse files
author
Fosco Marotto
committed
Adding delete to ParseFile.
1 parent 609bf20 commit 6fa1e87

File tree

4 files changed

+50
-3
lines changed

4 files changed

+50
-3
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.idea
2-
2+
vendor/
3+
composer.lock

src/Parse/ParseClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,4 +357,4 @@ public static function getProperDateFormat($value)
357357
return $date;
358358
}
359359

360-
}
360+
}

src/Parse/ParseFile.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,32 @@ public function getName()
6868
return $this->name;
6969
}
7070

71+
/**
72+
* Send a REST request to delete the ParseFile
73+
*
74+
* @throws ParseException
75+
*/
76+
public function delete()
77+
{
78+
if (!$this->url) {
79+
throw new ParseException("Cannot delete file that has not been saved.");
80+
}
81+
82+
$headers = ParseClient::_getRequestHeaders(null, true);
83+
$url = ParseClient::HOST_NAME . '/1/files/' . $this->getName();
84+
$rest = curl_init();
85+
curl_setopt($rest, CURLOPT_URL, $url);
86+
curl_setopt($rest, CURLOPT_CUSTOMREQUEST, "DELETE");
87+
curl_setopt($rest, CURLOPT_RETURNTRANSFER, 1);
88+
curl_setopt($rest, CURLOPT_HTTPHEADER, $headers);
89+
$response = curl_exec($rest);
90+
$contentType = curl_getinfo($rest, CURLINFO_CONTENT_TYPE);
91+
if (curl_errno($rest)) {
92+
throw new ParseException(curl_error($rest), curl_errno($rest));
93+
}
94+
curl_close($rest);
95+
}
96+
7197
/**
7298
* Return the mimeType for the file, if set.
7399
*
@@ -207,6 +233,10 @@ private function download()
207233
if (curl_errno($rest)) {
208234
throw new ParseException(curl_error($rest), curl_errno($rest));
209235
}
236+
$httpStatus = curl_getinfo($rest, CURLINFO_HTTP_CODE);
237+
if ($httpStatus > 399) {
238+
throw new ParseException("Download failed, file may have been deleted.", $httpStatus);
239+
}
210240
$this->mimeType = curl_getinfo($rest, CURLINFO_CONTENT_TYPE);
211241
$this->data = $response;
212242
curl_close($rest);
@@ -412,4 +442,4 @@ private function getMimeTypeForExtension($extension)
412442
return 'unknown/unknown';
413443
}
414444

415-
}
445+
}

tests/ParseFileTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,20 @@ public function testUnsavedFileOnObjectSave()
123123
$this->assertEquals($contents, $contentsAgain);
124124
}
125125

126+
public function testFileDelete()
127+
{
128+
$data = "c-c-c-combo breaker";
129+
$name = "php.txt";
130+
$file = ParseFile::createFromData($data, $name);
131+
$file->save();
132+
$url = $file->getURL();
133+
$fileAgain = ParseFile::_createFromServer($name, $url);
134+
$contents = $fileAgain->getData();
135+
$this->assertEquals($data, $contents);
136+
$file->delete();
137+
$fileAgain = ParseFile::_createFromServer($name, $url);
138+
$this->setExpectedException('Parse\ParseException', 'Download failed');
139+
$contents = $fileAgain->getData();
140+
}
141+
126142
}

0 commit comments

Comments
 (0)