D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home
/
tattooscyy
/
api2
/
app
/
Http
/
Controllers
/
Filename :
PictureController.php
back
Copy
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use JWTAuth; use App\Models\Picture; class PictureController extends Controller { public function __construct() { $this->user = JWTAuth::parseToken()->authenticate(); } /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $pictures = Picture::where('user_id', auth('web')->user()->id)->orderBy('id', 'DESC')->paginate(50); return response()->json($pictures); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { // } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { if($request->file('uploads')){ $pic = \App::make('\App\Utils\Imag')->url($request->file('uploads'), '/uploads/' . auth('web')->user()->id . '/pictures/', time()); $part_picture = '/public/uploads/'.auth('web')->user()->id.'/pictures/'; $big_picture = asset($part_picture.$pic); $small_picture = asset($part_picture.'s_'.$pic); $obj = new Picture; $obj->picture = $big_picture; $obj->picture_small = $small_picture; $obj->user_id = auth('web')->user()->id; $obj->ip = $_SERVER['REMOTE_ADDR']; $obj->save(); return $obj->toArray(); } } /** * Display the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function show($id) { $picture = Picture::find($id); return response()->json($picture); } /** * Show the form for editing the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function edit($id) { // } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { $picture = Picture::where('user_id', auth('web')->user()->id)->find($id); if($request->file('uploads')) { $path_arr = explode('/', $picture->picture); @unlink(public_path().'/uploads/'.auth('web')->user()->id.'/pictures/'.end($path_arr)); @unlink(public_path().'/uploads/'.auth('web')->user()->id.'/pictures/s_'.end($path_arr)); $pic = \App::make('\App\Utils\Imag')->url($request->file('uploads'), '/uploads/pictures/'.auth('web')->user()->id.'/', time()); $part_picture = '/public/uploads/'.auth('web')->user()->id.'/pictures/'; $picture->picture = asset($part_picture.$pic); $picture->small = asset($part_picture.'s_'.$pic); } $picture->type = (isset($request->type))?$request->type:''; $picture->ip = $_SERVER['REMOTE_ADDR']; $picture->save(); return response()->json($picture); } /** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { $picture = Picture::where('user_id', auth('web')->user()->id)->find($id); $path_arr = explode('/', $picture->picture); unlink(public_path().'/uploads/'.auth('web')->user()->id.'/pictures/'.end($path_arr)); unlink(public_path().'/uploads/'.auth('web')->user()->id.'/pictures/s_'.end($path_arr)); $picture->delete(); return response()->json(['message'=>'Object with picture deleted']); } }