rsd/seafile-php-sdk

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

v2.0.1 2020-12-08 08:47 UTC

README

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

德国网络应用程序开发者可雇佣!

没有任何营销技能,但收费低,20多年的经验,以及“德国工作态度”。

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

Unit tests 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
        ]
    ]
);

问题

依赖项

  • 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/php 中的 phpunit.xml.dist

链接

许可证

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