コード例 #1
0
ファイル: asset.php プロジェクト: awd/assets
 /**
 * 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;
 }
コード例 #2
0
ファイル: common.test.php プロジェクト: awd/assets
 /**
  * test file name creation
  * 
  * @return void
  */
 function testAssetFileName()
 {
     $result = assetFileName("/tmp/fake_file.jpg", "original", null);
     $this->assertTrue(preg_match("/fake_file\\.(.*)\\.jpg/", $result));
     $result = assetFileName("/tmp/fake_file.jpg", "original", null);
     $this->assertTrue(preg_match("/fake_file\\.([a-zA-Z0-9]+)\\.jpg/", $result));
     $result = assetFileName("/tmp/fake_file.jpg", "original", null);
     $this->assertFalse(preg_match("/fake_file\\.(![a-zA-Z0-9]+).jpg/", $result));
     $result = assetFileName("/tmp/fake_file.666.jpg", "thumbnail", null);
     $this->assertTrue(preg_match("/thumbnail\\.fake_file\\.666\\.jpg/", $result));
     $result = assetFileName("/tmp/fake_file.jpg", "original", "key");
     $this->assertTrue(preg_match("/key_fake_file\\.(.*)\\.jpg/", $result));
 }