<?php

namespace App\Services;

use Exception;
use OSS\Core\OssException;
use OSS\OssClient;

class UploadService
{
    public function upload($file)
    {
        try {
            $type_arr = ['text/html', 'text/htm'];
            if (in_array($file->getMimeType(), $type_arr)) return false;
            $size = $file->getSize();
            if ($size > 10 * 1024 * 1000) return false;
//            dd($file->getClientOriginalName());
            $path = Request()->path ? Request()->path : date('Y') . date('m') . "/" . date('d');
            $object = $path . "/" . $file->getClientOriginalName();
            $file_url = 'https://' . config('alioss.domain') . '/' . $object;
            $access_key = config('alioss.id');
            $access_secret = config('alioss.secret');
            $endpoint = config('alioss.endpoint');
            $ossClient = new OssClient($access_key, $access_secret, $endpoint);
            $ossClient->uploadFile(config('alioss.bucket'), $object, $file->getPathname());
            return $file_url;
        } catch (OssException $e) {
            throw new \Exception($e->getMessage());
        }
    }

    //文件地址上传
    public function uploadFile($file){
        $access_key = config('alioss.id');
        $access_secret = config('alioss.secret');
        $endpoint = config('alioss.endpoint');
        $ossClient = new OssClient($access_key, $access_secret, $endpoint);
        //生成file
        $object = date('Y').date('m')."/".date('d')."/".basename($file);
        $url = 'https://'.config('alioss.domain').'/'.$object;
        try {
            // $result = $ossClient->putObject(config('alioss.buckets.picture'), $object, $file);
            $result = $ossClient->uploadFile(config('alioss.bucket'), $object, $file);
        } catch(Exception $e) {
            // return $this->failure('oss_put_failure', $e->getMessage());
        }

        return $url;
    }

    public function getOssDir($dir) {
        $access_key = config('alioss.id');
        $access_secret = config('alioss.secret');
        $endpoint = config('alioss.endpoint');
        $bucket = config('alioss.bucket');
        $ossClient = new OssClient($access_key, $access_secret, $endpoint);
        $options = array(
            'max-keys' => 1000, // 最大返回的object数量，默认为100，最大为1000
            'delimiter' => '/', // 分隔符，用来对返回的keys进行分组，如果有Prefix，则将Prefix后面的第一个分隔符及其后面的内容作为Key的前缀进行分组，如果没有Prefix，则从Bucket根目录开始
            "prefix"=>$dir,
        );
        $listObjectInfo = $ossClient->listObjects($bucket,  $options);
        if ($listObjectInfo->getPrefixList() && !empty($listObjectInfo->getPrefixList())) {
            $arr = [];
            foreach ($listObjectInfo->getPrefixList() as $prefix) {
                $arr[] = $prefix->getPrefix();
            }
            return $arr;
        } else {
           return [];
        }
    }

    public function getOssFile($dir) {
        $access_key = config('alioss.id');
        $access_secret = config('alioss.secret');
        $endpoint = config('alioss.endpoint');
        $bucket = config('alioss.bucket');
        $ossClient = new OssClient($access_key, $access_secret, $endpoint);
        $options = array(
            'max-keys' => 1000, // 最大返回的object数量，默认为100，最大为1000
            'delimiter' => '/', // 分隔符，用来对返回的keys进行分组，如果有Prefix，则将Prefix后面的第一个分隔符及其后面的内容作为Key的前缀进行分组，如果没有Prefix，则从Bucket根目录开始
            "prefix"=>$dir,
        );
        $listObjectInfo = $ossClient->listObjects($bucket,  $options);
        if ($listObjectInfo->getObjectList() && !empty($listObjectInfo->getObjectList())) {
            $arr = [];
            foreach ($listObjectInfo->getObjectList() as $object) {
                if ($object->getKey() == $dir) continue;
                $arr[] = $object->getKey();
            }
            return $arr;
        } else {
           return [];
        }
    }

}
