Repository navigation
Expand file tree
/
Copy pathProcess.php
More file actions
144 lines (128 loc) · 4.34 KB
/
Copy pathProcess.php
File metadata and controls
144 lines (128 loc) · 4.34 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
<?php
/**
* This file contains code about \CloudConvert\Process class
*/
namespace CloudConvert;
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 The \CloudConvert\Api instance
* @param $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)
{
$this->data = $this->api->post($this->url, $parameters, 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 $localfile Local file name (or directory) the file should be downloaded to
* @param $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 download($localfile = null, $remotefile = null)
{
if (!isset($this->output->url)) {
throw new Exceptions\ApiException("There is no output file available (yet)", 400);
}
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");
}
$local = Stream::factory(fopen($localfile, 'w'));
$download = $this->api->get($this->output->url . (isset($remotefile) ? '/' . $remotefile : '' ), false, false);
$local->write($download);
return $this;
}
/**
* Download all output process files from API
*
* @param $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)) { // there are not multiple output files -> do normal download
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;
}
}