-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathImage.php
161 lines (136 loc) · 4.41 KB
/
Image.php
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
<?php
declare(strict_types = 1);
namespace Nylas\Schedulers;
use function fopen;
use function is_string;
use function file_exists;
use RuntimeException;
use Nylas\Utilities\API;
use Nylas\Utilities\Options;
use Nylas\Utilities\Validator as V;
use Psr\Http\Message\StreamInterface;
use GuzzleHttp\Exception\GuzzleException;
/**
* ----------------------------------------------------------------------------------
* Nylas Scheduler Image Upload
* ----------------------------------------------------------------------------------
*
* @author lanlin
* @change 2023/07/24
*/
class Image
{
// ------------------------------------------------------------------------------
/**
* @var Options
*/
private Options $options;
// ------------------------------------------------------------------------------
/**
* Scheduler constructor.
*
* @param Options $options
*/
public function __construct(Options $options)
{
$this->options = $options;
$this->options->setSchedulerServer($this->options->getRegion());
}
// ------------------------------------------------------------------------------
/**
* Upload and image. Retrieve a pre-signed S3 URL for uploading.
*
* @param string $pageId
* @param array $params
*
* @return array
* @throws GuzzleException
*/
public function getPreSignedUrl(string $pageId, array $params): array
{
$rules = V::keySet(
V::key('objectName', V::stringType()::notEmpty()),
V::key('contentType', V::stringType()::notEmpty()),
);
V::doValidate($rules, $params);
return $this->options
->getSync()
->setPath($pageId)
->setFormParams($params)
->setHeaderParams($this->options->getAuthorizationHeader())
->put(API::LIST['schedulerUploadImg']);
}
// ------------------------------------------------------------------------------
/**
* Upload an image (same as file upload)
*
* @param string $pageId
* @param array $params
* @param array $image when empty, means upload from $_FILES['image']
*
* tips: when $image is empty, use $_FILES['image']
* <form action="upload.php" method="post" enctype="multipart/form-data">
* <input name="image" type="file" />
* <input type="submit" value="Send files" />
* </form>
*
* @return array
* @throws GuzzleException
*/
public function uploadAnImage(string $pageId, array $params, array $image = []): array
{
if (empty($image))
{
$image = $this->concatUploadImage();
}
V::doValidate($this->multipartRules(), $image);
$image['name'] ??= 'file';
if (is_string($image['contents']) && file_exists($image['contents']))
{
$image['contents'] = fopen($image['contents'], 'rb');
}
$signed = $this->getPreSignedUrl($pageId, $params);
return $this->options
->getSync()
->setFormFiles($image)
->setHeaderParams($this->options->getAuthorizationHeader())
->post($signed['signedUrl']);
}
// ------------------------------------------------------------------------------
/**
* multipart upload rules
*
* @return V
*/
private function multipartRules(): V
{
return V::keySet(
V::key('name', V::stringType()::notEmpty(), false),
V::key('headers', V::arrayType(), false),
V::key('filename', V::stringType()::notEmpty(), false),
V::key('contents', V::oneOf(
V::resourceType(),
V::stringType()::notEmpty(),
V::instance(StreamInterface::class)
))
);
}
// ------------------------------------------------------------------------------
/**
* @return array
*/
private function concatUploadImage(): array
{
if (empty($_FILES['image']) || empty($_FILES['image']['tmp_name']))
{
throw new RuntimeException('None upload image found');
}
return [
'name' => 'file',
'headers' => [],
'filename' => $_FILES['image']['name'],
'contents' => $_FILES['image']['tmp_name'],
];
}
// ------------------------------------------------------------------------------
}