Xety/Cake3-Upload

Some new features like file type recognition and more powerful identifiers

Opened this issue · 0 comments

Some time ago I forked this project and made some improvements for my projects like file type / content detection through heuristics and some of that stuff can be found from https://github.com/sarrala/Cake3-Upload/tree/master/src

I've also divided this plugin in a way that one could use it as base and override stuff.

Also fixed few bugs (and probably added some) and added more magic keywords with more powerful templates for adding custom keywords:

    protected function _getUploadPath(Entity $entity, $path = false, $source_file = false, $extension = false, $options = []) {

        if ($path === false) {
            return false;
        }

        $extension = $extension ? $extension : '';
        $path = trim( $path, DS );

        $identifiers = array_merge([ 
                ':uid' => function ($e) use($options) { return $options['loggedInUser']; },
                ':owner' => function ($e) { return $e->user_id; },
                ':id' => $entity->id, 
                //':mime' => *CURRENTLY USELESS, REQUIRES EXECUTION REORDERING*
                ':size' => function ($e) use($source_file) { return filesize($source_file); }, 
                ':hsize' => function ($e) use($source_file) { return str_pad(base_convert(filesize($source_file),10,16),8,'0',STR_PAD_LEFT); }, 
                ':md5' => function ($e) use($source_file) { return md5_file($source_file); }, 
                ':sha256' => function ($e) use($source_file) { return hash_file('sha256',$source_file); },
                ':fast-hash' => function ($e) { return md5( uniqid( uniqid('', rand(0,1)), true )); }, 
                ':fast-uniq' => function ($e) { return hash('sha256', mt_rand() . uniqid( uniqid('', rand(0,1)), true ) . uniqid('', mt_rand()%2 )); },
                ':date' => date( 'Y-m-d' ),
                ':time' => date( 'His' ),
                ':y' => date( 'Y' ),
                ':m' => date( 'm' ),
                ':d' => date( 'd' ),
                ':extcase' => $extension,
                ':ext' => strtolower( $extension ),
                ':.extcase' => '.' . $extension,
                ':.ext' => '.' . strtolower( $extension ),
        ],$this->_config['templates']);

        foreach ($identifiers as $src => $dst) {
            if (mb_strpos($path, $src) !== false) {
                $path = strtr($path, [$src => is_callable($dst) ? $dst($entity) : $dst]);
            }
        }

        return $path;

    }

If any of this stuff feels useful then maybe it could be integrated into this project.
Unfortunately, I have not written any tests for this stuff.

Any comments on those features?