/** * Saves the original data entry, and clears out any data needing to be overwritten (files and records) * * @param object $model Model using this behavior * @param string $name Name of the AssetBehvior child instance * @access private */ function _saveOriginal(&$model, $name) { $setting = $this->settings[$model->name][$name]; # create original data to save into the database $original = array('foreign_key' => $model->id, 'model' => $model->name, 'field' => $name, 'version' => 'original', 'filename' => null, 'mime' => assetMimeType($model->data[$name]['tmp_name']), 'size' => $model->data[$name]['size']); # include dimensions if a valid file-type (image, swf, etc) list($original['width'], $original['height']) = getimagesize($model->data[$name]['tmp_name']); # if this is not an update and files are set to overwrite if (!isset($setting['overwrite']) || $setting['overwrite'] == true) { # count the existing rows for renaming the file $existing = $model->{$name}->find('all', array('fields' => array('id', 'filename'), 'conditions' => array('model' => $model->name, 'foreign_key' => $model->id, 'field' => $name))); # loop through the existing files and remove each entry foreach (!empty($existing) && is_array($existing) ? $existing : array() as $remove) { assetDelete($model, array($remove[$name]['filename'])); } # delete all existing records for this field $model->{$name}->deleteAll(array('model' => $model->name, 'foreign_key' => $model->id, 'field' => $name)); } # set the filename field value $original['filename'] = assetFileName($model->data[$name]['name'], 'original', $name); # create new entry $model->{$name}->create(); # save the original file data if (!$model->{$name}->create() || !$model->{$name}->save($original, false)) { $model->{$name}->delete($original['parent_id']); $this->_setError($model, 'Critical Error found while saving record in afterSave: ' . $name, 'error'); return false; } # update the parent_id $model->{$name}->saveField('parent_id', $model->{$name}->id); # create the directory path for this asset if (!assetDirectoryCreate($model)) { $this->_setError($model, 'Storage directory was not created, file not properly uploaded. Please try again and/or contact an Administrator.'); return false; } $this->directory = assetDirectoryPath($model); # move the original file if (!assetSaveFile($model, $model->data[$name]['tmp_name'], $original['filename'])) { $this->_setError($model, 'Critical Error found while saving file in afterSave: ' . $name, 'error'); return false; } # save the original filename unset($model->data[$name]); $model->data[$name]['filename'] = $original['filename']; $model->data[$name]['type'] = $original['mime']; return true; }
/** * Saves the newly renamed file into the previously created directory * * @param object $model Model using this behavior * @param string $source Full file path of the source file to be moved or copied * @param string $filename New name of the created file * @param mixed $upload Tmp files are moved (uploaded), while duplicate files are copied (for resizing purposes) * @access private */ function assetSaveFile(&$model, $source, $filename, $upload = true) { # create the desintation $destination = assetDirectoryPath($model) . $filename; switch (true) { case is_uploaded_file($source): if (!move_uploaded_file($source, $destination)) { return false; } break; case $upload == false: if (!copy($source, $destination)) { return false; } break; case $upload == 'move': if (!rename($source, $destination)) { return false; } break; case !is_uploaded_file($source): return assetSaveFile($model, $source, $filename, 'move'); break; } # determine if the file exists if (!file_exists($destination)) { return false; } # change the permissions if (!chmod($destination, 0755)) { return false; } # nothing failed return true; }