Commit cd33d1a9 authored by mickael's avatar mickael
Browse files

Initial commit

parents
{
"name": "certic/circe-php-client",
"description": "Client library for Circe server",
"type": "library",
"license": "BSD",
"minimum-stability": "stable",
"autoload": {
"psr-0": {
"Certic": "src/"
}
},
"require": {
"php": ">=7.0.0",
"ext-json": "*",
"ext-curl": "*"
},
"require-dev": {
}
}
<?php
declare(strict_types=1);
namespace Certic\Circe;
class Client
{
private $endpoint;
private $uuid;
private $secret;
public function __construct(string $api_endpoint, string $secret_key, string $app_uuid)
{
$this->endpoint = $api_endpoint;
$this->secret = $secret_key;
$this->uuid = $app_uuid;
}
public function availableTransformations(): array
{
}
public function send(Job $job, bool $wait = false, string $destination_file = null)
{
if (!$destination_file) {
$destination_file = \tempnam(\sys_get_temp_dir(), "circe") . '.tar.gz';
}
$job_archive_path = $job->makeArchive();
$job_hash = \hash_hmac_file("sha256", $job_archive_path, $this->secret);
$endpoint = $wait ? $this->endpoint . '/job/?block=1' : $this->endpoint . "/job/";
$ch = \curl_init();
\curl_setopt($ch, CURLOPT_URL, $endpoint);
\curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
\curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
\curl_setopt($ch, CURLOPT_POST, true);
\curl_setopt($ch, CURLOPT_POSTFIELDS, \file_get_contents($job_archive_path));
\curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type:application/json',
'Content-Length: ' . \filesize($job_archive_path),
'Authorization:' . $this->uuid . ' ' . $job_hash,
]);
$output = \curl_exec($ch);
if ($output === false) {
throw new Exception(curl_error($ch));
}
if ($wait) {
file_put_contents($destination_file, $output);
$job->setResultFilePath($destination_file);
} else {
$job->setUuid($output);
}
}
public function poll(Job $job, string $destination_file = null, int $timeout = 30, int $poll_interval = 1)
{
if (!$job->getUuid()) {
$this->send($job);
}
if (!$destination_file) {
$destination_file = \tempnam(\sys_get_temp_dir(), "circe-") . '.tar.gz';
}
$then = time();
while (true) {
}
}
public function newJob(): Job
{
return new Job();
}
}
<?php
declare(strict_types=1);
namespace Certic\Circe;
class Exception extends \Exception
{
}
<?php
declare(strict_types=1);
namespace Certic\Circe;
class Job
{
private $files = [];
private $transformations = [];
private $hook;
private $dir_path;
private $result_file_path;
private $uuid;
public function addFile(string $file_path): Job
{
$this->files[] = $file_path;
return $this;
}
public function addTransformation(string $name, array $options = null): Job
{
if ($options) {
$this->transformations[] = ["name" => $name, "options" => $options];
} else {
$this->transformations[] = ["name" => $name];
}
return $this;
}
public function setNotifyHook(string $web_hook): Job
{
$this->hook = $web_hook;
return $this;
}
public function makeArchive(): string
{
if ($this->dir_path) {
$this->deleteTree($this->dir_path);
}
$this->dir_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid() . DIRECTORY_SEPARATOR;
if (!mkdir($this->dir_path, 0777, true)) {
throw new Exception("Could not create temporary directory");
}
$tar_path = $this->dir_path . "job.tar";
$tar = new \PharData($tar_path);
if ($this->hook) {
$tar->addFromString("job.json", json_encode([
"transformations" => $this->transformations,
"notify_hook" => $this->hook
]));
} else {
$tar->addFromString("job.json", json_encode([
"transformations" => $this->transformations
]));
}
foreach ($this->files as $file) {
$tar->addFile($file, basename($file));
}
$tar->compress(\Phar::GZ);
return $tar_path;
}
public function __destruct()
{
$this->deleteTree($this->dir_path);
}
private function deleteTree(string $folder, bool $keepRootFolder = false): bool
{
if (empty($folder) || !file_exists($folder)) {
return true;
} elseif (is_file($folder) || is_link($folder)) {
return @unlink($folder);
}
$files = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($folder, \RecursiveDirectoryIterator::SKIP_DOTS),
\RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($files as $fileinfo) {
$action = ($fileinfo->isDir() ? 'rmdir' : 'unlink');
if (!@$action($fileinfo->getRealPath())) {
return false; // Abort due to the failure.
}
}
return (!$keepRootFolder ? @rmdir($folder) : true);
}
public function getResultFilePath(): string
{
return $this->result_file_path;
}
public function setResultFilePath(string $result_file_path): Job
{
$this->result_file_path = $result_file_path;
return $this;
}
public function setUuid(string $uuid):Job
{
$this->uuid = $uuid;
return $this;
}
public function getUuid(): string
{
return $this->uuid;
}
public function getResult(): JobResult
{
if ($this->getResultFilePath()) {
return new JobResult($this->getResultFilePath());
}
}
}
<?php
declare(strict_types=1);
namespace Certic\Circe;
class JobResult
{
private $result_file_path;
public function __construct(string $result_file_path)
{
$this->result_file_path = $result_file_path;
}
public function getFiles(): \Iterator
{
$tar = new \PharData($this->result_file_path);
foreach ($tar as $file){
yield $file;
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment