|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace urvin\phikaru; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
|
6
|
|
|
use GuzzleHttp\Exception\RequestException; |
|
7
|
|
|
|
|
8
|
|
|
class Phikaru |
|
9
|
|
|
{ |
|
10
|
|
|
const UPLOAD_URI = 'upload'; |
|
11
|
|
|
const REMOVE_URI = 'remove'; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $baseUrl; |
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $signatureSalt; |
|
21
|
|
|
/** |
|
22
|
|
|
* @var Client|null |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $http = null; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Phikaru constructor. |
|
28
|
|
|
* @param string $baseUrl |
|
29
|
|
|
* @param string $signatureSalt |
|
30
|
|
|
*/ |
|
31
|
10 |
|
public function __construct(string $baseUrl, string $signatureSalt) |
|
32
|
|
|
{ |
|
33
|
10 |
|
if(empty($baseUrl)) { |
|
34
|
|
|
throw new \InvalidArgumentException("Base URL should not be empty"); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
10 |
|
$this->baseUrl = rtrim($baseUrl, '/'); |
|
38
|
10 |
|
$this->signatureSalt = $signatureSalt; |
|
39
|
10 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return Client |
|
43
|
|
|
*/ |
|
44
|
3 |
|
protected function http(): Client |
|
45
|
|
|
{ |
|
46
|
3 |
|
if(empty($this->http)) { |
|
47
|
1 |
|
$this->http = new Client([ |
|
48
|
1 |
|
'base_uri' => $this->baseUrl . '/' |
|
49
|
|
|
]); |
|
50
|
|
|
} |
|
51
|
3 |
|
return $this->http; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Generate thumbnail url via UrlBuilder |
|
56
|
|
|
* @param int|null $width |
|
57
|
|
|
* @param int|null $height |
|
58
|
|
|
* @param int|null $cast |
|
59
|
|
|
* @param string|null $filename |
|
60
|
|
|
* @param string|null $extension |
|
61
|
|
|
* @return UrlBuilder |
|
62
|
|
|
*/ |
|
63
|
1 |
|
public function thumbnail(?int $width = null, ?int $height = null, ?int $cast = null, ?string $filename = null, ?string $extension = null): UrlBuilder |
|
64
|
|
|
{ |
|
65
|
1 |
|
$builder = UrlBuilder::construct($this->baseUrl, $this->signatureSalt); |
|
66
|
|
|
|
|
67
|
1 |
|
if(!empty($width)) { |
|
68
|
1 |
|
$builder->width($width); |
|
69
|
|
|
} |
|
70
|
1 |
|
if(!empty($height)) { |
|
71
|
1 |
|
$builder->height($height); |
|
72
|
|
|
} |
|
73
|
1 |
|
if(!empty($cast)) { |
|
74
|
1 |
|
$builder->cast($cast); |
|
75
|
|
|
} |
|
76
|
1 |
|
if(!empty($filename)) { |
|
77
|
1 |
|
$builder->filename($filename); |
|
78
|
|
|
} |
|
79
|
1 |
|
if(!empty($extension)) { |
|
80
|
1 |
|
$builder->extension($extension); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
1 |
|
return $builder; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param string $destinationFilename |
|
88
|
|
|
* @param string $sourceFilename |
|
89
|
|
|
* @throws \Exception |
|
90
|
|
|
* @throws \InvalidArgumentException |
|
91
|
|
|
*/ |
|
92
|
4 |
|
public function upload(string $destinationFilename, string $sourceFilename): void |
|
93
|
|
|
{ |
|
94
|
4 |
|
if(empty($destinationFilename)) { |
|
95
|
1 |
|
throw new \InvalidArgumentException("Destination filename should not be empty"); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
3 |
|
if(empty($sourceFilename)) { |
|
99
|
1 |
|
throw new \InvalidArgumentException("Source filename should not be empty"); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
2 |
|
if(!is_readable($sourceFilename)) { |
|
103
|
1 |
|
throw new \RuntimeException('Source file is not readable'); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
1 |
|
$fileHandler = fopen($sourceFilename, 'r'); |
|
107
|
|
|
|
|
108
|
1 |
|
if($fileHandler === false) { |
|
109
|
|
|
throw new \RuntimeException('Could not read source file'); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
try { |
|
113
|
1 |
|
$this->http()->put(static::UPLOAD_URI . '/' . $destinationFilename, [ |
|
114
|
1 |
|
'body' => $fileHandler |
|
115
|
|
|
]); |
|
116
|
1 |
|
} catch (RequestException $e) { |
|
117
|
1 |
|
throw new Exception('Upload exception: ' . $e->getMessage(), 0, $e); |
|
118
|
1 |
|
} finally { |
|
119
|
1 |
|
if(is_resource($fileHandler)) { |
|
120
|
1 |
|
fclose($fileHandler); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
1 |
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param string $filename |
|
127
|
|
|
* @throws \Exception |
|
128
|
|
|
* @throws \InvalidArgumentException |
|
129
|
|
|
*/ |
|
130
|
2 |
|
public function remove(string $filename): void |
|
131
|
|
|
{ |
|
132
|
2 |
|
if(empty($filename)) { |
|
133
|
1 |
|
throw new \InvalidArgumentException("Filename should not be empty"); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
try { |
|
137
|
1 |
|
$this->http()->delete(static::REMOVE_URI . '/' . $filename); |
|
138
|
1 |
|
} catch (RequestException $e) { |
|
139
|
1 |
|
throw new Exception('Remove exception: ' . $e->getMessage(), 0, $e); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
} |