Skip to content

Commit 08505c5

Browse files
author
Benjamin Wilson Friedman
authored
Merge pull request #274 from montymxb/file-curl-fix
Update ParseFile to use ParseClient::_request
2 parents 0e8b40e + a7a477c commit 08505c5

File tree

2 files changed

+36
-48
lines changed

2 files changed

+36
-48
lines changed

src/Parse/ParseClient.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ public static function _encodeArray($value, $allowParseObjects)
302302
* @param null $data Data to provide with the request.
303303
* @param bool $useMasterKey Whether to use the Master Key.
304304
* @param bool $appRequest App request to create or modify a application
305+
* @param string $contentType The content type for this request, default is application/json
305306
*
306307
* @throws \Exception
307308
*
@@ -313,7 +314,8 @@ public static function _request(
313314
$sessionToken = null,
314315
$data = null,
315316
$useMasterKey = false,
316-
$appRequest = false
317+
$appRequest = false,
318+
$contentType = 'application/json'
317319
) {
318320
if ($data === '[]') {
319321
$data = '{}';
@@ -334,12 +336,12 @@ public static function _request(
334336
curl_setopt($rest, CURLOPT_URL, $url);
335337
curl_setopt($rest, CURLOPT_RETURNTRANSFER, 1);
336338
if ($method === 'POST') {
337-
$headers[] = 'Content-Type: application/json';
339+
$headers[] = 'Content-Type: '.$contentType;
338340
curl_setopt($rest, CURLOPT_POST, 1);
339341
curl_setopt($rest, CURLOPT_POSTFIELDS, $data);
340342
}
341343
if ($method === 'PUT') {
342-
$headers[] = 'Content-Type: application/json';
344+
$headers[] = 'Content-Type: '.$contentType;
343345
curl_setopt($rest, CURLOPT_CUSTOMREQUEST, $method);
344346
curl_setopt($rest, CURLOPT_POSTFIELDS, $data);
345347
}

src/Parse/ParseFile.php

Lines changed: 31 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -83,27 +83,23 @@ public function getName()
8383
/**
8484
* Send a REST request to delete the ParseFile.
8585
*
86+
* @param bool $useMasterKey Whether to use the Master Key.
8687
* @throws ParseException
8788
*/
88-
public function delete()
89+
public function delete($useMasterKey = true)
8990
{
9091
if (!$this->url) {
9192
throw new ParseException('Cannot delete file that has not been saved.');
9293
}
9394

94-
$headers = ParseClient::_getRequestHeaders(null, true);
95-
$url = ParseClient::getAPIUrl().'files/'.$this->getName();
96-
$rest = curl_init();
97-
curl_setopt($rest, CURLOPT_URL, $url);
98-
curl_setopt($rest, CURLOPT_CUSTOMREQUEST, 'DELETE');
99-
curl_setopt($rest, CURLOPT_RETURNTRANSFER, 1);
100-
curl_setopt($rest, CURLOPT_HTTPHEADER, $headers);
101-
$response = curl_exec($rest);
102-
$contentType = curl_getinfo($rest, CURLINFO_CONTENT_TYPE);
103-
if (curl_errno($rest)) {
104-
throw new ParseException(curl_error($rest), curl_errno($rest));
105-
}
106-
curl_close($rest);
95+
ParseClient::_request(
96+
'DELETE',
97+
'files/'.$this->getName(),
98+
null,
99+
null,
100+
$useMasterKey
101+
);
102+
107103
}
108104

109105
/**
@@ -188,54 +184,44 @@ public function _encode()
188184
/**
189185
* Uploads the file contents to Parse, if not saved.
190186
*
187+
* @param bool $useMasterKey Whether to use the Master Key.
191188
* @return bool
192189
*/
193-
public function save()
190+
public function save($useMasterKey = false)
194191
{
195192
if (!$this->url) {
196-
$response = $this->upload();
193+
$response = $this->upload($useMasterKey);
197194
$this->url = $response['url'];
198195
$this->name = $response['name'];
199196
}
200197

201198
return true;
202199
}
203200

204-
private function upload()
201+
/**
202+
* Internally uploads the contents of the file to a Parse Server
203+
*
204+
* @param bool $useMasterKey Whether to use the Master Key.
205+
* @return mixed Result from Parse API Call.
206+
* @throws ParseException
207+
*/
208+
private function upload($useMasterKey = false)
205209
{
210+
// get the MIME type of this file
206211
$fileParts = explode('.', $this->getName());
207212
$extension = array_pop($fileParts);
208213
$mimeType = $this->mimeType ?: $this->getMimeTypeForExtension($extension);
209214

210-
$headers = ParseClient::_getRequestHeaders(null, false);
211-
$url = ParseClient::getAPIUrl().'files/'.$this->getName();
212-
$rest = curl_init();
213-
curl_setopt($rest, CURLOPT_URL, $url);
214-
curl_setopt($rest, CURLOPT_RETURNTRANSFER, 1);
215-
curl_setopt($rest, CURLOPT_BINARYTRANSFER, 1);
216-
$headers[] = 'Content-Type: '.$mimeType;
217-
curl_setopt($rest, CURLOPT_POST, 1);
218-
curl_setopt($rest, CURLOPT_POSTFIELDS, $this->getData());
219-
curl_setopt($rest, CURLOPT_HTTPHEADER, $headers);
220-
$response = curl_exec($rest);
221-
$contentType = curl_getinfo($rest, CURLINFO_CONTENT_TYPE);
222-
if (curl_errno($rest)) {
223-
throw new ParseException(curl_error($rest), curl_errno($rest));
224-
}
225-
curl_close($rest);
226-
if (strpos($contentType, 'text/html') !== false) {
227-
throw new ParseException('Bad Request', -1);
228-
}
229-
230-
$decoded = json_decode($response, true);
231-
if (isset($decoded['error'])) {
232-
throw new ParseException(
233-
$decoded['error'],
234-
isset($decoded['code']) ? $decoded['code'] : 0
235-
);
236-
}
215+
return ParseClient::_request(
216+
'POST',
217+
'files/'.$this->getName(),
218+
null,
219+
$this->getData(),
220+
$useMasterKey,
221+
false,
222+
$mimeType
223+
);
237224

238-
return $decoded;
239225
}
240226

241227
private function download()

0 commit comments

Comments
 (0)