Skip to content

Commit

Permalink
added StarredFile resource
Browse files Browse the repository at this point in the history
  • Loading branch information
Rene Schmidt committed Jan 15, 2016
1 parent 97230d2 commit a2b8248
Show file tree
Hide file tree
Showing 8 changed files with 495 additions and 9 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -339,16 +339,16 @@ $client = new Client(
| Resource | Support grade |
| ---------------------- | ------------- |
| Account | :large_blue_circle::large_blue_circle::large_blue_circle::white_circle: |
| Starred Files | :white_circle::white_circle::white_circle::white_circle: |
| Starred Files | :large_blue_circle::large_blue_circle::large_blue_circle::large_blue_circle: |
| Group | :large_blue_circle::white_circle::white_circle::white_circle: |
| File Share Link | :large_blue_circle::large_blue_circle::white_circle::white_circle: |
| Library/Library | :large_blue_circle::large_blue_circle::white_circle::white_circle: |
| Library/File | :large_blue_circle::large_blue_circle::white_circle::white_circle: |
| Library/Directory | :large_blue_circle::large_blue_circle::white_circle::white_circle: |
| Library/Multiple Files | :large_blue_circle::large_blue_circle::large_blue_circle::large_blue_circle: |
| Avatar | :large_blue_circle::large_blue_circle::large_blue_circle::large_blue_circle: |
| Events | :white_circle::white_circle::white_circle::white_circle: |
| Organization | :white_circle::white_circle::white_circle::white_circle: |
| Events | not planned |
| Organization | not planned |

## Seafile server compatibility

Expand Down
103 changes: 103 additions & 0 deletions bin/examples/starred_file.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

/**
* Attention: This example script will modify the test library! Do not run this script
* unless you are prepared for that.
*/

require_once __DIR__ . '/../../vendor/autoload.php';

use Seafile\Client\Resource\Directory;
use Seafile\Client\Resource\File;
use Seafile\Client\Resource\Library;
use Seafile\Client\Resource\SharedLink;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\MessageFormatter;
use Monolog\Logger;
use Seafile\Client\Http\Client;
use Seafile\Client\Resource\StarredFile;
use Seafile\Client\Type\SharedLink as SharedLinkType;

$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}")
)
);

/**
* Example:
* {"token": "your_token"}
*/
$tokenFile = getenv("HOME") . "/.seafile-php-sdk/api-token.json";

/**
* Example:
* {
* "baseUri": "https://your.seafile-server.example.com",
* "testLibId": "ID of an encrypted library",
* "testLibPassword": "Password of that encrypted library"
* }
*/
$cfgFile = getenv("HOME") . "/.seafile-php-sdk/cfg.json";

if (!is_readable($tokenFile)) {
throw new Exception($tokenFile . ' is not readable or does not exist.');
}

if (!is_readable($cfgFile)) {
throw new Exception($cfgFile . ' is not readable or does not exist.');
}

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

$client = new Client(
[
'base_uri' => $cfg->baseUri,
'debug' => true,
'handler' => $stack,
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Token ' . $token->token
]
]
);

$libraryResource = new Library($client);
$directoryResource = new Directory($client);
$fileResource = new File($client);
$starredFileResource = new StarredFile($client);

// get all starred files
$logger->log(Logger::INFO, "#################### Getting all starred files");
$dirItems = $starredFileResource->getAll();

if (!empty($dirItems)) {
foreach ($dirItems as $dirItem) {
var_dump($dirItem);
}

$logger->log(Logger::INFO, "#################### Unstarring files...");

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

$logger->log(Logger::INFO, "#################### Sleeping 10s before starring them again...");
sleep(10);

foreach ($dirItems as $dirItem) {
$lib = $libraryResource->getById($dirItem->repo);
$starredFileResource->star($lib, $dirItem);
}
} else {
$logger->log(Logger::DEBUG, "#################### No starred files found.");
}

print(PHP_EOL . 'Done' . PHP_EOL);
124 changes: 124 additions & 0 deletions lib/Resource/StarredFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

namespace Seafile\Client\Resource;

use Seafile\Client\Http\Client;
use \Seafile\Client\Type\Library as LibraryType;
use \Seafile\Client\Type\DirectoryItem;
use \Seafile\Client\Type\StarredFile as StarredFileType;

/**
* Handles everything regarding Seafile starred files.
*
* Please note that only starred files of the API user can be accessed.
*
* PHP version 5
*
* @category API
* @package Seafile\Resource
* @author Rene Schmidt DevOps UG (haftungsbeschränkt) & Co. KG <[email protected]>
* @copyright 2015 Rene Schmidt DevOps UG (haftungsbeschränkt) & Co. KG <[email protected]>
* @license https://opensource.org/licenses/MIT MIT
* @link https://github.com/rene-s/seafile-php-sdk
*/
class StarredFile extends AbstractResource
{
/**
* @var string
*/
protected $resourceUri = '';

/**
* Constructor
*
* @param Client $client Client instance
*/
public function __construct(Client $client)
{
parent::__construct($client);

$this->resourceUri = $this->clipUri($client->getConfig('base_uri')) . '/starredfiles/';
}

/**
* Get all starred files
*
* @return DirectoryItem[]
*/
public function getAll()
{
$response = $this->client->request('GET', $this->resourceUri);

$json = json_decode((string)$response->getBody());

$dirItemCollection = [];

foreach ($json as $starredFile) {
$dirItemCollection[] = (new DirectoryItem)->fromJson($starredFile);
}

return $dirItemCollection;
}

/**
* Create directory within $parentDir
*
* @param LibraryType $library Library instance
* @param DirectoryItem $dirItem DirectoryItem instance to star
*
* @return string URL of starred file list
* @throws \Exception
*/
public function star(LibraryType $library, DirectoryItem $dirItem)
{
if ($dirItem->type !== 'file') {
throw new \Exception('Cannot star other items than files.');
}

$response = $this->client->request(
'POST',
$this->resourceUri,
[
'headers' => ['Accept' => 'application/json'],
'multipart' => [
[
'name' => 'repo_id',
'contents' => $library->id
],
[
'name' => 'p',
'contents' => $dirItem->path
]
],
]
);

if ($response->getStatusCode() !== 201 || $response->hasHeader('Location') === false) {
throw new \Exception('Could not star file');
}

return $response->getHeader('Location')[0];
}

/**
* Unstar a file
*
* @param LibraryType $library Library instance
* @param DirectoryItem $dirItem DirectoryItem instance
*
* @return bool
*/
public function unstar(LibraryType $library, DirectoryItem $dirItem)
{
$uri = sprintf(
'%s/?repo_id=%s&p=%s',
$this->resourceUri,
$library->id,
$dirItem->path
);

$response = $this->client->request('DELETE', $uri);

return $response->getStatusCode() === 200;
}
}
51 changes: 46 additions & 5 deletions lib/Type/DirectoryItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use DateTime;

/**
* Directory type class
* Directory Item class.
*
* PHP version 5
*
Expand All @@ -24,22 +24,63 @@ class DirectoryItem extends AbstractType
public $id = "";

/**
* @var string
* @var bool
*/
public $size = "";
public $dir = null;

/**
* @var DateTime
*/
public $mtime;

/**
* @var string
*/
public $name = "";

/**
* @var int
*/
public $org = null;

/**
* @var string
*/
public $path = null;

/**
* @var string
*/
public $repo = null;

/**
* @var string
*/
public $size = "";

/**
* @var string
*/
public $type = "";

/**
* @var DateTime
* Populate from array
*
* @param array $fromArray Create from array
*
* @return static
*/
public $mtime;
public function fromArray(array $fromArray)
{
$typeExists = array_key_exists('type', $fromArray);
$dirExists = array_key_exists('dir', $fromArray);

if ($typeExists === false && $dirExists === true && is_bool($fromArray['dir'])) {
$fromArray['type'] = $fromArray['dir'] === true ? 'dir' : 'file';
}

$array = parent::fromArray($fromArray);

return $array;
}
}
16 changes: 16 additions & 0 deletions test/assets/StarredFileTest_getAll.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[
{
"file_name": "seafile-tutorial.doc",
"icon_path": "word.png",
"oid": "eeeab96740ef53249b9d21fb3fa28050842266aa",
"mtime_relative": "<time datetime=\"2015-12-26T04:20:04\" is=\"relative-time\" title=\"Sat, 26 Dec 2015 04:20:04 +0800\" >2015-12-26</time>",
"repo": "eeece753-3a16-4c46-91f7-ee1b013ad17e",
"org": -1,
"path": "/seafile-tutorial.doc",
"size": 300544,
"repo_id": "eeece753-3a16-4c46-91f7-ee1b013ad17e",
"mtime": 1451074804,
"dir": false,
"repo_name": "My Library"
}
]
Loading

0 comments on commit a2b8248

Please sign in to comment.