<?php

namespace App\Http\Controllers\Api;

use App\Exceptions\ClientSafeException;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class CommonController extends BaseController
{
    public function upload(Request $request)
    {
        $disk = Storage::disk('public');
        $file = $request->file('file');
        $validate = validation_image($file);
        if(!$validate['success']) return $this->safeException($validate['msg']);
        $path = uniqid('admin'. '/' .now()->format('Y-m-d').'/').'.'.$file->getClientOriginalExtension();
        if ($result = $disk->put($path, file_get_contents($file->getRealPath()))) {
            $data = [
                'image' => $path,
                'url' => config('app.url') . 'storage/' .  $path
            ];
            return $this->response($data);
        } else {
            return $this->safeException('上传失败');
        }
    }
}