@@ -83,27 +83,23 @@ public function getName()
83
83
/**
84
84
* Send a REST request to delete the ParseFile.
85
85
*
86
+ * @param bool $useMasterKey Whether to use the Master Key.
86
87
* @throws ParseException
87
88
*/
88
- public function delete ()
89
+ public function delete ($ useMasterKey = true )
89
90
{
90
91
if (!$ this ->url ) {
91
92
throw new ParseException ('Cannot delete file that has not been saved. ' );
92
93
}
93
94
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
+
107
103
}
108
104
109
105
/**
@@ -188,54 +184,44 @@ public function _encode()
188
184
/**
189
185
* Uploads the file contents to Parse, if not saved.
190
186
*
187
+ * @param bool $useMasterKey Whether to use the Master Key.
191
188
* @return bool
192
189
*/
193
- public function save ()
190
+ public function save ($ useMasterKey = false )
194
191
{
195
192
if (!$ this ->url ) {
196
- $ response = $ this ->upload ();
193
+ $ response = $ this ->upload ($ useMasterKey );
197
194
$ this ->url = $ response ['url ' ];
198
195
$ this ->name = $ response ['name ' ];
199
196
}
200
197
201
198
return true ;
202
199
}
203
200
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 )
205
209
{
210
+ // get the MIME type of this file
206
211
$ fileParts = explode ('. ' , $ this ->getName ());
207
212
$ extension = array_pop ($ fileParts );
208
213
$ mimeType = $ this ->mimeType ?: $ this ->getMimeTypeForExtension ($ extension );
209
214
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
+ );
237
224
238
- return $ decoded ;
239
225
}
240
226
241
227
private function download ()
0 commit comments