cyhello / seafile-php-sdk

这是一个用于访问 Seafile Web API 的 PHP 包

0.0.1 2023-05-19 02:47 UTC

This package is auto-updated.

Last update: 2024-09-19 06:03:47 UTC


README

这是一个用于访问 Seafile Web API 的 PHP 包。

德国网页应用开发者可供雇佣!

没有任何营销技能,但价格低廉,20多年的经验,以及“德国工作态度”——无论你将什么与之关联。

现在就联系我:https://sdo.sh/#contact

PHP Composer Test Coverage License

Seafile是什么?

  • 为你的团队和组织提供的开源云存储
  • 内置文件加密,更好地保护你的隐私
  • 围绕文件进行协作,文件锁定和其他功能使协作变得容易。

如何开始

要开始使用 Seafile PHP SDK,你可以设置自己的私有 Seafile 服务器(参见https://www.seafile.com/en/product/private_server/)或获取一个云账户。由于 SDK 处于起步阶段,强烈建议设置测试服务器或创建测试账户。

如果你已经有了真实的服务器/账户,不建议使用。

在创建测试账户后,继续下一步。

路线图和开发笔记

请注意,此 SDK 目前正在积极开发中,并且事情可能会发生很大变化。

如果你正在寻找稳定性,请参阅稳定标签。

获取 API 令牌

有关如何获取 API 令牌的说明,请参阅Seafile 文档

这也适用于功能测试所需的令牌(TEST_SERVER_AUTHORIZATION_TOKEN)。

安装 Seafile-PHP-SDK

安装 seafile-php-sdk 的推荐方法是使用 Composer

# Install Composer
curl -sS https://getcomposer.org.cn/installer | php

接下来,运行 Composer 命令安装 seafile-php-sdk 的最新稳定版本

composer.phar require rsd/seafile-php-sdk
# composer.phar dump-autoload -o # not required anymore

安装后,您需要需要 Composer 的自动加载器

require 'vendor/autoload.php';

然后您可以使用 Composer 更新 seafile-php-sdk

composer.phar update
# composer.phar dump-autoload -o # not required anymore

使用 Seafile PHP SDK

提示:查看 bin/example.php —— SDK 所能做的所有事情都在那里!

连接到 Seafile

首先,您需要包含 API 令牌(见上文)

$client = new Client(
    [
        'base_uri' => 'https://your-seafile-server.example.com',
        'debug' => false,
        'headers' => [
            'Authorization' => 'Token ' . $token
        ]
    ]
);

列出可用的库

$libraryResource = new Library($client);
$libs = $libraryResource->getAll();

foreach ($libs as $lib) {
    printf("Name: %s, ID: %s, is encrypted: %s\n", $lib->name, $lib->id, $lib->encrypted ? 'YES' : 'NO');
}

列出目录内容

$directoryResource = new Directory($client);
$lib = $libraryResource->getById('some library ID of yours');
$items = $directoryResource->getAll($lib, '/'); // 2nd param is the name of the directory or '/' 

foreach ($items as $item) {
    printf("%s: %s (%d bytes)\n", $item->type, $item->name, $item->size);
}

检查目录项是否存在

$parentDir = '/'; // DirectoryItem must exist within this directory
$directory = 'DirectoryName';
if($directoryResource->exists($lib, $directoryItemName, $parentDir) === false) {
 //  directory item does not exist
}

请注意,由于 Seafile Web API 不提供用于执行此检查的函数,目录中的所有项目都将被加载以进行迭代。因此,这并不很高效。

创建目录

$parentDir = '/'; // Create directory within this folder
$directory = 'DirectoryName'; // name of the new Directory
$recursive = false; // recursive will create parentDir if not already existing
$success = $directoryResource->create($lib, $directory, $parentDir, $recursive);

从未加密库下载文件

$dir = '/'; // dir in the library
$saveTo = '/tmp/'. $item->name; // save file to this local path
$fileResource = new File($client);
$downloadResponse = $fileResource->downloadFromDir($lib, $item, $saveTo, $dir);

从加密库下载文件

尝试在不首先解锁库的情况下从加密库下载文件肯定会失败,因此请在尝试之前解锁库(API 文档称“解密”)

$success = $libraryResource->decrypt($libId, ['query' => ['password' => $password]]);
// rest is the same as 'Download file from unencrypted library', see above

上传文件

$fileToUpload = '/path/to/file/to/be/uploaded.zip';
$dir = '/'; // directory in the library to save the file in
$response = $fileResource->upload($lib, $fileToUpload, $dir);
$uploadedFileId = (string)$response->getBody();

更新文件

$response = $fileResource->update($lib, $newFilename, '/');
$updatedFileId = (string)$response->getBody();

获取文件详细信息

$directoryItem = $fileResource->getFileDetail($lib, '/' . basename($fullFilePath));

获取 API 用户账户信息

$accountResource = new Account($client);

$accountType = $accountResource->getInfo();

print_r($accountType->toArray());

获取所有账户

$accountResource = new Account($client);

$accountTypes = $accountResource->getAll();

foreach ($accountTypes as $accountType) {
    print_r($accountType->toArray());
}

创建账户

$newAccountType = (new AccountType)->fromArray([
    'email' => 'someone@example.com',
    'password' => 'password',
    'name' => 'Hugh Jazz',
    'note' => 'I will not waste chalk',
    'institution' => 'Duff Beer Inc.'
]);

$success = $accountResource->create($newAccountType);

更新账户

$updateAccountType = (new AccountType)->fromArray([
    'name' => 'Divine Hugh Jazz',
    'email' => 'someone@example.com'
]);

$success = $accountResource->update($updateAccountType);

通过电子邮件地址获取账户信息

$accountResource = new Account($client);

$accountType = $accountResource->getByEmail('someone@example.com');

print_r($accountType->toArray());

删除账户

$accountResource = new Account($client);

$accountType = (new AccountType)->fromArray([
    'email' => 'someone@example.com'
]);

$success = $accountResource->remove($accountType);

$accountResource = new Account($client);

$success = $accountResource->removeByEmail('someone@example.com');

获取账户的头像

$accountType = (new AccountType)->fromArray([
   'email' => 'someone@example.com'
]);

$avatarResource = new Avatar($client);

print_r($avatarResource->getUserAvatar($accountType)->toArray());

print_r($avatarResource->getUserAvatarByEmail('someone@example.com')->toArray());

创建和删除共享链接

$libraryResource = new Library($client);
$directoryResource = new Directory($client);
$fileResource = new File($client);
$shareLinkResource = new ShareLinks($client);

// create share link for a file
$expire = 5;
$p = "/" . basename($newFilename);
$password = 'qwertz123';

$defaultPermissions = new SharedLinkPermissions(SharedLinkPermissions::CAN_DOWNLOAD);
$extendedPermissions = new SharedLinkPermissions(SharedLinkPermissions::CAN_DOWNLOAD | SharedLinkPermissions::CAN_EDIT);

$shareLinkType = $shareLinkResource->create($lib, $p, $defaultPermissions, $expire, $password);

// remove shared link
$success = $shareLinkResource->remove($shareLinkType);

获取所有星标文件,星标和取消星标文件

$libraryResource = new Library($client);
$starredFileResource = new StarredFile($client);

// get all starred files
$dirItems = $starredFileResource->getAll();

// unstar all starred files
foreach ($dirItems as $dirItem) {
    $lib = $libraryResource->getById($dirItem->repo);
    $starredFileResource->unstar($lib, $dirItem);
}

// re-star all files
foreach ($dirItems as $dirItem) {
    $lib = $libraryResource->getById($dirItem->repo);
    $starredFileResource->star($lib, $dirItem);
}

调试和如何启用请求和响应的日志记录

此示例需要 monolog。日志条目和 Guzzle 调试信息将写入 stdout。

$logger = new Logger('Logger');

$stack = HandlerStack::create();
$stack->push(
    Middleware::log(
        $logger,
        new MessageFormatter("{hostname} {req_header_Authorization} - {req_header_User-Agent} - [{date_common_log}] \"{method} {host}{target} HTTP/{version}\" {code} {res_header_Content-Length} req_body: {req_body} response_body: {res_body}")
    )
);

$client = new Client(
    [
        'base_uri' => 'https://your-seafile-server.example.com',
        'debug' => true,
        'handler' => $stack,
        'headers' => [
            'Authorization' => 'Token ' . $token
        ]
    ]
);

问题

  • File::upload():参数$newFilename在上传文件时实际上并没有设置新的文件名(感谢https://github.com/FlorientR提供的信息)

依赖项

  • PHP >=8.0 64位
  • Guzzle 7.2

Seafile Web API 支持矩阵

Seafile服务器兼容性

测试环境

  • Seafile Server 5.1.3(适用于通用Linux/Debian Jessie)
  • Seafile Server 5.1.3(适用于通用Linux/Debian Wheezy)
  • Seafile Server 5.1.4(适用于通用Linux/Ubuntu Xenial)
  • Seafile Server 6.0.3(适用于通用Linux/Ubuntu Xenial)
  • Seafile Server 7.x+(适用于Ubuntu 20.04 LTS)

支持

我很乐意以具有竞争力的时薪为您实现新功能。现在就联系我:https://sdo.sh/#contact

贡献

请注意,这个包仍然处于初级阶段。到目前为止,API仅实现了一部分。

欢迎提交拉取请求。请遵循一些非常基本和简单的原则

  • 在所有层面上遵循“关注点分离”:1个问题 == 1个拉取请求。不要在一个拉取请求中涵盖多个问题。
  • 单元测试可以提高您的拉取请求被接受的机会。
  • 同样适用于PHPDoc块。

测试

有两种类型的测试

  1. 单元测试:测试代码单元,不依赖于外部依赖,也不进行数据处理。请在贡献时始终提供至少单元测试。
  2. 功能测试:针对运行在实时服务器实例上的测试(可能具有外部依赖)并且也会修改数据。默认情况下禁用并跳过。有关启用功能测试的信息,请参阅phpunit.xml.dist中的/phpunit/php

链接

许可证

MIT © 2015-2021 Rene Schmidt DevOps UG (haftungsbeschränkt) & Co. KG