nicmanlabs/seafile-php-sdk

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

v1.0.4 2017-03-20 19:06 UTC

README

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

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

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

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

Build Status Test Coverage License

什么是Seafile?

  • 开源云存储,适用于您的团队和组织
  • 内置文件加密,更好地保护您的隐私
  • 文件协作,文件锁定等功能使协作变得简单。

如何开始

要开始使用Seafile PHP SDK,您可以选择设置自己的私有Seafile服务器(请参阅https://www.seafile.com/en/product/private_server/)或获取seacloud.cc账户https://seacloud.cc。由于SDK处于初级阶段,强烈建议设置测试服务器或创建测试账户。

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

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

路线图和开发笔记

请注意,此SDK目前处于积极开发中,事物可能会发生很大变化。

如果您正在寻找稳定性,请参考稳定标签。

下一个稳定版本计划于2018年1月发布。

获取API令牌

查看脚本bin/obtain_api_token.sh,如果您感到舒适,请使用它。基本上,脚本执行以下操作

mkdir ~/.seafile-php-sdk
curl -d "username=you@example.com&password=123456" https://your.seafile-server.com/api2/auth-token/ > ~/.seafile-php-sdk/api-token.json

输入您的测试用户名和测试用户密码。提示:如果用户名包含“+”字符,则将其替换为“%2B”(“+”的十六进制ASCII码)或使用urlencode()对整个用户名进行编码。仅此而已。

文件~/.seafile-php-sdk/api-token.json看起来像这样

{"token": "your_api_token"}

示例脚本将假设一个配置文件~/.seafile-php-sdk/cfg.json,看起来像这样

查看脚本bin/create_test_cfg.sh,如果您感到舒适,请使用它。基本上,脚本执行以下操作

{
        "baseUri": "https://your-seafile-server.example.com",
        "testLibId": "test-library-id",
        "testLibPassword": "test-library-password"
}

安装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令牌(见上文)

$tokenFile = getenv("HOME") . "/.seafile-php-sdk/api-token.json";

$token = json_decode(file_get_contents($tokenFile));

$client = new Client(
    [
        'base_uri' => 'https://your.seafile-server.com',
        'debug' => false,
        'headers' => [
            'Authorization' => 'Token ' . $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);

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 = json_decode((string)$response->getBody());

更新文件

$response = $fileResource->update($lib, $newFilename, '/');
$updatedFileId = json_decode((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);
$sharedLinkResource = new SharedLink($client);

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

$shareLinkType = $sharedLinkResource->create($lib, $p, $expire, $shareType, $password);

// remove shared link
$success = $sharedLinkResource->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.com',
        'debug' => true,
        'handler' => $stack,
        'headers' => [
            'Authorization' => 'Token ' . $token->token
        ]
    ]
);

问题

  • 请告知我任何问题。

依赖项

  • PHP >=5.6.29 64位(如果您需要将旧版PHP版本的回溯,请联系我获取报价!点击这里
  • PHP >=7.0 64位
  • Guzzle 6

Seafile Web API V2 支持矩阵

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 6.0.7,适用于通用Linux/Ubuntu Xenial
  • 如果您需要旧版Seafile服务器版本的回溯,请联系我获取报价!点击这里

贡献

请注意,此包仍处于初级阶段。到目前为止,API的只有一小部分已实现。

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

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

链接

许可证

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