/**
  * Main media upload API
  */
 protected function postMedia($mediaType = 'user', $item_id = 0)
 {
     // Get corresponding item
     switch ($mediaType) {
         case 'message':
             $item = Alert::find($item_id);
             $allowedTypes = array('alert_picture');
             break;
         case 'user':
             $item = User::find($item_id);
             $allowedTypes = array('profile_picture', 'cover_picture');
             break;
         case 'brand':
             $item = Brand::find($item_id);
             $allowedTypes = array('logo_picture', 'cover_picture');
             break;
         default:
             throw new Exception('Invalid media type:' . $mediaType);
             break;
     }
     if (!$item) {
         App::abort(404);
     }
     // Gathering and validate upload information
     $uploadFiles = Input::file();
     $uploadType = key($uploadFiles);
     if (!Input::hasFile($uploadType)) {
         array_shift($uploadFiles);
         $uploadType = key($uploadFiles);
     }
     if (!in_array($uploadType, $allowedTypes)) {
         return Redirect::back()->with('warning', 'Invalid upload name : ' . $uploadType);
     }
     // Early mime validation
     $validType = false;
     if ($mime = Input::file($uploadType)->getMimetype()) {
         $validType = strpos($mime, 'image') === 0;
     }
     if (!$validType) {
         return Redirect::back()->with('warning', 'Invalid mime type : ' . $mime);
     }
     // Prepare uploader
     $fs = new Filesystem();
     $storage = new UploadFileSystem($this->media_directory);
     $file = new UploadFile($uploadType, $storage);
     // Set to item's media slug
     $mediaName = $uploadType . '_' . $mediaType . '_' . $item->id;
     $file->setName($mediaName);
     // Validate file upload
     // MimeType List => http://www.webmaster-toolkit.com/mime-types.shtml
     $file->addValidations(array(new UploadMimetype($mime), new UploadSize('5M')));
     // Access data about the file that has been uploaded
     $data = array('path' => $this->media_directory . '/' . $file->getNameWithExtension(), 'name' => $file->getNameWithExtension(), 'extension' => $file->getExtension(), 'mime' => $file->getMimetype(), 'size' => $file->getSize(), 'md5' => $file->getMd5(), 'dimensions' => $file->getDimensions());
     // Try to upload file
     try {
         // If it already there, remove
         if ($fs->exists($data['path'])) {
             $fs->delete($data['path']);
         }
         $file->upload();
         chmod($data['path'], 0777);
         //why not 0644 or 0664
         $attachment = Attachment::create(array('mime' => $data['mime'], 'path' => $data['path'], 'url' => URL::to('/media/' . $mediaType . '/' . $uploadType . '/' . $item->id)));
         foreach ($item->attachments as $previousAttachment) {
             if ($previousAttachment->pivot->type == $uploadType) {
                 $item->attachments()->detach($previousAttachment->id);
             }
         }
         $item->attachments()->save($attachment, array('type' => $uploadType));
         return Redirect::back();
     } catch (Exception $e) {
         // Fail!
         $error = $file->getErrors();
         return Redirect::back()->with('warning', current($error));
     }
 }