milesj / uploader

[Deprecated] A CakePHP plugin for file uploading and validating.
MIT License
193 stars 73 forks source link

FileValidationBehavior bug? #124

Closed choma closed 11 years ago

choma commented 11 years ago

Hi, I'm using version 4.2.0, on cake 2.3.6. The plugin is working well, except for validation. I have tried to validate the uploads type (with type, extension and mimeType rules), in both ways declared on the docs, like this:

$actsAs = array(
   'Uploader.Attachment' => array(
        'image' => array(
            'transforms' => array(...)
        )
    ),
    'Uploader.FileValidation' => array(
        'image' => array(
            'type' => 'image',

            // in this way does not work either
            /*
            'type' => array(
               'value' => 'image'
            )
            */
        )
    )
);

Is this a bug or am I doing something wrong?

Thanks in advance!

milesj commented 11 years ago

So is type failing, or all of them are?

choma commented 11 years ago

All of them.

milesj commented 11 years ago

Can you debug what the actual ext/mimetype of the file you are uploading is?

choma commented 11 years ago

I have tried with severals types of files, so I think the problem isn't there. If I use validations this way:

'Uploader.FileValidation' => array(
        'image' => array(
            'type' => 'image',
        )
);

I get this error:

Error: Cannot unset string offsets
File: /var/www/inta.v1/webroot/app/Plugin/Uploader/Model/Behavior/FileValidationBehavior.php
Line: 280

But if I use validations this way:

'Uploader.FileValidation' => array(
        'image' => array(
            'type' => 'image',
        )
);

I get this error:

Warning (2): file_exists() expects parameter 1 to be string, array given [APP/Vendor/mjohnson/transit/src/Transit/File.php, line 40]

Notice (8): Undefined variable: header [APP/View/Layouts/default.ctp, line 23]

Array does not exist

Error: An Internal Error Has Occurred.
milesj commented 11 years ago

Interesting. Never seen this error before, will look into.

milesj commented 11 years ago

I tested each of the following rules separately successfully.

'Uploader.FileValidation' => array(
    'path' => array(
        'required' => false,
        'extension' => array('jpg', 'jpeg'),
        'mimeType' => array('image/jpeg'),
        'type' => 'image',
        'extension' => array(
            'value' => array('jpg', 'jpeg'),
            'error' => 'Invalid extension'
        ),
        'mimeType' => array(
            'value' => array('image/jpeg'),
            'error' => 'Invalid mime type'
        ),
        'type' => array(
            'value' => 'image',
            'error' => 'Invalid type'
        )
    )
)

I'm pretty sure you need to update Transit. If you are using composer, just update everything.

choma commented 11 years ago

I downloaded the plugin again, and then I tried to update Transit through composer and I got the message "Nothing to install or update". I'll close the issue anyway, maybe it is a bad configuration. Thanks!

milesj commented 11 years ago

The reason I ask is because the line numbers in all your errors point to nothing in the actual code, so it seems like your code is out of date or altered.

What does your model look like?

choma commented 11 years ago

That is the first thing I thought, given that I was using an old version of the plugin, which I updated (I updated everything with composer too). But I can't get it to work in anyway. This is my model:

class News extends AppModel {

var $name = 'News';

var $validate = array(
    'title_spa' => array( 'required' => array('rule' => 'notEmpty') ),
    'summary_spa' => array( 'required' => array('rule' => 'notEmpty') ),
    'content_spa' => array( 'required' => array('rule' => 'notEmpty') ),
    /*'image' => array(
        //'required' => array('rule' => 'notEmpty', 'on' => 'create'),
        //'mimeType'=> array('rule' => array('mimeType', array('image/jpeg'))),
    )*/
);

var $actsAs = array(
    'Sluggable' => array(
        'title_field' => 'title_spa',
        'slug_field' => 'permalink',
        'slug_max_length' => 100,
        'separator' => '-'
    ),
    'Uploader.Attachment' => array(
        'image' => array(
            //'allowEmpty' => false,
            //'mimeType' => 'image',
            'transforms' => array(
                'thumb_image' => array(
                    'prepend' => 'thumb_',
                    'method' => 'resize',
                    'width' => 50,
                    'height' => 50
                ),
                'thumb_medium' => array(
                    'prepend' => 'thumb_medium_',
                    'method' => 'resize',
                    'width' => 400,
                    'height' => 400
                )
            )
        )
    ),
    'Uploader.FileValidation' => array(
        'image' => array(
            //'extension' => array('value' => array('jpg', 'jpeg', 'png', 'gif')),
            //'type' => array( 'value' => 'image' ),
            //'type' => 'image',
            'required' => array(
                'value' => false,
                'on' => 'update',
                'allowEmpty' => true
            )
        )
    )
);
}

I'm making some modifications on the AppModel, like this:

class AppModel extends Model {

public function beforeUpload($options) {

    // guardo el archivo dentro de una carpeta con el nombre del modelo
    //$options['finalPath'] = 'files/uploads/'.strtolower($this->alias).'/';
    $options['finalPath'] = '';
    //$options['uploadDir'] = WWW_ROOT . $options['finalPath'];
    $options['uploadDir'] = WWW_ROOT . DS . 'files' . DS . strtolower(Inflector::pluralize($this->alias)). $options['finalPath'];

    //if ( !isset($options['overwrite']) ) {
        //$options['overwrite'] = true;   // para que actualice los archivos
    //}

    return $options;

}

/**
 * callback del plugin Uploader
 * 
 **/
public function beforeTransform($options) {

    // para modificar los nombres de archivos con transforms
    $options['nameCallback'] = 'formatName';

    // guardo el archivo dentro de una carpeta con el nombre del modelo
    //$options['finalPath'] = 'files/uploads/'.strtolower($this->alias).'/';
    $options['finalPath'] = '';
    //$options['uploadDir'] = WWW_ROOT . $options['finalPath'];
    $options['uploadDir'] = WWW_ROOT . DS . 'files' . DS . strtolower(Inflector::pluralize($this->alias)). $options['finalPath'];

    return $options;

}

/**
 * quito detalles de las "transforms" del nombre de los archivos
 * 
 **/
public function formatName($name, $file) {
    return $this->getUploadedFile()->name();
}

}

Just in case, my Vendor/mjohnson/transit/version.md file says: 1.3.6