<?php

namespace app\controller\admin;

use app\extra\basic\Base;
use app\extra\tools\UploadExtend;
use app\middleware\AuthMiddleware;
use LinFly\Annotation\Route\Controller;
use LinFly\Annotation\Route\Middleware;
use LinFly\Annotation\Route\Route;
use support\Request;
use support\Response;
use Tinywan\Storage\Storage;


#[Controller(prefix: "/api/upload"),Middleware(AuthMiddleware::class)]
class Upload extends Base
{


    /**
     * 上传文件
     * @param Request $request
     * @return Response
     */
    #[Route(path: "data",methods: "post")]
    public function upload2image(Request $request): Response
    {
        try {
            $resp = UploadExtend::uploadFile();
            if (!isset($resp[0]['url'])) return errorTrans(40010);
            return successTrans("success.data",[
                "fileName"  => $resp[0]['origin_name'],
                "src"       => $resp[0]['url'],
            ],200);
        } catch (\Throwable $throwable) {
            return error($throwable->getMessage());
        }
    }



    /**
     * 上传文件
     * @param Request $request
     * @return Response
     */
    #[Route(path: "file",methods: "post")]
    public function upload2file(Request $request): Response
    {
        try {
            $resp = UploadExtend::disk(UploadExtend::MODE_LOCAL)->uploadFile();
            if (!isset($resp[0]['url'])) return errorTrans(40010);
            return successTrans("success.data",[
                "fileName"  => $resp[0]['origin_name'],
                "src"       => $resp[0]['url'],
            ],200);
        } catch (\Throwable $throwable) {
            echo $throwable->getFile()."\n";
            echo $throwable->getLine()."\n";
            return error($throwable->getMessage());
        }
    }

}