FriendsOfCake / cakephp-upload

CakePHP: Handle file uploading sans ridiculous automagic
https://cakephp-upload.readthedocs.io/
MIT License
551 stars 255 forks source link

filegrabber in cakephp 4 #606

Closed tgoeminne closed 8 months ago

tgoeminne commented 8 months ago

Hi,

this used to work in cakephp 3 to use the Filegrabber Behavior to use urls to import files. But in cakephp 4 does not work anymore. Can tell me where I need to change it? It now needs a laminas object instead of data array?

<?php

namespace App\Model\Behavior;

use Josegonzalez\Upload\Model\Behavior\UploadBehavior as UploadBehavior;
use Cake\Http\Client;
use Cake\Datasource\EntityInterface;
use Cake\Event\EventInterface;
use ArrayObject;
use Cake\Log\Log;

class FileGrabberBehavior extends UploadBehavior {

/**
 * Download remote file into PHP's TMP dir
 */
    public function beforeSave(EventInterface $event, EntityInterface $entity, ArrayObject $options)
    {

        if($entity->isNew()) {

            foreach (array_keys($this->getConfig(null, [])) as $field) {

                if (!empty($entity->get($field)) && $this->_isURI($entity->get($field))) {

                    $uri = $entity->get($field);

                    if (!$this->_grab($entity, $field, $uri)) {
                        //Log::write('debug', print_r($uri, true));
                        return false;
                    }
                }
            }
        }
        parent::beforeSave($event,$entity,$options);
    }

    public function _grab($entity, $field, $uri) {

        $socket = new client();
        $response = $socket->get(trim($uri));
        $headers = $response->getHeaders();

        //Log::write('debug', print_r($headers, true));

        $file_name = basename($uri);
        // this part needs to change, send it to another dir
        $tmp_file = sys_get_temp_dir() . '/' . $file_name;

        if ($response->getStatusCode() != 200) {
            return false;
        }

        $entity->set($field, array(
            'name' => $file_name,
            'type' => $headers['Content-Type'][0],
            'tmp_name' => $tmp_file,
            'error' => 0,
            'size' => $headers['Content-Length'][0],
        ));

        $file = file_put_contents($tmp_file, $response->getBody());

        if (!$file) {

            //Log::write('debug', print_r($file, true));
            return false;
        }
        return true;
    }

    public function _isURI($string) {
        return (filter_var($string, FILTER_VALIDATE_URL) ? true : false);
    }
}
davidyell commented 8 months ago

This doesn't feel like an issue for the plugin to me. This is more about upgrading your Behaviour to work with CakePHP 4.

You are probably going to find answers for this type of question in the documentation. https://book.cakephp.org/4/en/orm/behaviors.html#creating-a-behavior

tgoeminne commented 8 months ago

ok i already found it I used the beforemarshall and uploadedfile class to change data

tgoeminne commented 7 months ago

Filegrabber used to be part of the old plugin. Thats why I ask here.