FileService.php 3.93 KB
<?php
namespace Resource;
use Yii;
use yii\base\Exception;
use yii\log\Logger;


/**
 * Class FileService 和ImageServer 是一样的,只是命名不一样
 * @package resource
 */
class FileService
{

    /** @var **/
    protected $serviceProvider;

    /** @var string **/
    public $name = 'OSS';

    /**
     * @param \yii\web\Application $app
     */
    public function __construct()
    {
        if('OSS' == $this->name){
            $this->options = \yii::$app->params['ossOptions'];
            $serviceProvider = new ServiceProviderOss($this->options);
            $this->setServiceProvider($serviceProvider);
        }
    }

    /**
     * @param $path
     * @param $destPath
     * @return mixed
     */
    public function setServiceProvider(ServiceProviderAbstract $serviceProvider)
    {
        $this->serviceProvider = $serviceProvider;
    }

    /**
     * @param $path
     * @param $destPath
     * @return mixed
     */
    public function add($path, $destPath)
    {
        return $this->serviceProvider->add($path, $destPath);
    }

    /**
     * @param $path
     * @param null $style
     * @return mixed
     */
    public function getUrl($path, $style = null)
    {
        return $this->serviceProvider->getUrl($path, $style);
    }

    /**
     * @param $path
     * @return mixed
     */
    public  function delete($path)
    {
        return $this->serviceProvider->delete($path);
    }

    /**
     * @param $oldPath
     * @param $newPath
     * @return mixed
     */
    public  function replace($oldPath, $newPath, $tmpPath)
    {
        return $this->serviceProvider->replace($oldPath, $newPath, $tmpPath);
    }

    /**
     * @param $path
     * @return mixed
     */
    public  function exists($path)
    {
        return $this->serviceProvider->exists($path);
    }

    /**
     * @param $oldPath
     * @param $newPath
     * @return mixed
     */
    public  function move($oldPath, $newPath)
    {
        return $this->serviceProvider->move($oldPath, $newPath);
    }

    /**
     * @param $paths
     * @return mixed
     */
    public  function batchDelete($paths)
    {
        return $this->serviceProvider->batchDelete($paths);
    }

    /** 测试用 */
    public function getProvider()
    {
         return $this->serviceProvider;
    }

    /** 获取指定目录下面的所有文件和文件夹
     * @param string $dir
     * @param int $maxKeys
     * @return array|bool|null
     */
    public function getDirFiles($dir = '', $maxKeys = 20)
    {
        $serviceProvider = $this->serviceProvider;
        if (!$serviceProvider) {
            return null;
        }
        if (empty($dir)) {
            return false;
        }
        $returnList = ['files' => [], 'directories' => []];
        $ossClient = $serviceProvider->getClient();
        $delimiter = '/';
        $nextMarker = '';

        $options = array(
            'delimiter' => $delimiter,
            'prefix' => $dir,
            'max-keys' => $maxKeys,
            'marker' => $nextMarker,
        );
        $ossConfig = $this->options;
        $bucket = $ossConfig['bucket'];
        try {
            $listObjectInfo = $ossClient->listObjects($bucket, $options);
            $objectList = $listObjectInfo->getObjectList(); // object list
            $prefixList = $listObjectInfo->getPrefixList(); // directory list
            if (!empty($objectList)) {
                foreach ($objectList as $objectInfo) {
                    $returnList['files'][] = [$objectInfo->getKey(), $objectInfo->getLastModified(), 'type' => $objectInfo->getType()];
                }
            }
            if (!empty($prefixList)) {
                foreach ($prefixList as $prefixInfo) {
                    $returnList['directories'][] = $prefixInfo->getPrefix();
                }
            }
            return $returnList;
        } catch (Exception $e) {
            Yii::getLogger()->log($e->getMessage(), Logger::LEVEL_ERROR);
            return false;
        }

    }
}