示例#1
0
 public static function createFromUpload(array $file_upload, $options = array())
 {
     if (!isset($file_upload['error']) || is_array($file_upload['error'])) {
         throw new \Exception('Invalid Upload');
     }
     switch ($file_upload['error']) {
         case UPLOAD_ERR_OK:
             break;
         case UPLOAD_ERR_NO_FILE:
             throw new \Exception('No file sent.');
         case UPLOAD_ERR_INI_SIZE:
         case UPLOAD_ERR_FORM_SIZE:
             throw new \Exception('Exceeded filesize limit.');
         default:
             throw new \Exception('Unknown errors.');
     }
     if (empty($file_upload['tmp_name']) || empty($file_upload['name'])) {
         throw new \Exception('Invalid Upload Properties');
     }
     if (empty($file_upload['size'])) {
         throw new \Exception('Invalid Upload Size');
     }
     $app = \Base::instance();
     $options = $options + array('width' => 460, 'height' => 308);
     // Do the upload
     $model = new static();
     $grid = $model->getDb()->getGridFS($model->collectionNameGridFS());
     $file_path = $model->inputFilter()->clean($file_upload['tmp_name']);
     $name = $model->inputFilter()->clean($file_upload['name']);
     //$buffer = file_get_contents($file_upload['tmp_name']);
     //TODO MOVE THIS TO A LISTENER
     $image = new \Dsc\Image($file_upload['tmp_name']);
     $cropped = $image->cropResize(250, 250);
     $cropped = $cropped->crop(250, 250);
     $buffer = $cropped->toBuffer();
     $options['name'] = $file_upload['name'];
     return static::createFromBuffer($buffer, $options);
 }
示例#2
0
 /**
  * Creates an asset directly from a URL
  *
  * @param unknown $url
  * @param unknown $options
  * @return multitype:string NULL boolean
  */
 public static function createFromUrl($url, $options = array())
 {
     $options = $options + array('width' => 460, 'height' => 308);
     $app = \Base::instance();
     $web = \Web::instance();
     $request = array();
     $result = array();
     $request = $web->request($url);
     if (!empty($request['body'])) {
         $model = new static();
         $db = $model->getDb();
         $grid = $db->getGridFS($model->collectionNameGridFS());
         $url_path = parse_url($url, PHP_URL_PATH);
         $pathinfo = pathinfo($url_path);
         $filename = $model->inputfilter()->clean($url_path);
         $buffer = $request['body'];
         $originalname = str_replace("/", "-", $filename);
         $thumb = null;
         if ($thumb_binary_data = $model->getThumb($buffer, null, $options)) {
             $thumb = new \MongoBinData($thumb_binary_data, 2);
         }
         $title = \Dsc\String::toSpaceSeparated($model->inputFilter()->clean($originalname));
         $values = array('storage' => 'gridfs', 'contentType' => $model->getMimeType($buffer), 'md5' => md5($filename), 'thumb' => $thumb, 'url' => null, 'title' => $title, "filename" => $filename, "source_url" => $url);
         $model->bind($values);
         $values['slug'] = $model->generateSlug();
         $values['url'] = "/asset/" . $values['slug'];
         // save the file
         if ($storedfile = $grid->storeBytes($buffer, $values)) {
             $model->load(array('_id' => $storedfile));
             $model->bind($values);
             if (!empty($options['tags'])) {
                 $model->set('tags', $options['tags']);
             }
             if ($model->save()) {
             }
         }
         // $storedfile has newly stored file's Document ID
         $result["asset_id"] = (string) $storedfile;
         $result["slug"] = $model->{'slug'};
         $result['error'] = false;
     } else {
         $result['error'] = true;
         $result['message'] = 'Could not download asset from provided URL';
     }
     return $result;
 }
示例#3
0
 /**
  * This is static so you can do 
  * YourModel::collection()->find() or anything else with the MongoCollection object
  */
 public static function collection()
 {
     $item = new static();
     return $item->getDb()->selectCollection($item->collectionName());
 }