Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some new features like file type recognition and more powerful identifiers #17

Open
sarrala opened this issue Aug 20, 2016 · 0 comments

Comments

@sarrala
Copy link

sarrala commented Aug 20, 2016

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant