- Seafile PHP SDK
- German Web Application Developer Available for Hire!
- What is Seafile?
- How to get Started
- Roadmap and notes on development
- Obtain API token
- Installing Seafile-PHP-SDK
- Using Seafile PHP SDK
- Connecting to Seafile
- List available libraries
- List directory contents
- Check if directory item exists
- Create directory
- Download file from unencrypted library
- Download file from encrypted library
- Upload file
- Update file
- Get file details
- Get API user account info
- Get all accounts
- Create account
- Update account
- Get account info by email address
- Delete account
- Get avatar of an account
- Create and remove shared link
- Get all starred files, star and unstar file
- Debugging and how to enable logging of requests and responses
- Version history
- Issues
- Dependencies
- Seafile Web API V2 Support Matrix
- Seafile Web API V2.1 Support Matrix
- Seafile server compatibility
- Contributing
- Links
- License
Seafile PHP SDK
This is a PHP package for accessing Seafile Web API.
German Web Application Developer Available for Hire!
No marketing skills whatsoever, but low rates, nearly 20 years of experience, and german work attitude.
Get in touch now: https://sdo.sh/DevOps/#contact
What is Seafile?
- Open Source Cloud Storage for your teams and organizations
- Built in File Encryption, better Protecting your Privacy
- Collaboration around Files, file locking and other features make collaboration easy.
How to get Started
To get started with Seafile PHP SDK, you may either set up your own private Seafile server (see https://www.seafile.com/en/product/private_server/) or obtain seacloud.cc accounthttps://seacloud.cc. Because the SDK is in its infancy it's highly recommended to set up a test server or create a test account.
It's not advisable yet to use your real server/account if you already got one.
After you have created your test account continue to the next step.
Roadmap and notes on development
Please note that this SDK currently is under active development and that things might change rather drastically.
If you are looking for stability please refer to stable tags.
The next stable version is planned for January 2018.
Obtain API token
Have a look at script bin/obtain_api_token.sh
and use it if you feel comfortable with it. Basically, the script does this:
- 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
Insert your test user name and test user password. Hint: if user name contains a "+" char, replace the char with "%2B" (hex ascii for "+") or urlencode()
the user name altogether. Just so you know.
The file ~/.seafile-php-sdk/api-token.json
will look something like this:
{"token": "your_api_token"}
The example script will assume a config file ~/.seafile-php-sdk/cfg.json
that looks like this:
Have a look at script bin/create_test_cfg.sh
and use it if you feel comfortable with it. Basically, the script does this:
{
"baseUri": "https://your-seafile-server.example.com",
"testLibId": "test-library-id",
"testLibPassword": "test-library-password"
}
Installing Seafile-PHP-SDK
The recommended way to install seafile-php-sdk is throughComposer.
- # Install Composer
- curl -sS https://getcomposer.org/installer | php
Next, run the Composer command to install the latest stable version of seafile-php-sdk:
- composer.phar require rsd/seafile-php-sdk
- # composer.phar dump-autoload -o # not required anymore
After installing, you need to require Composer's autoloader:
- require 'vendor/autoload.php';
You can then later update seafile-php-sdk using composer:
- composer.phar update
- # composer.phar dump-autoload -o # not required anymore
Using Seafile PHP SDK
Hint: Have a look at bin/example.php
— everything this SDK can do is covered there!
Connecting to Seafile
First, you need to include the API token (see above):
- $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
- ]
- ]
- );
List available libraries
- $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');
- }
List directory contents
- $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);
- }
Check if directory item exists
- $parentDir = '/'; // DirectoryItem must exist within this directory
- $directory = 'DirectoryName';
- if($directoryResource->exists($lib, $directoryItemName, $parentDir) === false) {
- // directory item does not exist
- }
Be aware that because Seafile Web API does not provide a function to do this check on its own, all items of the directory will get loaded for iteration. So that's not very efficient.
Create directory
- $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);
Download file from unencrypted library
- $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);
Download file from encrypted library
Trying to download a file from an encrypted library without unlocking it first wouldinevitably fail, so just unlock (API docs say "decrypt") the library before attempting:
- $success = $libraryResource->decrypt($libId, ['query' => ['password' => $password]]);
- // rest is the same as 'Download file from unencrypted library', see above
Upload file
- $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());
Update file
- $response = $fileResource->update($lib, $newFilename, '/');
- $updatedFileId = json_decode((string)$response->getBody());
Get file details
- $directoryItem = $fileResource->getFileDetail($lib, '/' . basename($fullFilePath));
Get API user account info
- $accountResource = new Account($client);
- $accountType = $accountResource->getInfo();
- print_r($accountType->toArray());
Get all accounts
- $accountResource = new Account($client);
- $accountTypes = $accountResource->getAll();
- foreach ($accountTypes as $accountType) {
- print_r($accountType->toArray());
- }
Create account
- $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);
Update account
- $updateAccountType = (new AccountType)->fromArray([
- 'name' => 'Divine Hugh Jazz',
- 'email' => 'someone@example.com'
- ]);
- $success = $accountResource->update($updateAccountType);
Get account info by email address
- $accountResource = new Account($client);
- $accountType = $accountResource->getByEmail('someone@example.com');
- print_r($accountType->toArray());
Delete account
- $accountResource = new Account($client);
- $accountType = (new AccountType)->fromArray([
- 'email' => 'someone@example.com'
- ]);
- $success = $accountResource->remove($accountType);
or
- $accountResource = new Account($client);
- $success = $accountResource->removeByEmail('someone@example.com');
Get avatar of an account
- $accountType = (new AccountType)->fromArray([
- 'email' => 'someone@example.com'
- ]);
- $avatarResource = new Avatar($client);
- print_r($avatarResource->getUserAvatar($accountType)->toArray());
or
- print_r($avatarResource->getUserAvatarByEmail('someone@example.com')->toArray());
Create and remove shared link
- $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);
Get all starred files, star and unstar file
- $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);
- }
Debugging and how to enable logging of requests and responses
This example requires monolog. Log entries and Guzzle debug info will be written to 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
- ]
- ]
- );
Version history
Version 2.x (not yet released)
This version will not be backwards compatible with Version 1.x.
- SEAF-001: Will support PHP >7.0.0 only. In case you require a version that still supports PHP 5.6 let _gth@sdo.sh">me know so I can tell you my rates :)
- SEAF-002: Requires PHPUnit 6.x
Version 1.0.1
Latest and last version to support PHP 5.6.
Issues
Please let me know of issues.
Dependencies
PHP >=7.0 64 bits
- Guzzle 6
Seafile Web API V2 Support Matrix
Resource | Support grade |
---|---|
Account | |
Starred Files | |
Group | |
File Share Link | |
Library/Library | |
Library/File | |
Library/Directory | |
Library/Multiple Files | |
Avatar | |
Events | Yet to be done, _gth@sdo.sh">contact me |
Organization | Yet to be done, _gth@sdo.sh">contact me |
Seafile Web API V2.1 Support Matrix
Resource | Support grade |
---|---|
Web API v2.1 | Yet to be done, _gth@sdo.sh">contact me |
Seafile server compatibility
Tested with:
Seafile Server 5.1.3 for generic Linux/Debian JessieSeafile Server 5.1.3 for generic Linux/Debian WheezySeafile Server 5.1.4 for generic Linux/Ubuntu XenialSeafile Server 6.0.3 for generic Linux/Ubuntu Xenial- Seafile Server 6.2.x for generic Linux/Ubuntu Xenial
- If you require a backport for an older Seafile server version _gth@sdo.sh">contact me for a quote!
Contributing
Please note that this package still is in its infancy. Only a part of the API has been implemented so far.
Pull requests are welcome. Please adhere to some very basic and simple principles:
- Follow "separation of concern" on all levels: 1 issue == 1 pull request. Do not cover multiple issues in a pull request.
- Unit tests raise the chance of your pull request getting accepted.
The same goes for PHPDoc blocks.
Links
- https://manual.seafile.com/develop/web_api.html#seafile-web-api-v2
- https://sdo.sh
- https://luckycloud.de/de/ (Seafile server hosting in Germany)
- https://www.yoursecurecloud.de/ (Seafile server hosting in Germany)
License
MIT © 2015-2017 Rene Schmidt DevOps UG (haftungsbeschränkt) & Co. KG