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

Enhancement - Delete file during/after edit #493

Open
stosh15x opened this issue Jul 1, 2018 · 3 comments
Open

Enhancement - Delete file during/after edit #493

stosh15x opened this issue Jul 1, 2018 · 3 comments

Comments

@stosh15x
Copy link

stosh15x commented Jul 1, 2018

Hi I recently noticed an issue that files where not being deleted when I was editing a table. (IE: User changes their profile photo.) I have the plugin uploading the file, updating the database but the old file doesn't get deleted.

Note:
I changed the path in the config to:

'path' => 'webroot{DS}img{DS}{model}{DS}{field}{DS}'

So here is my temporary solution in the model but I imagine there is a better way to accomplish this.

use Cake\Filesystem\File;
use Cake\Event\Event;
use Cake\ORM\Entity;
use ArrayObject;

// clean up default leaderboard image change
public function afterSave(Event $event, Entity $entity, ArrayObject $options) {
  $field = 'image';

  $original = $entity->getOriginal($field);
  if($entity->{$field} != $original && $original != null) {
    $path = ROOT . DS . $entity->image_dir . $original;
    $file = new File($path);
    if($file->exists()) {
       $file->delete();
    }
    $file->close();
  }
}
@b0n
Copy link

b0n commented May 31, 2019

I also write afterSave method with almost afterDelete code at Upload.Upload behavior.

    /**
     * Deletes the original files after the entity is saved
     *
     * @param Event $event
     * @param Entity $entity
     * @param ArrayObject $options
     * @return bool
     */
    public function afterSave(Event $event, Entity $entity, ArrayObject $options)
    {
        $result = true;

        $behavior = $this->getBehavior('Upload');

        foreach ($behavior->getConfig(null, []) as $field => $settings) {
            if ($entity->isNew()) {
                continue;
            }

            if (!$entity->isDirty($field)) {
                continue;
            }

            if (Hash::get($settings, 'keepFilesOnDelete', true)) {
                continue;
            }

            $dirField = Hash::get($settings, 'fields.dir', 'dir');
            if ($entity->has($dirField)) {
                $path = $entity->get($dirField);
            } else {
                $path = $behavior->getPathProcessor($entity, $entity->get($field), $field, $settings)->basepath();
            }

            $original_entity = clone $entity;
            $original_entity->{$field} = $entity->getOriginal($field);
            $callback = Hash::get($settings, 'deleteCallback', null);
            if ($callback && is_callable($callback)) {
                $files = $callback($path, $original_entity, $field, $settings);
            } else {
                $files = [$path . $original_entity->get($field)];
            }

            $writer = $behavior->getWriter($entity, [], $field, $settings);
            $success = $writer->delete($files);

            if ($result && (new Collection($success))->contains(false)) {
                $result = false;
            }
        }

        return $result;
    }

@general2000vn
Copy link

b0n,

Where should i place your afterSave() method?
I tried to place it in my Model which use the Upload behavious, but then it complain that undefine $behavior->getPathProcessor() and $behavior->getWriter .

I checked and see those method are public, but somehow my model can not call them.

Please kindly advice.
I am using CakePHP 4.x and Josegonzalez/cakephp-upload 6.0

@b0n
Copy link

b0n commented Jan 12, 2022

Hello general!

I use Josegonzalez/cakephp-upload 3.0. Please checkout and try these.
pathProcessor https://github.com/FriendsOfCake/cakephp-upload/blob/3.8.1/src/Model/Behavior/UploadBehavior.php#L171
getWriter https://github.com/FriendsOfCake/cakephp-upload/blob/3.8.1/src/Model/Behavior/UploadBehavior.php#L194

And Josegonzalez/cakephp-upload 6.0 has these methods. Please check this out.

https://github.com/FriendsOfCake/cakephp-upload/blob/6.0.0/src/Model/Behavior/UploadBehavior.php

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

No branches or pull requests

4 participants