milesj / uploader

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

Tranformed images are not deleted automatically #189

Open WebCustoms opened 9 years ago

WebCustoms commented 9 years ago

Hi,

first, great job!!

I have a problem! If I delete a database record, only the original imge are deleted. The transformed images are not deleted!

What must I do to make it work?


<?php

App::uses('AppModel', 'Model');

define('ADVER_IMAGE_DIR', '/files/advertisements/');
define('ADVER_ORIGINAL_DIR', ADVER_IMAGE_DIR . '/originals/');
define('ADVER_UPLOAD_ORIGINAL_DIR', WWW_ROOT . ADVER_ORIGINAL_DIR);

define('ADVER_THUMBNAIL_DIR', ADVER_IMAGE_DIR . '/thumbs/');
define('ADVER_UPLOAD_THUMBNAIL_DIR', WWW_ROOT . ADVER_THUMBNAIL_DIR);

/**
 * Advertisement Model
 *
 */
class Advertisement extends AppModel {

    public $actsAs = array(
        'Uploader.FileValidation' => array(
            'image' => array(
                'maxWidth' => 1920,
                'minHeight' => 1080,
                'extension' => array('gif', 'jpg', 'png', 'jpeg'),
                'type' => 'image',
                'mimeType' => array('image/gif', 'image/jpeg', 'image/png'),
                'filesize' => 5242880,
                'required' => true
            )
        ),
        'Uploader.Attachment' => array(
            'image' => array(
                'nameCallback' => 'imageName',
                'tempDir' => TMP,
                'finalPath' => ADVER_ORIGINAL_DIR,
                'uploadDir' => ADVER_UPLOAD_ORIGINAL_DIR,
                'overwrite' => true,
                'stopSave' => true,
                'allowEmpty' => true,
                'metaColumns' => array(
                    'ext' => 'extension',
                    'basename' => 'basename',
                    'type' => 'type',
                    'size' => 'size',
                    'exif.model' => 'camera',
                    'exif.make' => 'make',
                ),
                'transforms' => array(
                    'imageSmall' => array(
                        'nameCallback' => 'imageName',
                        'class' => 'crop',
                        'append' => '-small',
                        'overwrite' => true,
                        'self' => false,
                        'width' => 100,
                        'height' => 100,
                        'finalPath' => ADVER_THUMBNAIL_DIR,
                        'uploadDir' => ADVER_UPLOAD_THUMBNAIL_DIR
                    ),
                    'imageMedium' => array(
                        'nameCallback' => 'imageName',
                        'class' => 'resize',
                        'append' => '-medium',
                        'width' => 800,
                        'height' => 600,
                        'aspect' => false,
                        'finalPath' => ADVER_THUMBNAIL_DIR,
                        'uploadDir' => ADVER_UPLOAD_THUMBNAIL_DIR
                    ),
                    'imageLarge' => array(
                        'nameCallback' => 'imageName',
                        'class' => 'resize',
                        'append' => '-large',
                        'width' => 1920,
                        'height' => 1080,
                        'aspect' => false,
                        'finalPath' => ADVER_THUMBNAIL_DIR,
                        'uploadDir' => ADVER_UPLOAD_THUMBNAIL_DIR
                    )
                )
            )
        )
    );

    public function imageName($name, $file) {
        return $this->getUploadedFile()->name();
    }

}
zhaff commented 9 years ago

Hi, You need to use dbCloumn to store the filename once transformed.

Example:

'transforms' => array(
                    'imageSmall' => array(
                        'class' => 'crop',
                        'append' => '-small',
                        'overwrite' => true,
                        'self' => false,
                        'width' => 138,
                        'height' => 118,
                        'nameCallback' => 'transformNameCallback',
                        'dbColumn' => 'thumbnail',
                    ),
                ),

In this case I used field named "thumbnail" to store the filename. Have a try.

bradmaxs commented 9 years ago

@richcom: I too am having that problem. I tried removing the nameCallback from the transform and still the same. I do however have a lot of other files that I am dealing with with this uploader (config for example) and the error I am getting is that it is looking for the config finalPath provided which references another image and model completely. For me it deletes the transformed image and not the original.

I am using Version 4.6.0 with CakePHP Version 2.5.8.

'Uploader.Attachment' => array(
    'config' => array(
        'finalPath' => '/files/item-configs/',
        'dbColumn' => 'name_original',
        'nameCallback' => 'formatName',
        'overwrite' => false,
        'stopSave' => true,
        'allowEmpty' => true,
        'metaColumns' => array(
            'ext' => 'ext',
            'type' => 'file_type',
            'size' => 'size'
        )
    ),
    'part_icon' => array(
        'finalPath' => '/files/part-icons/',
        'dbColumn' => 'name_original',
        'nameCallback' => 'formatName',
        'overwrite' => false,
        'stopSave' => true,
        'allowEmpty' => true,
        'metaColumns' => array(
            'ext' => 'ext',
            'type' => 'file_type',
            'size' => 'size',
            'width' => 'width',
            'height' => 'height'
        ),
        'transforms' => array(
            'thumb' => array(
                'class' => 'crop',
                'nameCallback' => 'formatName',
                'append' => '-thumb',
                'overwrite' => true,
                'width' => 250,
                'height' => 250
            )
        )
    )
);

@zhaff: The 'imageSmall' key takes care of the dbColumn so you don't have to repeat it in the array. I did test and if you provide the dbColum, it does overwrite the 'imageSmall' key and save to whatever you name it however it isn't necessary since it is already handled.

milesj commented 9 years ago

What versions of Cake are you all using?

bradmaxs commented 9 years ago

It seems that other images are not being deleted now.

I am using Version 4.6.0 with CakePHP Version 2.5.8.

I checked my error logs in cake after deleting an image (this time without transformation) and it has the wrong path even though the right one is stored in the db.

Let's say I am trying to delete /files/part-icon/ba1fcc2a10897dd417ec7e6408a7c7e3.stl

The error I am getting is /files/item-configs/ba1fcc2a10897dd417ec7e6408a7c7e3.stl does not exist

Seems it is getting stuck on the first directory in the Attachment array.

Thanks Miles.