Skip to content

Commit

Permalink
更新
Browse files Browse the repository at this point in the history
  • Loading branch information
xutl committed Mar 20, 2022
1 parent f5c3d2c commit 6cdbb90
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 97 deletions.
18 changes: 0 additions & 18 deletions phpunit.xml.dist

This file was deleted.

171 changes: 92 additions & 79 deletions src/AliyunOSSAdapter.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
<?php
/**
* This is NOT a freeware, use is subject to license terms
* @copyright Copyright (c) 2010-2099 Jinan Larva Information Technology Co., Ltd.
* @link http://www.larva.com.cn/
*/

namespace Larva\Flysystem\Oss;

Expand All @@ -13,6 +8,7 @@
use League\Flysystem\FilesystemAdapter;
use League\Flysystem\FilesystemException;
use League\Flysystem\FilesystemOperationFailed;
use League\Flysystem\InvalidVisibilityProvided;
use League\Flysystem\PathPrefixer;
use League\Flysystem\StorageAttributes;
use League\Flysystem\UnableToCheckExistence;
Expand All @@ -33,7 +29,7 @@
use Throwable;

/**
* 阿里云适配器
* 阿里云 OSS 适配器
*/
class AliyunOSSAdapter implements FilesystemAdapter
{
Expand Down Expand Up @@ -110,9 +106,9 @@ public function __construct(OssClient $client, string $bucket, string $prefix =
}

/**
* 判断文件是否存在
* @param string $path
* @return bool
* 判断文件对象是否存在
*
* @throws FilesystemException
* @throws UnableToCheckExistence
*/
public function fileExists(string $path): bool
Expand All @@ -125,9 +121,10 @@ public function fileExists(string $path): bool
}

/**
* 判断文件夹是否存在
* @param string $path
* @return bool
* 判断目录是否存在
*
* @throws FilesystemException
* @throws UnableToCheckExistence
*/
public function directoryExists(string $path): bool
{
Expand All @@ -142,10 +139,9 @@ public function directoryExists(string $path): bool
}

/**
* 写入一个文件
* @param string $path
* @param string $contents
* @param Config $config
* 写入文件到对象
*
* @throws UnableToWriteFile
* @throws FilesystemException
*/
public function write(string $path, string $contents, Config $config): void
Expand All @@ -154,6 +150,8 @@ public function write(string $path, string $contents, Config $config): void
}

/**
* 上传
*
* @param string $path
* @param string|resource $body
* @param Config $config
Expand Down Expand Up @@ -204,10 +202,11 @@ private function createOptionsFromConfig(Config $config): array
}

/**
* 写入文件
* @param string $path
* @param $contents
* @param Config $config
* 将流写入对象
*
* @param resource $contents
*
* @throws UnableToWriteFile
* @throws FilesystemException
*/
public function writeStream(string $path, $contents, Config $config): void
Expand All @@ -216,9 +215,10 @@ public function writeStream(string $path, $contents, Config $config): void
}

/**
* 读取文件
* @param string $path
* @return string
* 读取对象
*
* @throws UnableToReadFile
* @throws FilesystemException
*/
public function read(string $path): string
{
Expand All @@ -231,9 +231,12 @@ public function read(string $path): string
}

/**
* 读取流
* @param string $path
* 以流的形式读取对象
*
* @return resource
*
* @throws UnableToReadFile
* @throws FilesystemException
*/
public function readStream(string $path)
{
Expand All @@ -250,7 +253,9 @@ public function readStream(string $path)

/**
* 删除对象
* @param string $path
*
* @throws UnableToDeleteFile
* @throws FilesystemException
*/
public function delete(string $path): void
{
Expand All @@ -264,7 +269,9 @@ public function delete(string $path): void

/**
* 删除目录
* @param string $path
*
* @throws UnableToDeleteDirectory
* @throws FilesystemException
*/
public function deleteDirectory(string $path): void
{
Expand All @@ -284,9 +291,9 @@ public function deleteDirectory(string $path): void
}

/**
* 创建文件夹
* @param string $path
* @param Config $config
* 创建目录
*
* @throws UnableToCreateDirectory
*/
public function createDirectory(string $path, Config $config): void
{
Expand All @@ -299,9 +306,10 @@ public function createDirectory(string $path, Config $config): void
}

/**
* 设置访问策略
* @param string $path
* @param string $visibility
* 设置对象可见性
*
* @throws InvalidVisibilityProvided
* @throws FilesystemException
*/
public function setVisibility(string $path, string $visibility): void
{
Expand All @@ -313,9 +321,10 @@ public function setVisibility(string $path, string $visibility): void
}

/**
* 获取访问策略
* @param string $path
* @return FileAttributes
* 获取对象可见性
*
* @throws UnableToRetrieveMetadata
* @throws FilesystemException
*/
public function visibility(string $path): FileAttributes
{
Expand All @@ -329,7 +338,7 @@ public function visibility(string $path): FileAttributes
}

/**
* 获取文件 MetaData
* 获取对象 MetaData
* @param string $path
* @param string $type
* @return FileAttributes|null
Expand All @@ -339,7 +348,7 @@ private function fetchFileMetadata(string $path, string $type): ?FileAttributes
try {
$meta = $this->client->getObjectMeta($this->bucket, $this->prefixer->prefixPath($path));
} catch (Throwable $exception) {
throw UnableToRetrieveMetadata::create($path, $type, $exception->getErrorMessage(), $exception);
throw UnableToRetrieveMetadata::create($path, $type, $exception->getMessage(), $exception);
}
$attributes = $this->mapObjectMetadata($meta, $path);
if (!$attributes instanceof FileAttributes) {
Expand Down Expand Up @@ -387,9 +396,10 @@ private function extractExtraMetadata(array $metadata): array
}

/**
* 获取内容类型
* @param string $path
* @return FileAttributes
* 获取对象 mime type
*
* @throws UnableToRetrieveMetadata
* @throws FilesystemException
*/
public function mimeType(string $path): FileAttributes
{
Expand All @@ -401,9 +411,10 @@ public function mimeType(string $path): FileAttributes
}

/**
* 获取最后更改
* @param string $path
* @return FileAttributes
* 获取对象最后修改时间
*
* @throws UnableToRetrieveMetadata
* @throws FilesystemException
*/
public function lastModified(string $path): FileAttributes
{
Expand All @@ -415,9 +426,10 @@ public function lastModified(string $path): FileAttributes
}

/**
* 获取文件大小
* @param string $path
* @return FileAttributes
* 获取对象大小
*
* @throws UnableToRetrieveMetadata
* @throws FilesystemException
*/
public function fileSize(string $path): FileAttributes
{
Expand All @@ -430,9 +442,10 @@ public function fileSize(string $path): FileAttributes

/**
* 列出对象
*
* @param string $path
* @param bool $deep
* @return iterable
* @return iterable<StorageAttributes>
* @throws OssException
*/
public function listContents(string $path, bool $deep): iterable
Expand All @@ -449,37 +462,6 @@ public function listContents(string $path, bool $deep): iterable
}
}

/**
* 移动对象到新位置
* @param string $source
* @param string $destination
* @param Config $config
*/
public function move(string $source, string $destination, Config $config): void
{
try {
$this->copy($source, $destination, $config);
$this->delete($source);
} catch (FilesystemOperationFailed $exception) {
throw UnableToMoveFile::fromLocationTo($source, $destination, $exception);
}
}

/**
* 复制对象到新位置
* @param string $source
* @param string $destination
* @param Config $config
*/
public function copy(string $source, string $destination, Config $config): void
{
try {
$this->client->copyObject($this->bucket, $this->prefixer->prefixPath($source), $this->bucket, $this->prefixer->prefixPath($destination));
} catch (Throwable $exception) {
throw UnableToCopyFile::fromLocationTo($source, $destination, $exception);
}
}

/**
* 列出对象
* @param string $directory
Expand Down Expand Up @@ -509,4 +491,35 @@ private function listObjects(string $directory = '', bool $recursive = false): a
}
return $result;
}

/**
* 移动对象到新位置
*
* @throws UnableToMoveFile
* @throws FilesystemException
*/
public function move(string $source, string $destination, Config $config): void
{
try {
$this->copy($source, $destination, $config);
$this->delete($source);
} catch (FilesystemOperationFailed $exception) {
throw UnableToMoveFile::fromLocationTo($source, $destination, $exception);
}
}

/**
* 复制对象到新位置
*
* @throws UnableToCopyFile
* @throws FilesystemException
*/
public function copy(string $source, string $destination, Config $config): void
{
try {
$this->client->copyObject($this->bucket, $this->prefixer->prefixPath($source), $this->bucket, $this->prefixer->prefixPath($destination));
} catch (Throwable $exception) {
throw UnableToCopyFile::fromLocationTo($source, $destination, $exception);
}
}
}

0 comments on commit 6cdbb90

Please sign in to comment.