<?php

namespace App\Http\Controllers;

use File;
use Illuminate\Http\Request;
use Storage;

class StorageController extends Controller
{
    public function index(Request $request)
    {
        $File    = $request->file('image');
        $oldName = $File->getClientOriginalName();
        $hash    = File::hash($File->path());
        $size    = File::size($File->path());
        if ($size > (10 * 1024 * 1024)) {
            return [
                'code'    => 0,
                'message' => '文件不能超过10M',
            ];
        }
        $pathUrl  = 'images' . date('/Y-m/d');
        $fileName = $hash . '.' . $File->getClientOriginalExtension();
        $path     = Storage::disk('public')->putFileAs(
            $pathUrl, $File, $fileName
        );
        if (!$path) {
            return [
                'code'    => 0,
                'message' => '文件上传失败',
            ];
        }

        return [
            'code'    => 1,
            'message' => '文件上传成功',
            'path'    => '/' . $pathUrl . '/' . $fileName,
        ];
    }
}
