Ejemplo n.º 1
0
 /**
  * file upload for photo albums or any possible model
  * if model is Album and the id is 'new', the user can create
  *
  * if any other model, the user has to be authorized
  *
  * @param $model
  * @param $model_id
  * @param $id
  *
  * @return string
  */
 public function fileUploads($model, $model_id, $id)
 {
     Helper::allow('album-new');
     $this->photos = new Photo();
     $this->_authorize($model, $model_id, $id);
     $models = str_plural($model);
     $files = array('files' => Input::file('files'));
     $rules = ['files.*.*' => 'required|mimes:png,jpeg,jpg,gif|max:8000'];
     $validation = Validator::make(['files' => $this->request->file('files')], $rules);
     $json = [];
     foreach ($files['files'] as $file) {
         $fileName = str_replace(' ', '-', strtolower($file->getClientOriginalName()));
         $json = ['name' => $fileName, 'size' => $file->getClientSize()];
         if (count($validation->errors()) > 0) {
             $json["error"] = $validation->errors()->first();
         }
         $ids = $this->album->id . '_' . Auth::id() . '_';
         $imgTitle = $ids . time() . '_' . $fileName;
         $imagePath = storage_path() . '/app/public/' . $models . '/';
         $json['name'] = $imgTitle;
         //Input::file('image')->move($imagePath, $imgTitle);
         $upload_success = Image::make($file)->resize(800, null, function ($constraint) {
             $constraint->aspectRatio();
             $constraint->upsize();
         })->save($imagePath . $imgTitle);
         if ($upload_success) {
             $this->photos->title = $this->photos->location = $imgTitle;
             $this->album->photos()->save($this->photos);
             //DB::table('albums')->where('id', $this->album->id)->update($this->album->toArray());
             $json += ['url' => '/img/cache/wm-rt-large/' . $imgTitle, 'size' => Storage::size('/public/' . $models . '/' . $imgTitle), 'deleteUrl' => '/img/delete/sites/' . $imgTitle, 'deleteType' => 'GET'];
         } else {
             $json["error"] = "Something went wrong.";
         }
     }
     return response()->json(['files' => [$json], 'album_id' => $this->album->id, 'session' => session('album_id')]);
 }