@@ -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