Commit 722c4caf authored by Thomas Persohn's avatar Thomas Persohn
Browse files

Merge branch 'master' into api_upload

parents 90e01eef 775d526d
......@@ -155,6 +155,17 @@ $file = $client->sendFile('My Awesome Movie', '/path/to/the/file', $folder, ['ch
$file->name = 'Q2 meeting';
$client->push($file);
// Add subtitles to the file
$file->addSubtitle('/path/to/subtitle/file', 'es');
foreach ($file->subtitles as $subtitle) {
echo $subtitle->language.': '.$subtitle->href."\n";
}
// Remove subtitles
$file->removeSubtitle($subtitle); // You can also just use a URL
// or:
$client->removeSubtitle($subtitle); // You can also just use a URL
// Folder properties
$folder->name // Name of the folder
......@@ -388,6 +399,7 @@ $resource->metadata // Array of metadata
$resource->usages // Collection of usages
$resource->widgets // Collection of widgets
$resource->flavors // Collection of flavors
$resource->subtitles // Collection of subtitles
// Usage of widgets
foreach ($resource->widgets as $embed) echo urldecode($embed);
......
......@@ -15,6 +15,7 @@
"autoload": {
"psr-0": {
"Libcast": "src"
}
},
"files": ["src/functions.php"]
}
}
<?php
namespace Libcast\Client\Command;
use Libcast\Client\Model\FileInterface;
class AddSubtitle extends FileCommand
{
/**
*
* @var string
*/
protected $method = 'POST';
/**
*
* @param FileInterface $file
* @param string $path Path to the subtitle file
* @param string $language 2-letters ISO 639-1 language code
*/
public function __construct(FileInterface $file, $path, $language)
{
if (!is_readable($path)) {
throw new \RuntimeException(sprintf('File "%s" does not exist or permission to read it is denied.', $path));
}
$extension = pathinfo($path, PATHINFO_EXTENSION);
if (!in_array($extension, $formats = ['srt', 'vtt'])) {
throw new \RuntimeException('The only subtitle formats recognized at the time are: '.implode(', ', $formats));
}
if (strlen($language) != 2) {
throw new \RuntimeException('Subtitle language must be a 2-letters ISO 639-1 language code.');
}
$this->fullPath = $file->href.'/subtitles';
$this->setPayload([
'language' => $language,
]);
$this->requestOptions[CURLOPT_POSTFIELDS] = [
'subtitle' => curl_file_create($path),
'language' => $language,
];
}
}
\ No newline at end of file
......@@ -83,6 +83,18 @@ abstract class Command implements CommandInterface
*/
protected $url;
/**
*
* @var array cURL options
*/
protected $requestOptions = array();
/**
*
* @var array cURL headers
*/
protected $requestHeaders = array();
/**
*
* @param string $name
......@@ -205,7 +217,7 @@ abstract class Command implements CommandInterface
*/
protected function getRequestHeaders()
{
return array();
return $this->requestHeaders;
}
/**
......@@ -214,7 +226,7 @@ abstract class Command implements CommandInterface
*/
protected function getRequestOptions()
{
return array();
return $this->requestOptions;
}
/**
......@@ -245,6 +257,7 @@ abstract class Command implements CommandInterface
case 401: throw new UnauthorizedException($response->getReasonPhrase());
case 403: throw new ForbiddenException($response->getReasonPhrase());
case 404: throw new NotFoundException($response->getReasonPhrase());
case 412: throw new ServerException($response->getReasonPhrase());
case 500: throw new ServerException($response->getReasonPhrase());
}
......
<?php
namespace Libcast\Client\Command;
use Libcast\Client\Entity\Subtitle;
class RemoveSubtitle extends Command
{
/**
*
* @var string
*/
protected $method = 'DELETE';
/**
*
* @param Subtitle $subtitle
*/
public function __construct($subtitle)
{
if ($subtitle instanceof Subtitle) {
$this->fullPath = $subtitle->href;
} elseif(is_string($subtitle)) {
$this->fullPath = $subtitle;
} else {
throw new \BadParameterException(__CLASS__.' command requires a Subtitle instance or a subtitle API URL.');
}
}
}
......@@ -10,12 +10,6 @@ use JMS\Serializer\SerializerInterface;
class SendFile extends FileCommand
{
/**
*
* @var array cURL options
*/
private $requestOptions = array();
/**
*
* @var array Command options
......@@ -64,7 +58,7 @@ class SendFile extends FileCommand
if (is_readable($path)) { // Local file
$this->fileSize = filesize($path);
$fields = array('path' => "@$path", 'name' => $name);
$fields = array('path' => curl_file_create($path), 'name' => $name);
if (!is_null($slug)) {
$fields['folder'] = $slug;
}
......@@ -118,7 +112,7 @@ class SendFile extends FileCommand
$chunk = 0;
foreach ($chunks as $chunkPath) {
$this->requestOptions[CURLOPT_POSTFIELDS]['chunk'] = $chunk++;
$this->requestOptions[CURLOPT_POSTFIELDS]['path'] = "@$chunkPath";
$this->requestOptions[CURLOPT_POSTFIELDS]['path'] = curl_file_create($chunkPath);
$response = $this->send($client);
unlink($chunkPath);
}
......@@ -137,6 +131,11 @@ class SendFile extends FileCommand
*/
private function splitFile($path, $chunkSize)
{
// PHP >= 5.5
if ($path instanceof \CURLFile) {
$path = $path->name;
}
if ('@' === $path{0}) {
$path = substr($path, 1);
}
......
......@@ -114,6 +114,11 @@ abstract class Entity implements ClientAwareInterface, WarmableInterface
$this->$property->setClient($this->client);
}
// Special properties should not be accessible
if ('_' == $property{0}) {
return;
}
return $this->$property;
}
......
......@@ -8,6 +8,7 @@ use Libcast\Client\Model\StreamInterface;
use JMS\Serializer\Annotation\Type;
use JMS\Serializer\Annotation\XmlRoot;
use JMS\Serializer\Annotation\XmlList;
use JMS\Serializer\Annotation\SerializedName;
/**
......@@ -66,6 +67,13 @@ class File extends Entity implements FileInterface
*/
protected $metadata = [];
/**
* @Type("array<Libcast\Client\Entity\Subtitle>")
* @XmlList(inline = false, entry = "subtitle")
* @var array of Subtitle
*/
protected $subtitles = array();
/**
* URL of the thumbnail
*
......@@ -106,6 +114,25 @@ class File extends Entity implements FileInterface
return $this->client->createResource($resource, $stream);
}
/**
*
* @param string $path Path to the subtitle file
* @param string $language 2-letters ISO 639-1 language code
*/
public function addSubtitle($path, $language)
{
return $this->client->addSubtitle($this, $path, $language);
}
/**
*
* @param string $href Subtitle href or a Subtitle instance
*/
public function removeSubtitle($href)
{
return $this->client->removeSubtitle($href);
}
// Deprecated API
/**
......
......@@ -130,6 +130,13 @@ class Resource extends Entity implements ResourceInterface
*/
protected $widgets = [];
/**
* @Type("array<Libcast\Client\Entity\Subtitle>")
* @XmlList(inline = false, entry = "subtitle")
* @var array of Subtitle
*/
protected $subtitles = array();
/**
*
* @Exclude
......
<?php
namespace Libcast\Client\Entity;
use JMS\Serializer\Annotation\Type;
use JMS\Serializer\Annotation\XmlRoot;
use JMS\Serializer\Annotation\XmlAttribute;
/**
* Immutable subtitle entity
*
* @XmlRoot("subtitle")
*
* @property string $language Lower case ISO 639-1 language code
* @property string $href URL to download (in a Resource) or to delete (in a File) the subtitle file
*/
class Subtitle
{
/**
* Lower case ISO 639-1 code
*
* @XmlAttribute
* @Type("string")
* @var string
*/
protected $language;
/**
*
* @XmlAttribute
* @Type("string")
* @var string
*/
protected $href;
/**
*
* @param string $href
* @param string $language
*/
public function __construct($href = null, $language = null)
{
$this->href = $href;
$this->language = $language;
}
/**
*
* @param string $property
*
* @return mixed
*/
public function __get($property)
{
if (property_exists($this, $property)) {
return $this->$property;
}
}
}
......@@ -78,6 +78,8 @@ use Psr\Log\LogLevel;
* File
*
* @method \Libcast\Client\Entity\File sendFile(string $name, string $path) Upload a file to the user briefcase
* @method \Libcast\Client\Entity\File addSubtitle(\Libcast\Client\Model\FileInterface $path, string $language) Upload a subtitle file and attach it to the File
* @method \Libcast\Client\Entity\File removeSubtitle(\Libcast\Client\Entity\Subtitle $subtitle) Remove a subtitle from a File given a Subtitle instance or its URL
*
* Token
*
......
<?php
/**
*
* @see php.net/curl_file_create
*/
if (!function_exists('curl_file_create')) {
if (!extension_loaded('curl')) {
throw new \Exception('cURL extension is required by the Libcast API client.');
}
// We are in PHP < 5.5
/**
*
* @param string $filename
* @param string $mimetype
* @param string $postname
*
* @return string
*/
function curl_file_create($filename, $mimetype = '', $postname = '')
{
return "@$filename;filename="
. ($postname ?: basename($filename))
. ($mimetype ? ";type=$mimetype" : '');
}
}
Markdown is supported
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