-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Rene Schmidt
committed
Jan 15, 2016
1 parent
97230d2
commit a2b8248
Showing
8 changed files
with
495 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] |
Oops, something went wrong.