forked from cloudconvert/cloudconvert-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcess.php
More file actions
203 lines (183 loc) · 6.38 KB
/
Process.php
File metadata and controls
203 lines (183 loc) · 6.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
/**
* This file contains code about \CloudConvert\Process class
*/
namespace CloudConvert;
use CloudConvert\Exceptions\InvalidParameterException;
use GuzzleHttp\Stream\Stream;
/**
* CloudConvert Process Wrapper
*
* @package CloudConvert
* @category CloudConvert
* @author Josias Montag <josias@montag.info>
*/
class Process extends ApiObject
{
/**
* Construct a new Process instance
*
* @param Api $api
* @param string $url The Process URL
* @return \CloudConvert\Process
*
* @throws InvalidParameterException if one parameter is missing or with bad value
*/
public function __construct(Api $api, $url)
{
parent::__construct($api, $url);
return $this;
}
/**
* Starts the Process
*
* @param array $parameters Parameters for creating the Process. See https://cloudconvert.com/apidoc#start
* @return \CloudConvert\Process
*
* @throws \CloudConvert\Exceptions\ApiException if the CloudConvert API returns an error
* @throws \GuzzleHttp\Exception if there is a general HTTP / network error
*
*/
public function start($parameters)
{
if (isset($parameters['file']) && gettype($parameters['file']) == 'resource') {
$file = $parameters['file'];
unset($parameters['file']);
if ($parameters['wait']) {
unset($parameters['wait']);
$wait = true;
}
}
$this->data = $this->api->post($this->url, $parameters, false);
if (isset($file)) {
$this->upload($file);
}
if (isset($wait)) {
$this->wait();
}
return $this;
}
/**
* Uploads the input file. See https://cloudconvert.com/apidoc#upload
*
* @param string $filename Filename of the input file
* @return \CloudConvert\Process
*
* @throws \CloudConvert\Exceptions\ApiException if the CloudConvert API returns an error
* @throws \GuzzleHttp\Exception if there is a general HTTP / network error
*
*/
public function upload($stream, $filename = null)
{
if (!isset($this->upload->url)) {
throw new Exceptions\ApiException("Upload is not allowed in this process state", 400);
}
if (empty($filename)) {
$metadata = stream_get_meta_data($stream);
$filename = basename($metadata['uri']);
}
$this->api->put($this->upload->url . "/" . rawurlencode($filename), $stream, false);
return $this;
}
/**
* Waits for the Process to finish (or end with an error)
*
* @return \CloudConvert\Process
*
* @throws \CloudConvert\Exceptions\ApiException if the CloudConvert API returns an error
* @throws \GuzzleHttp\Exception if there is a general HTTP / network error
*
*/
public function wait()
{
if ($this->step == 'finished' || $this->step == 'error') {
return $this;
}
return $this->refresh(['wait' => 'true']);
}
/**
* Download process files from API
*
* @param string $localfile Local file name (or directory) the file should be downloaded to
* @param string $remotefile Remote file name which should be downloaded (if there are
* multiple output files available)
*
* @return \CloudConvert\Process
*
* @throws \CloudConvert\Exceptions\ApiException if the CloudConvert API returns an error
* @throws \GuzzleHttp\Exception if there is a general HTTP / network error
* @throws Exceptions\InvalidParameterException
*
*/
public function download($localfile = null, $remotefile = null)
{
if (isset($localfile) && is_dir($localfile) && isset($this->output->filename)) {
$localfile = realpath($localfile) . DIRECTORY_SEPARATOR
. (isset($remotefile) ? $remotefile : $this->output->filename);
} elseif (!isset($localfile) && isset($this->output->filename)) {
$localfile = (isset($remotefile) ? $remotefile : $this->output->filename);
}
if (!isset($localfile) || is_dir($localfile)) {
throw new Exceptions\InvalidParameterException("localfile parameter is not set correctly");
}
return $this->downloadStream(fopen($localfile, 'w'), $remotefile);
}
/**
* Download process files from API and write to a given stream
*
* @param resource $stream Stream to write the downloaded data to
* @param string $remotefile Remote file name which should be downloaded (if there are
* multiple output files available)
*
* @return \CloudConvert\Process
*
* @throws \CloudConvert\Exceptions\ApiException if the CloudConvert API returns an error
* @throws \GuzzleHttp\Exception if there is a general HTTP / network error
*
*/
public function downloadStream($stream, $remotefile = null)
{
if (!isset($this->output->url)) {
throw new Exceptions\ApiException("There is no output file available (yet)", 400);
}
$local = Stream::factory($stream);
$download = $this->api->get($this->output->url . (isset($remotefile) ? '/' . rawurlencode($remotefile) : ''), false, false);
$local->write($download);
return $this;
}
/**
* Download all output process files from API
*
* @param string $directory Local directory the files should be downloaded to
*
* @return \CloudConvert\Process
*
* @throws \CloudConvert\Exceptions\ApiException if the CloudConvert API returns an error
* @throws \GuzzleHttp\Exception if there is a general HTTP / network error
*
*/
public function downloadAll($directory = null)
{
if (!isset($this->output->files)) { // the are not multiple output files -> do normal downloader
return $this->download($directory);
}
foreach ($this->output->files as $file) {
$this->download($directory, $file);
}
return $this;
}
/**
* Delete Process from API
*
* @return \CloudConvert\Process
*
* @throws \CloudConvert\Exceptions\ApiException if the CloudConvert API returns an error
* @throws \GuzzleHttp\Exception if there is a general HTTP / network error
*
*/
public function delete()
{
$this->api->delete($this->url, false, false);
return $this;
}
}