milesj / uploader

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

File validation with remote image url #105

Closed styks1987 closed 11 years ago

styks1987 commented 11 years ago

When passing a url to the 'image' field works great when Uploader.FileValidation is not enabled.

The problem comes when it tries validate the file.

$validator->setFile(new File($value['tmp_name'])); // line 355 in the FileValidationBehavior.php (Uploader)

This index does not exist if you are only passing a url. So I added this

if(is_array($value)){
    $validator->setFile(new File($value['tmp_name']));
}else{
    $validator->setFile(new File($value));
}

Then in the File.php (transit plugin)

The construct looks to see if the file exists which does not seem to work with urls. Do you know of any workarounds to load images from urls?

I also tried to fork your repo but I think something is wrong with github as it keeps giving me a 404.

thanks!

styks1987 commented 11 years ago

This is my simple fix.

Transit Plugin construct function

        public function __construct($path) {
        if(preg_match('/^http/', $path)){
            $new_path = TMP.time().substr($path, -7, 7);
            if(copy($path, $new_path)){
                $path = $new_path;
            }else{
                throw new IoException(sprintf('%s Could not copy the file.', $path));
            }
        }
        if (!file_exists($path)) {
            throw new IoException(sprintf('%s does not exist', $path));
        }
        $this->_path = $path;
    }

Uploader Plugin - FileValidationBehavior.php ~ line 352

// Use robust validator
            } else {
                $validator = new ImageValidator();
                if(is_array($value)){
                    $validator->setFile(new File($value['tmp_name']));
                }else{
                    $validator->setFile(new File($value));
                }

                return call_user_func_array(array($validator, $method), $params);
            }
milesj commented 11 years ago

Transit was built to only work with file uploads and the same applies to its validation system. I have no intention of changing its functionality.

I get what you're trying to do, but I am not sure I agree with it. File importing was simply just a bonus feature meant for internal use and nothing more. Attempting to validate it is literally copying the file twice into the local system causing more overhead.

I guess I need to know more about what your setup is? Are you allowing users to import files from a remote location? If so, they could simply save that file then upload it normally IMO.

styks1987 commented 11 years ago

Hey thanks for the response.

There very well may be a better solution to this. Here is what I need to do.

Scenario 1: I have to allow an anonymous user to upload an image. This image is sent to our S3 box and then put in queue for review by the admin. When the system loads up the image again for the admin to review, it pulls back the existing image from S3 as well as the crop dimensions specified by the end user. The admin has the ability to accept these crop dimensions or specify new ones. When they specify new dimensions, we need to recreate the image sizes based off of the new crop dimensions. To solve scenario 1: Now that I think about it, I don't have to validate it a second time because if it is already on our S3, it has already passed validation. So, I guess I would need to turn validation off if the file is a remote file.

Scenario 2: If the form fails validation for the anonymous user, I want to reload the image. Since something besides the image could have caused the failure, the image may not have been validated yet. I don't want to have to make the user relocate the image and specify new dimensions. So I use a hidden field and use the temporary file to reload the image. This doesn't need to be a url. It is actually just a string path.

To solve scenario 2: To solve this scenario, I made the modifications to FileValidationBehavior.php.

thanks and have a great day!

milesj commented 11 years ago

I added import validation since lot's of people kept requesting it. Let me know if it works for you.

https://github.com/milesj/Uploader/commit/471474bba24d2fa549eaff93aebafc221de9275e

milesj commented 11 years ago

This is working correctly in 4.0.8.