Skip to content

Commit

Permalink
Merge pull request #1444 from dreamsxin/request_file
Browse files Browse the repository at this point in the history
Fix #1442 implemented \Phalcon\Request\File::getRealType
  • Loading branch information
Phalcon committed Oct 26, 2013
2 parents 29750ba + c6836c5 commit 79570ec
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
52 changes: 51 additions & 1 deletion ext/http/request/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "kernel/array.h"
#include "kernel/object.h"
#include "kernel/fcall.h"
#include "kernel/string.h"

/**
* Phalcon\Http\Request\File
Expand Down Expand Up @@ -75,6 +76,7 @@ PHALCON_INIT_CLASS(Phalcon_Http_Request_File){
zend_declare_property_null(phalcon_http_request_file_ce, SL("_tmp"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_http_request_file_ce, SL("_size"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_http_request_file_ce, SL("_type"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_http_request_file_ce, SL("_real_type"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_http_request_file_ce, SL("_error"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_http_request_file_ce, SL("_key"), ZEND_ACC_PROTECTED TSRMLS_CC);

Expand Down Expand Up @@ -185,13 +187,61 @@ PHP_METHOD(Phalcon_Http_Request_File, getType){
/**
* Gets the real mime type of the upload file using finfo
*
* @todo Not implemented
* @return string
*/
PHP_METHOD(Phalcon_Http_Request_File, getRealType){

zval *constant, *finfo, *temp_file, *mime, *pattern, *matches, *ret;

PHALCON_MM_GROW();

PHALCON_OBS_VAR(mime);
phalcon_read_property_this(&mime, this_ptr, SL("_real_type"), PH_NOISY_CC);

if (Z_TYPE_P(mime) == IS_STRING) {
RETURN_CTOR(mime);
}

PHALCON_INIT_VAR(constant);
if (!zend_get_constant(SL("FILEINFO_MIME"), constant TSRMLS_CC)) {
RETURN_MM_NULL();
}

PHALCON_INIT_VAR(finfo);
phalcon_call_func_p1(finfo, "finfo_open", constant);

if (Z_TYPE_P(finfo) != IS_RESOURCE) {
RETURN_MM_NULL();
}

PHALCON_OBS_VAR(temp_file);
phalcon_read_property_this(&temp_file, this_ptr, SL("_tmp"), PH_NOISY_CC);

PHALCON_INIT_NVAR(mime);
phalcon_call_func_p2(mime, "finfo_file", finfo, temp_file);
phalcon_call_func_p1_noret("finfo_close", finfo);

if (Z_TYPE_P(mime) != IS_STRING) {
RETURN_MM_NULL();
}

PHALCON_INIT_VAR(pattern);
ZVAL_STRING(pattern, "#^([a-z\\-]+\\/[a-z0-9\\-\\.\\+]+)(;\\s.+)?$#", 1);

PHALCON_INIT_VAR(matches);
PHALCON_INIT_VAR(ret);
phalcon_preg_match(ret, &ret, pattern, mime, matches TSRMLS_CC);

if (zend_is_true(ret) && phalcon_array_isset_long(matches, 1)) {
PHALCON_OBS_NVAR(ret);
phalcon_array_fetch_long(&ret, matches, 1, PH_NOISY);

phalcon_update_property_this(this_ptr, SL("_real_type"), ret TSRMLS_CC);

RETURN_CTOR(ret);
}

RETURN_MM_NULL();
}

/**
Expand Down
20 changes: 20 additions & 0 deletions unit-tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,5 +329,25 @@ public function testIssues1265()
$this->assertEquals($request->getPost('array', 'string'), array('string' => 'world'));
$this->assertEquals($request->getPost('array', 'string', NULL, TRUE, TRUE), NULL);
}

public function testIssues1442()
{
$request = new \Phalcon\Http\Request();

$_FILES = array (
'test' => array(
'name' => 'test',
'type' => 'text/plain',
'tmp_name' => 'unit-tests/assets/phalconphp.jpg',
'size' => 1,
'error' => 0,
)
);

foreach ($request->getUploadedFiles(TRUE) as $file) {
$this->assertEquals($file->getType(), 'text/plain');
$this->assertEquals($file->getRealType(), 'image/jpeg');
}
}
}

0 comments on commit 79570ec

Please sign in to comment.