/**
  * Upload Handler
  * 
  * @param array $upload
  * @param array $config
  * @throws UploadException
  */
 protected function _upload($upload, $config)
 {
     // validate upload error
     if ($upload['error'] > 0) {
         throw new UploadException($upload['error']);
     }
     // check upload dir
     if (!is_dir($config['uploadDir']) || !is_writeable($config['uploadDir'])) {
         //$upload['error'] = UPLOAD_ERR_CANT_WRITE;
         debug('MediaUploader: Upload directory is not writeable (' . $config['uploadDir'] . ')');
         throw new UploadException(UPLOAD_ERR_CANT_WRITE);
     }
     // validate size
     if ($upload['size'] < $config['minFileSize']) {
         throw new UploadException(self::UPLOAD_ERR_MIN_FILE_SIZE);
     } elseif ($upload['size'] > $config['maxFileSize']) {
         throw new UploadException(self::UPLOAD_ERR_MAX_FILE_SIZE);
     } elseif (!MediaTools::validateMimeType($upload['type'], $config['allowedMimeType'])) {
         throw new UploadException(self::UPLOAD_ERR_MIME_TYPE);
     }
     // split basename
     list($filename, $ext, $dotExt) = MediaTools::splitBasename(trim($upload['name']));
     //validate extension
     if (!MediaTools::validateFileExtension($ext, $config['allowedFileExtension'])) {
         throw new UploadException(self::UPLOAD_ERR_FILE_EXT);
     }
     // filename
     $filename = Inflector::slug($filename, $config['slug']);
     if ($config['hashFilename']) {
         $filename = sha1($filename);
     }
     $filename = uniqid($filename . '_');
     $basename = $filename . $dotExt;
     $path = $config['uploadDir'];
     //build targetname
     $target = $path . $basename;
     if (file_exists($target) && $config['allowOverwrite'] == false) {
         $i = 0;
         $_filename = $filename;
         do {
             $filename = $_filename . '_' . ++$i;
             $basename = $filename . $dotExt;
             $target = $path . $basename;
         } while (file_exists($target) == true);
     }
     debug("Uploading file to " . $target);
     //move uploaded file to tmp upload dir
     //TODO use a file engine here. Something like $this->_engine->storeTemporaryUpload($upload);
     if (is_uploaded_file($upload['tmp_name'])) {
         if (!move_uploaded_file($upload['tmp_name'], $target)) {
             throw new UploadException(self::UPLOAD_ERR_STORE_UPLOAD);
         }
     } elseif (!copy($upload['tmp_name'], $target)) {
         throw new UploadException(self::UPLOAD_ERR_STORE_UPLOAD);
     }
     return array('name' => $upload['name'], 'type' => $upload['type'], 'size' => $upload['size'], 'path' => $target, 'basename' => $basename, 'filename' => $filename, 'ext' => $ext, 'dotExt' => $dotExt);
 }
 /**
  * Convert field values to Attachment data set
  *
  * @param string|array $value Array or comma-separated string
  * @param array $config Field config
  * @return array
  */
 protected function _parseAttachments(Model &$model, $basenames = array(), $config)
 {
     if (is_string($basenames)) {
         $basenames = explode(',', $basenames);
     }
     $attachments = array();
     foreach ((array) $basenames as $basename) {
         if (strlen(trim($basename)) == 0) {
             continue;
         }
         // ignore urls
         if (preg_match('@\\:\\/\\/@', $basename)) {
             $path = $basename;
             $ext = $dotExt = $filename = $basename = null;
         } else {
             $path = self::getPath($model, $config) . $basename;
             list($filename, $ext, $dotExt) = MediaTools::splitBasename($basename);
         }
         $attachment = compact('basename', 'filename', 'path', 'ext', 'dotExt');
         array_push($attachments, $attachment);
     }
     return $attachments;
 }