<?php

namespace App\Utils;

use Illuminate\Http\UploadedFile;
use Intervention\Image\Facades\Image;
use OSS\Core\OssException;
use OSS\OssClient;
use Illuminate\Support\Str;

class UploadUtils
{
    private static $ossClient;
    private static $bucket = 'ct-upimg';
    private static $prefix = 'ju8hn6/erp/shop/';


    public static function init()
    {
        if (static::$ossClient === null) {
            static::$ossClient = new OssClient(config('filesystems.disks.aliyun.access_id'), config('filesystems.disks.aliyun.access_key'), config('filesystems.disks.aliyun.endpoint'));
        }

        if (static::$bucket === null) {
            static::$bucket = config('filesystems.disks.aliyun.bucket');
        }
    }

    public static function putForUploadedResizeImage($bucketPath, UploadedFile $upload, $width = 800, $extension = null)
    {
        if ($upload->extension() != 'gif') {
            // 压缩图片
            $originalName = static::randomFileName($extension ?: $upload->getClientOriginalExtension());
            $storagePath = storage_path('app/public/file/') . $originalName;
            $img = Image::make($upload->getRealPath());

            // 未传宽度 大于800压缩至800 否则按照原宽度
            $width = $img->width() >= 800 ? 800 : $img->width();
            // 高度自适应
            $img->resize($width, null, function ($constraint) {
                $constraint->aspectRatio();
            });
            $img->save($storagePath);
            $upload = new UploadedFile($storagePath, $originalName);
        }

        // 上传并删除压缩图片
        try {
            $filePath = static::put($bucketPath, $upload->getRealPath(), $upload->getClientOriginalExtension());
        } catch (OssException $e) {
            throw $e;
        } finally {
            if (isset($storagePath)) {
                unlink($storagePath);
            }
        }

        return $filePath;
    }

    //上传图片
    public static function putForUploadedFile($bucketPath, UploadedFile $upload)
    {
        return static::put($bucketPath, $upload->getRealPath(), $upload->getClientOriginalExtension());
    }

    public static function put($bucketPath, $filepath, $ext)
    {
        static::init();
        try {
            $filePath = static::$prefix . $bucketPath . '/' . static::randomFileName($ext);
            static::$ossClient->uploadFile(static::$bucket, $filePath, $filepath);
        } catch (OssException $e) {
            throw $e;
        }
        return $filePath;
    }

    public static function buildFilePath()
    {
        return date('Y') . '/' . date('m') . '/' . date('d');
    }

    public static function randomFileName($ext, $len = 40)
    {
        return Str::random($len) . '.' . $ext;
    }

    //删除修改前，上传修改后图片
    public static function delAndUploadImage($file, $filePath, UploadedFile $upload)
    {
        static::delFile($file);
        return static::putForUploadedFile($filePath, $upload);
    }

    //删除修改前，上传修改后视频
    public static function delAndUploadVideo($file, $filePath, UploadedFile $upload)
    {
        static::delFile($file);
        return static::putForUploadedFile($filePath, $upload);
    }

    public static function delFile($file)
    {
        static::init();
        if (static::exist($file)) {
            return static::$ossClient->deleteObject(static::$bucket, $file);
        }
        return false;
    }

    public static function exist($file)
    {
        static::init();
        return static::$ossClient->doesObjectExist(static::$bucket, $file);
    }

    public static function getFullImgUrl($filePath)
    {
        return config('filesystems.disks.aliyun.url') . $filePath;
    }

}
