<commit_before><?php
/** 
* Attachment Behavior
*
* A CakePHP Behavior that attaches a file to a model, and uploads automatically, then stores a value in the database.
*
* @author      <NAME> - www.milesj.me
* @copyright   Copyright 2006-2010, <NAME>, Inc.
* @license     http://opensource.org/licenses/mit-license.php - Licensed under The MIT License
* @link        http://milesj.me/resources/script/uploader-plugin
*/

App::import('Component', array('Uploader.Uploader', 'Uploader.S3Transfer'));

class AttachmentBehavior extends ModelBehavior {

    /**
     * AS3 domain snippet.
     */
    const AS3_DOMAIN = 's3.amazonaws.com';

    /**
     * Files that have been uploaded / attached; used for fallback functions.
     *
     * @access private
     * @var array
     */
    private $__attached = array();

    /**
     * All user defined attachments; images => model.
     *
     * @access private
     * @var array
     */
    private $__attachments = array();

    /**
     * The default settings for attachments.
     *
     * @access private
     * @var array
     */
    private $__defaults = array(
        'uploadDir' 	=> null,
        'dbColumn'		=> 'uploadPath',
        'maxNameLength' => null,
        'overwrite'		=> true,
        'name'			=> null,
        'transforms'	=> array(),
        's3'			=> array(),
        'skipSave'      => true,
        'metaColumns'   => array(
            'type' => '',
            'size' => '',
            'filesize' => '',
            'ext' => '',
            'group' => '',
            'width' => '',
            'height' => ''
        )
    );

    /**
     * Initialize uploader and save attachments.
     *
     * @access public
     * @uses UploaderComponent, S3TransferComponent
     * @param object $Model
     * @param array $settings
     * @return boolean
     */
    public function setup(&$Model, $settings = array()) {
        $this->Uploader = new UploaderComponent();
        $this->S3Transfer = new S3TransferComponent();

        if (!empty($settings) && is_array($settings)) {
            foreach ($settings as $field => $attachment) {
                $this->__attachments[$Model->alias][$field] = $attachment + $this->__defaults;
            }
        }
    }

    /**
     * Deletes any files that have been attached to this model.
     *
     * @access public
     * @param object $Model
     * @return boolean
     */
    public function beforeDelete(&$Model) {
        $data = $Model->read(null, $Model->id);

        if (!empty($data[$Model->alias])) {
            foreach ($data[$Model->alias] as $field => $value) {
                if ($this->__isAS3($value)) {
                    $this->S3Transfer->delete($value);
                } else if (is_file(WWW_ROOT . $value)) {
                    $this->Uploader->delete($value);
                }
            }
        }

        return true;
    }

    /**
     * Before saving the data, try uploading the image, if successful save to database.
     *
     * @access public
     * @param object $Model
     * @return boolean
     */
    public function beforeSave(&$Model) {
        $this->Uploader->initialize($Model);
        $this->Uploader->startup($Model);

        if (!empty($Model->data[$Model->alias])) {
            foreach ($Model->data[$Model->alias] as $file => $data) {
                if (isset($this->__attachments[$Model->alias][$file])) {
                    $attachment = $this->__attachments[$Model->alias][$file];
                    $options = array();
                    $s3 = false;

                    // S3
                    if (!empty($attachment['s3'])) {
                        if (!empty($attachment['s3']['bucket']) && !empty($attachment['s3']['accessKey']) && !empty($attachment['s3']['secretKey'])) {
                            $this->S3Transfer->bucket = $attachment['s3']['bucket'];
                            $this->S3Transfer->accessKey = $attachment['s3']['accessKey'];
                            $this->S3Transfer->secretKey = $attachment['s3']['secretKey'];

                            if (isset($attachment['s3']['useSsl']) && is_bool($attachment['s3']['useSsl'])) {
                                $this->S3Transfer->useSsl = $attachment['s3']['useSsl'];
                            }

                            $this->S3Transfer->startup($Model);
                            $s3 = true;
                        } else {
                            trigger_error('Uploader.Attachment::beforeSave(): To use the S3 transfer, you must supply an accessKey, secretKey and bucket.', E_USER_WARNING);
                        }
                    }

                    // Uploader
                    if (!empty($attachment['uploadDir'])) {
                        $this->Uploader->uploadDir = $attachment['uploadDir'];
                    }

                    if (is_numeric($attachment['maxNameLength'])) {
                        $this->Uploader->maxNameLength = $attachment['maxNameLength'];<commit_msg>Fixing issue #8, cant save record if file field exists. http://github.com/milesj/cake-uploader/issues/closed#issue/8<commit_after><?php
/** 
* Attachment Behavior
*
* A CakePHP Behavior that attaches a file to a model, and uploads automatically, then stores a value in the database.
*
* @author      <NAME> - www.milesj.me
* @copyright   Copyright 2006-2010, <NAME>, Inc.
* @license     http://opensource.org/licenses/mit-license.php - Licensed under The MIT License
* @link        http://milesj.me/resources/script/uploader-plugin
*/

App::import('Component', array('Uploader.Uploader', 'Uploader.S3Transfer'));

class AttachmentBehavior extends ModelBehavior {

    /**
     * AS3 domain snippet.
     */
    const AS3_DOMAIN = 's3.amazonaws.com';

    /**
     * Files that have been uploaded / attached; used for fallback functions.
     *
     * @access private
     * @var array
     */
    private $__attached = array();

    /**
     * All user defined attachments; images => model.
     *
     * @access private
     * @var array
     */
    private $__attachments = array();

    /**
     * The default settings for attachments.
     *
     * @access private
     * @var array
     */
    private $__defaults = array(
        'uploadDir' 	=> null,
        'dbColumn'		=> 'uploadPath',
        'maxNameLength' => null,
        'overwrite'		=> true,
        'name'			=> null,
        'transforms'	=> array(),
        's3'			=> array(),
        'skipSave'      => true,
        'metaColumns'   => array(
            'type' => '',
            'size' => '',
            'filesize' => '',
            'ext' => '',
            'group' => '',
            'width' => '',
            'height' => ''
        )
    );

    /**
     * Initialize uploader and save attachments.
     *
     * @access public
     * @uses UploaderComponent, S3TransferComponent
     * @param object $Model
     * @param array $settings
     * @return boolean
     */
    public function setup(&$Model, $settings = array()) {
        $this->Uploader = new UploaderComponent();
        $this->S3Transfer = new S3TransferComponent();

        if (!empty($settings) && is_array($settings)) {
            foreach ($settings as $field => $attachment) {
                $this->__attachments[$Model->alias][$field] = $attachment + $this->__defaults;
            }
        }
    }

    /**
     * Deletes any files that have been attached to this model.
     *
     * @access public
     * @param object $Model
     * @return boolean
     */
    public function beforeDelete(&$Model) {
        $data = $Model->read(null, $Model->id);

        if (!empty($data[$Model->alias])) {
            foreach ($data[$Model->alias] as $field => $value) {
                if ($this->__isAS3($value)) {
                    $this->S3Transfer->delete($value);
                } else if (is_file(WWW_ROOT . $value)) {
                    $this->Uploader->delete($value);
                }
            }
        }

        return true;
    }

    /**
     * Before saving the data, try uploading the image, if successful save to database.
     *
     * @access public
     * @param object $Model
     * @return boolean
     */
    public function beforeSave(&$Model) {
        $this->Uploader->initialize($Model);
        $this->Uploader->startup($Model);

        if (!empty($Model->data[$Model->alias])) {
            foreach ($Model->data[$Model->alias] as $file => $data) {
                if (isset($this->__attachments[$Model->alias][$file])) {

					// Let the save work even if the image is empty.
					// If the image should be required, use the FileValidation behavior.
					if (empty($data['tmp_name'])) {
						continue;
					}
					
                    $attachment = $this->__attachments[$Model->alias][$file];
                    $options = array();
                    $s3 = false;

                    // S3
                    if (!empty($attachment['s3'])) {
                        if (!empty($attachment['s3']['bucket']) && !empty($attachment['s3']['accessKey']) && !empty($attachment['s3']['secretKey'])) {
                            $this->S3Transfer->bucket = $attachment['s3']['bucket'];
                            $this->S3Transfer->accessKey = $attachment['s3']['accessKey'];
                            $this->S3Transfer->secretKey = $attachment['s3']['secretKey'];

                            if (isset($attachment['s3']['useSsl']) && is_bool($attachment['s3']['useSsl'])) {
                                $this->S3Transfer->useSsl = $attachment['s3']['useSsl'];
                            }

                            $this->S3Transfer->startup($Model);
                            $s3 = true;
                        } else {
                            trigger_error('Uploader.Attachment::beforeSave(): To use the S3 transfer, you must supply an accessKey, secretKey and bucket.', E_USER_WARNING);
                        }
                    }

                    // Uploader
                    if (!empty($attachment['uploadDir'])) {
                        $this->Uploader->uploadDir = $attachment['uploadDir'];
                    }

                    if (is_numeric($attachment['maxNameLength'])) {
                        $this->Uploader->maxNameLength = $attachment['maxNameLength'];
{'fraction_non_alphanumeric': 0.15063291139240506, 'fraction_numerical': 0.007400194741966894, 'mean_word_length': 1.7294711666223757, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 127, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6026452', 'n_tokens_mistral': 2871, 'n_tokens_neox': 2730, 'n_words': 604}