Giter Club home page Giter Club logo

cake3-upload's Introduction

Hi there ๐Ÿ‘‹

  • 34 years and learning the development since 10+ years in autodidact.

  • ๐ŸŒฑ Mainly backend developer with PHP and Laravel, Symfony & CakePHP.

  • ๐Ÿ‘ฏ I have also worked/work with JavaScript, TypeScript, SASS, VueJS, WebPack, Redis...

  • โšกMy personnal website : https://github.com/XetaIO/Xetaravel

Xeta Logo

https://xetaravel.com is a resource to help people starting with Laravel. I have developed this site to try Laravel, and I have decided to release it to help people starting with Laravel, so there is probably some custom configurations/functions that only fit my needs. Now i use this site for my personnal blog.

๐Ÿ† Profile's Stats

Xety's GitHub stats Xety Trophies

cake3-upload's People

Contributors

antograssiot avatar cwbit avatar lorenzo avatar xety avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cake3-upload's Issues

Some new features like file type recognition and more powerful identifiers

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?

2.0 - Rewrite the entire plugin to use the correct callback methods

There we go.

One of the big problem in this plugin, is that the plugin is using the beforeSave() callback method. And that cause several serious issues like #2.

Because this plugin is using by many people (7.2k download), I don't want to rewrite it only with my idea and don't listen the other people, but i want that all people who are interested by this plugin to participate to the 2.0 by exposing their idea, new features etc and we will try to do the best 2.0 as we can. ๐Ÿ˜›

The only thing that i want, is to keep all the current features as described here : Cake3-Upload#configuration to not break all the current configuration of the plugin. So the people will be able to update the plugin from the 1.x to the 2.0 without breaking changes.

Now i will expose the new features that i have in my mind :

  • Rezise image on fly using Intervention/image.
    • Use thumbnails model to create multiple images with different sizes. I.e :
      • users/1/avatar_250x250.png
      • users/1/avatar_80x80.png
      • etc
    • Also, check if we can add more customization to the image, like an option to change the background-color etc.
    • All these options must be customizable for each fields (Not a global configuration)
  • Allow multiple upload.
  • Keep the prefix option, but maybe write a Helper for that, and deprecate this option ?
  • Allow remote upload ? Warning, this can be a serious security issue, if it's not handling correctly.
  • Use the afterDelete() callback method to remove images linked to this Entity.
  • Add more complete examples
  • Use https://readthedocs.org for the docs ?

Expose your ideas, comments, what you want to see in the 2.0, what is the best callbacks to use for you etc. ๐Ÿ˜›

Use beforeMarshal instead of beforeSave

As now CakePHP run validation after Entity init, this will enable appropriate validation on uploaded file and use of custom methods/callbacks from Entity before it has saved.

Add :model identifier to path

It would be nice to have an upload path automagically created using the Inflector::tableize'd version of the entity alias, e.g. upload/:model/:id/:md5 -> upload/users/2/5e3e0d0f163196cb9526d97be1b2ce26.jpg

namespace issue

Your plugin is still called

Xety/Cake3Upload

it should have the version number in here though as per guidelines for
Are you planning to fix this for 4.x?

Xety/Upload

or

Xety/CakeUpload

etc, even though the latter is not needed with the Xety vendor prefix making it already unique and also scoped.

is usage instruction complete?

I have followed the instructions on a fresh install and my model saves without errors (after nixing out all the validation rules for my image model), but no file is saved in the webroot/img/foldername. no information is saved to the database either, other than an empty row (except id).

apologies, I'm new to cakePHP 3.0,

PHP upload errors do not trigger error in beforeSave

You're checking to see if the user didn't upload a file at all, but the other PHP file-upload errors cause unexpected behavior.

In my case, the user was trying to upload huge files, well beyond the upload_max_filesize limit. The "file" would get saved with 0 bytes, the database would get updated, and the system would tell the user that everything was great.

Here's a snippet that I think fixes the issue; just replace the UPLOAD_ERR_NO_FILE check with something like this:

if (!empty($file['error'])) {
    switch ($file['error']) {
        case UPLOAD_ERR_INI_SIZE:
            $message = "The uploaded file exceeds the upload_max_filesize directive in php.ini : " . ini_get('upload_max_filesize');
            break;
        case UPLOAD_ERR_FORM_SIZE:
            $message = "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form";
            break;
        case UPLOAD_ERR_NO_FILE:
            continue 2; // NOT throwing an exception here
            break;
        case UPLOAD_ERR_PARTIAL:
            $message = "The uploaded file was only partially uploaded";
            break;
        case UPLOAD_ERR_NO_TMP_DIR:
            $message = "Missing a temporary folder";
            break;
        case UPLOAD_ERR_CANT_WRITE:
            $message = "Failed to write file to disk";
            break;
        case UPLOAD_ERR_EXTENSION:
            $message = "File upload stopped by extension";
            break;
        default:
            $message = "Unknown upload error";
            break;
    }

    throw new \ErrorException($message);
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.