ReviewOS

also looking at this

meemalabs/meema-client-php

WIP: Upload Endpoint

#38
Merged glennmichael123 wants to merge upload-endpoint into main
6 files +172 -94
src/Models/Media.phpmodified+95-27
Changes to src/Models/Media.php
@@ -2,6 +2,9 @@
22
33namespace Meema\MeemaApi\Models;
44
5use GuzzleHttp\Client as GuzzleClient;
6use GuzzleHttp\Psr7;
7use GuzzleHttp\Psr7\Request;
58use Meema\MeemaApi\Client;
69use Meema\MeemaApi\Exceptions\InvalidFormatException;
710use Meema\MeemaApi\Response\Response;
@@ -46,21 +49,23 @@ class Media
4649 /**
4750 * Get specific media.
4851 *
52 * @param array $ids
53 *
4954 * @return array
5055 *
5156 * @throws InvalidFormatException
5257 */
53 public function get($id = null)
58 public function get($ids = null)
5459 {
5560 if ($this->model) {
5661 return $this->fetchForModel();
5762 }
5863
59 if (! $id) {
64 if (! $ids) {
6065 return $this->all();
6166 }
6267
63 $ids = is_array($id) ? $id : func_get_args();
68 $ids = is_array($ids) ? $ids : func_get_args();
6469
6570 foreach ($ids as $id) {
6671 if (! is_int($id)) {
@@ -71,6 +76,18 @@ class Media
7176 return $this->client->request('GET', 'media', ['media_ids' => $ids]);
7277 }
7378
79 /**
80 * Search for specific media.
81 *
82 * @param string $query
83 *
84 * @return array
85 */
86 public function search($query)
87 {
88 return $this->client->request('POST', 'media/search', compact('query'));
89 }
90
7491 /**
7592 * Get specific media.
7693 *
@@ -129,15 +146,15 @@ class Media
129146 /**
130147 * Delete a media.
131148 *
132 * @param int $id
149 * @param array $ids
133150 *
134151 * @return null
135152 *
136153 * @throws InvalidFormatException
137154 */
138 public function delete($id)
155 public function delete($ids)
139156 {
140 $ids = is_array($id) ? $id : func_get_args();
157 $ids = is_array($ids) ? $ids : func_get_args();
141158
142159 foreach ($ids as $id) {
143160 if (! is_int($id)) {
@@ -156,29 +173,80 @@ class Media
156173 }
157174
158175 /**
159 * Get specific folders.
176 * Upload a media file.
160177 *
161 * @param string $query
178 * @param string $path
162179 *
163180 * @return array
164181 */
165 public function search($query)
182 public function upload($path)
166183 {
167 return $this->client->request('POST', 'media/search', compact('query'));
184 $file = fopen($path, 'r');
185 $stream = Psr7\stream_for($file);
186
187 $fileName = basename($path);
188 $mimeType = mime_content_type($file);
189
190 $vaporParams = ['content_type' => $mimeType];
191
192 $signedUrl = $this->client->request('POST', 'vapor/signed-storage-url', $vaporParams);
193
194 if (is_array($signedUrl) && $signedUrl['url']) {
195 $headers = $signedUrl['headers'];
196 unset($headers['Host']);
197
198 $this->uploadToS3($signedUrl['url'], $headers, $fileName, $stream);
199
200 $uploadData = ['key' => $signedUrl['key'], 'file_name' => $fileName];
201
202 $response = $this->client->request('POST', 'upload', $uploadData);
203 }
204
205 return $response;
206 }
207
208 /**
209 * Upload the file stream to s3.
210 *
211 * @param string $signedUrl
212 * @param array $headers
213 * @param string $fileName
214 * @param GuzzleHttp\Psr7 $stream
215 *
216 * @return void
217 */
218 protected function uploadToS3($signedUrl, $headers, $fileName, $stream)
219 {
220 $client = new GuzzleClient();
221 $request = new Request(
src/Models/Media.php:221
  1. chrisSun Oct 25 2026 00:09:39 GMT+0000 (Coordinated Universal Time)

    let's only pass the $signedUrl and not the array, please

222 'PUT',
223 $signedUrl,
224 ['headers' => json_encode($headers)],
225 new Psr7\MultipartStream(
226 [
227 [
228 'name' => $fileName,
229 'contents' => $stream,
230 ],
231 ]
232 )
233 );
234
235 $client->send($request);
168236 }
169237
170238 /**
171239 * Archive a media.
src/Models/Media.php:239
  1. chrisSun Oct 25 2026 00:09:39 GMT+0000 (Coordinated Universal Time)

    We should rename this var to $ids but at the same time we should also accept an int-value for just logical reasons and to cover this edge-case in case the user just enters an id.

172240 *
173 * @param int $id
241 * @param array $ids
174242 *
175243 * @return array
176244 *
177245 * @throws InvalidFormatException
178246 */
179 public function archive($id)
247 public function archive($ids)
180248 {
181 $ids = is_array($id) ? $id : func_get_args();
249 $ids = is_array($ids) ? $ids : func_get_args();
182250
183251 foreach ($ids as $id) {
184252 if (! is_int($id)) {
@@ -199,15 +267,15 @@ class Media
199267 /**
200268 * Unarchive a media.
201269 *
202 * @param int $id
270 * @param array $ids
203271 *
204272 * @return array
205273 *
206274 * @throws InvalidFormatException
207275 */
208 public function unarchive($id)
276 public function unarchive($ids)
209277 {
210 $ids = is_array($id) ? $id : func_get_args();
278 $ids = is_array($ids) ? $ids : func_get_args();
211279
212280 foreach ($ids as $id) {
213281 if (! is_int($id)) {
@@ -228,15 +296,15 @@ class Media
228296 /**
229297 * Make a media private.
230298 *
231 * @param int $id
299 * @param array $ids
232300 *
233301 * @return array
234302 *
235303 * @throws InvalidFormatException
236304 */
237 public function makePrivate($id)
305 public function makePrivate($ids)
238306 {
239 $ids = is_array($id) ? $id : func_get_args();
307 $ids = is_array($ids) ? $ids : func_get_args();
240308
241309 foreach ($ids as $id) {
242310 if (! is_int($id)) {
@@ -257,15 +325,15 @@ class Media
257325 /**
258326 * Make a media public.
259327 *
260 * @param int $id
328 * @param array $ids
261329 *
262330 * @return array
263331 *
264332 * @throws InvalidFormatException
265333 */
266 public function makePublic($id)
334 public function makePublic($ids)
267335 {
268 $ids = is_array($id) ? $id : func_get_args();
336 $ids = is_array($ids) ? $ids : func_get_args();
269337
270338 foreach ($ids as $id) {
271339 if (! is_int($id)) {
@@ -286,15 +354,15 @@ class Media
286354 /**
287355 * Duplicate a media.
288356 *
289 * @param int $id
357 * @param array $ids
290358 *
291359 * @return array
292360 *
293361 * @throws InvalidFormatException
294362 */
295 public function duplicate($id)
363 public function duplicate($ids)
296364 {
297 $ids = is_array($id) ? $id : func_get_args();
365 $ids = is_array($ids) ? $ids : func_get_args();
298366
299367 foreach ($ids as $id) {
300368 if (! is_int($id)) {
@@ -339,7 +407,7 @@ class Media
339407 /**
340408 * Fetch the media for the folder.
341409 *
342 * @param int $id
410 * @param Meema\MeemaApi\Models\Folder $folder
343411 *
344412 * @return self
345413 */
@@ -353,7 +421,7 @@ class Media
353421 /**
354422 * Initialize media model.
355423 *
356 * @param Meema\MeemaApi\Models\Folder $folder
424 * @param Meema\MeemaApi\Models\Tag $tag
357425 *
358426 * @return self
359427 */