ReviewOS

also looking at this

meemalabs/meema-client-php

Storage Class

#59
Merged glennmichael123 wants to merge storage-class into main
3 files +175 -105
src/Models/Storage.phpadded+164-0
Changes to src/Models/Storage.php
@@ -0,0 +1,164 @@
1<?php
2
3namespace Meema\MeemaApi\Models;
4
5use GuzzleHttp\Client as GuzzleClient;
6use GuzzleHttp\Psr7;
7use GuzzleHttp\Psr7\Request;
8use Meema\MeemaApi\Client;
9
10class Storage
11{
12 /**
13 * @var \Meema\MeemaApi\Client
14 */
15 protected $client;
16
17 /**
18 * Construct storage model.
19 *
20 * @param Meema\MeemaApi\Client $client
21 */
22 public function __construct(Client $client)
23 {
24 $this->client = $client;
25 }
26
27 /**
28 * Upload a media file.
29 *
30 * @param string $path
31 *
32 * @return array
33 */
34 public function upload($path)
35 {
36 $file = fopen($path, 'r');
37 $stream = Psr7\stream_for($file);
38
39 $fileName = basename($path);
40 $mimeType = mime_content_type($file);
41
42 $vaporParams = ['content_type' => $mimeType];
43
44 $signedUrl = $this->client->request('POST', 'vapor/signed-storage-url', $vaporParams);
45
46 if (is_array($signedUrl) && $signedUrl['url']) {
47 $headers = $signedUrl['headers'];
48 unset($headers['Host']);
49
50 $this->uploadToS3($signedUrl['url'], $headers, $fileName, $stream);
51
52 $uploadData = ['key' => $signedUrl['key'], 'file_name' => $fileName];
53
54 $response = $this->client->request('POST', 'upload', $uploadData);
src/Models/Storage.php:54
  1. chrisSun Oct 25 2026 00:09:39 GMT+0000 (Coordinated Universal Time)

    $response may not be defined at this point

55 }
56
57 return $response;
58 }
59
60 /**
61 * Upload the file stream to s3.
62 *
63 * @param string $signedUrl
64 * @param array $headers
65 * @param string $fileName
66 * @param GuzzleHttp\Psr7 $stream
67 *
68 * @return void
69 */
70 protected function uploadToS3($signedUrl, $headers, $fileName, $stream)
71 {
72 $client = new GuzzleClient();
73 $request = new Request(
74 'PUT',
75 $signedUrl,
76 ['headers' => json_encode($headers)],
77 new Psr7\MultipartStream(
78 [
79 [
80 'name' => $fileName,
81 'contents' => $stream,
82 ],
83 ]
84 )
85 );
86
87 $client->send($request);
88 }
89
90 /**
91 * Set the visibility for a file.
92 *
93 * @param string $path
94 * @param string $visibility
95 *
96 * @return array|false file meta data
97 */
98 public function setVisibility($path, $visibility)
99 {
100 return $this->client->request('POST', 'storage/set-visibility', compact('path', 'visibility'));
101 }
102
103 /**
104 * Check whether a file exists.
105 *
106 * @param string $path
107 *
108 * @return bool
109 */
110 public function has($path)
111 {
112 $data = $this->client->request('POST', 'storage/has', compact('path'));
113
114 return $data['exists'] ?? false;
115 }
116
117 /**
118 * Get all the meta data of a file or directory.
119 *
120 * @param string $path
121 *
122 * @return false|array
123 */
124 public function getMetadata($path)
125 {
126 return $this->client->request('POST', 'storage/metadata', compact('path'));
127 }
128
129 /**
130 * Destroy the file in storage.
131 *
132 * @param string $path
133 *
134 * @return array
135 */
136 public function destroyFile($path)
137 {
138 return $this->client->request('POST', 'storage/destroy', compact('path'));
139 }
140
141 /**
142 * Destroy the directory in storage.
143 *
144 * @param string $directory
145 *
146 * @return array
147 */
148 public function destroyDirectory($directory)
149 {
150 return $this->client->request('POST', 'storage/destroy-directory', compact('directory'));
151 }
152
153 /**
154 * Create a directory in storage.
155 *
156 * @param string $directory
157 *
158 * @return array
159 */
160 public function createDirectory($directory)
161 {
162 return $this->client->request('POST', 'storage/create-directory', compact('directory'));
163 }
164}