From 0b704e95f1c4b12106d003c73f775a6c1c34e902 Mon Sep 17 00:00:00 2001 From: Lucas Bickel Date: Tue, 21 Feb 2017 20:55:03 +0100 Subject: [PATCH] Use Zend_File_Transfer instead of $_FILES This should give us more information in the case of an error and is the framework idiomatic way to handle a RESTful file upload. I'm hoping this helps debug https://github.com/LibreTime/libretime/issues/3 --- airtime_mvc/application/models/airtime/CcFiles.php | 4 ++-- .../modules/rest/controllers/MediaController.php | 14 +++++++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/airtime_mvc/application/models/airtime/CcFiles.php b/airtime_mvc/application/models/airtime/CcFiles.php index ce02538b5..9a7ebd88a 100644 --- a/airtime_mvc/application/models/airtime/CcFiles.php +++ b/airtime_mvc/application/models/airtime/CcFiles.php @@ -92,8 +92,8 @@ class CcFiles extends BaseCcFiles { //Extract the original filename, which we set as the temporary title for the track //until it's finished being processed by the analyzer. - $originalFilename = $_FILES["file"]["name"]; - $tempFilePath = $_FILES['file']['tmp_name']; + $originalFilename = $fileArray['file']['name']; + $tempFilePath = $fileArray['file']['tmp_name']; try { return self::createAndImport($fileArray, $tempFilePath, $originalFilename); diff --git a/airtime_mvc/application/modules/rest/controllers/MediaController.php b/airtime_mvc/application/modules/rest/controllers/MediaController.php index 7a39d7e22..79208f56c 100644 --- a/airtime_mvc/application/modules/rest/controllers/MediaController.php +++ b/airtime_mvc/application/modules/rest/controllers/MediaController.php @@ -129,7 +129,19 @@ class Rest_MediaController extends Zend_Rest_Controller } try { - $sanitizedFile = CcFiles::createFromUpload($this->getRequest()->getPost()); + // REST uploads are not from Zend_Form, hence we handle them using Zend_File_transfer directly + $upload = new Zend_File_Transfer(); + // this error should not really get hit, letting the user know if it does is nice for debugging + // see: https://github.com/LibreTime/libretime/issues/3#issuecomment-281143417 + if (!$upload->isValid('file')) { + throw new Exception("invalid file uploaded"); + } + $fileInfo = $upload->getFileInfo('file'); + // this should have more info on any actual faults detected by php + if ($fileInfo['file']['error']) { + throw new Exception(sprintf('File upload error: %s', $fileInfo['file']['error'])); + } + $sanitizedFile = CcFiles::createFromUpload($fileInfo); $this->getResponse() ->setHttpResponseCode(201) ->appendBody(json_encode($sanitizedFile));