/**
  * Store a newly created resource in storage.
  *
  * @return Response
  */
 public function store()
 {
     $rules = ['attachment' => 'required|mimes:jpeg,png,pdf,docx,xlsx,pptx,txt,zip,rar|max:10000', 'name' => 'max:255', 'media_category_id' => 'exists:media_category,id'];
     $validator = Validator::make(Input::only(array_keys($rules)), $rules);
     if ($validator->fails()) {
         throw new ResourceException($validator->errors()->first());
     }
     DB::beginTransaction();
     try {
         $media = new Media();
         if (Input::has('media_category_id')) {
             $media->media_category_id = Input::get('media_category_id');
         }
         $media->name = Input::get('name', '');
         $file = Input::file('attachment');
         $extension = $file->getClientOriginalExtension();
         $media->filesize = $file->getSize();
         $media->mime = $file->getClientMimeType();
         $media->key = strtolower(md5(uniqid($media->id . rand()))) . '.' . $extension;
         $media->path = $this->generateDateFolder();
         //upload file
         Storage::put('uploads/m/' . $media->path . $media->key, File::get($file));
         $media->save();
         DB::commit();
         return $this->show($media->id);
     } catch (\Exception $e) {
         DB::rollback();
         throw new ResourceException('Invalid request parameters');
     }
 }
Ejemplo n.º 2
0
 public function editWish($id)
 {
     //  return response()->json(['result' => 'success', 'data' => $text], 200);
     $wish = Wish::where('id', $id)->first();
     if ($wish) {
         $wish->name = Input::get('name');
         $wish->description = Input::get('description');
         $wish->category_id = Input::get('category')['id'];
         $wish->link = Input::get('link');
         $text = Input::get("photo");
         if (isset($text['$ngfDataUrl'])) {
             $destinationPath = 'images/wishes/';
             $data = $text['$ngfDataUrl'];
             list($type, $data) = explode(';', $data);
             list(, $type) = explode('/', $type);
             list(, $data) = explode(',', $data);
             $fileName = md5(date("YmdHis") . rand(5, 50)) . '.' . $type;
             $data = base64_decode($data);
             $media = Media::where('id', $wish->media_id)->first();
             if (file_put_contents($destinationPath . $fileName, $data)) {
                 if ($media->id != 3) {
                     $tmp = $media->name;
                     $media->name = URL::to('/') . "/" . $destinationPath . $fileName;
                     $media->save();
                     $arr = explode("/", $tmp);
                     unlink($destinationPath . $arr[count($arr) - 1]);
                 } else {
                     $media = new Media();
                     $media->name = URL::to('/') . "/" . $destinationPath . $fileName;
                     $media->save();
                     $wish->media_id = $media->id;
                 }
             }
         }
         $wish->save();
         return response()->json(['result' => 'success'], 200);
     } else {
         return response()->json(['result' => 'error'], 200);
     }
 }
Ejemplo n.º 3
0
 public function setPhoto()
 {
     if (Auth::check()) {
         $text = Input::get('file');
         $destinationPath = 'images/users/';
         $data = $text['$ngfDataUrl'];
         list($type, $data) = explode(';', $data);
         list(, $type) = explode('/', $type);
         list(, $data) = explode(',', $data);
         $fileName = md5(date("YmdHis") . rand(5, 50)) . '.' . $type;
         $data = base64_decode($data);
         $media = Media::where('id', Auth::user()->media_id)->first();
         if (file_put_contents($destinationPath . $fileName, $data)) {
             if ($media->id != 1) {
                 $tmp = $media->name;
                 $media->name = URL::to('/') . "/" . $destinationPath . $fileName;
                 $media->save();
                 $arr = explode("/", $tmp);
                 unlink($destinationPath . $arr[count($arr) - 1]);
             } else {
                 $media = new Media();
                 $media->name = URL::to('/') . "/" . $destinationPath . $fileName;
                 $media->save();
                 $user = Auth::user();
                 $user->media_id = $media->id;
                 $user->save();
             }
             return $this->getProfile();
         } else {
             return response()->json(['result' => 'error'], 200);
         }
     } else {
         return response()->json(['result' => 'error'], 200);
     }
 }