|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Codemash; |
| 4 | + |
| 5 | +use Codemash\Exceptions\RequestValidationException; |
| 6 | +use Codemash\Params\CodemashFileParams; |
| 7 | +use GuzzleHttp\Exception\GuzzleException; |
| 8 | + |
| 9 | +class CodemashFile |
| 10 | +{ |
| 11 | + private CodemashClient $client; |
| 12 | + private string $uriPrefix = 'v2/'; |
| 13 | + |
| 14 | + public function __construct(CodemashClient $client) |
| 15 | + { |
| 16 | + $this->client = $client; |
| 17 | + } |
| 18 | + |
| 19 | + private function prepUploadFileOptions(array $params): array |
| 20 | + { |
| 21 | + if ($params['base64']) { |
| 22 | + $options = [ |
| 23 | + 'headers' => [ |
| 24 | + 'Accept' => 'application/json', |
| 25 | + 'Content-Type' => 'application/json', |
| 26 | + ], |
| 27 | + 'body' => toJson([ |
| 28 | + 'base64File' => [ |
| 29 | + 'data' => $params['base64'], |
| 30 | + 'contentType' => $params['fileType'], |
| 31 | + 'fileName' => $params['fileName'], |
| 32 | + ], |
| 33 | + 'path' => $params['path'] ?? null, |
| 34 | + 'recordId' => $params['recordId'] ?? null, |
| 35 | + 'uniqueFieldName' => $params['uniqueFieldName'] ?? null, |
| 36 | + 'userId' => $params['userId'] ?? null, |
| 37 | + 'metaFieldName' => $params['metaFieldName'] ?? null, |
| 38 | + ]), |
| 39 | + ]; |
| 40 | + } else { |
| 41 | + $options = [ |
| 42 | + 'multipart' => [ |
| 43 | + [ |
| 44 | + 'name' => 'path', |
| 45 | + 'contents' => $params['path'] ?? null, |
| 46 | + ], |
| 47 | + [ |
| 48 | + 'name' => 'recordId', |
| 49 | + 'contents' => $params['recordId'] ?? null, |
| 50 | + ], |
| 51 | + [ |
| 52 | + 'name' => 'uniqueFieldName', |
| 53 | + 'contents' => $params['uniqueFieldName'] ?? null, |
| 54 | + ], |
| 55 | + [ |
| 56 | + 'name' => 'userId', |
| 57 | + 'contents' => $params['userId'] ?? null, |
| 58 | + ], |
| 59 | + [ |
| 60 | + 'name' => 'metaFieldName', |
| 61 | + 'contents' => $params['metaFieldName'] ?? null, |
| 62 | + ], |
| 63 | + [ |
| 64 | + 'name' => 'file', |
| 65 | + 'contents' => file_get_contents($params['fileUri']), |
| 66 | + 'filename' => $params['fileName'], |
| 67 | + ], |
| 68 | + ], |
| 69 | + ]; |
| 70 | + } |
| 71 | + |
| 72 | + return $options; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @throws GuzzleException |
| 77 | + * @throws RequestValidationException |
| 78 | + */ |
| 79 | + public function uploadFile(array $params): array |
| 80 | + { |
| 81 | + $params = CodemashFileParams::prepUploadFileParams($params); |
| 82 | + |
| 83 | + $options = $this->prepUploadFileOptions($params); |
| 84 | + |
| 85 | + $response = $this->client->request('POST', $this->uriPrefix . 'files', $options); |
| 86 | + |
| 87 | + return $response['result']; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @throws GuzzleException |
| 92 | + * @throws RequestValidationException |
| 93 | + */ |
| 94 | + public function uploadRecordFile(array $params): array |
| 95 | + { |
| 96 | + $params = CodemashFileParams::prepUploadRecordFileParams($params); |
| 97 | + |
| 98 | + $options = $this->prepUploadFileOptions($params); |
| 99 | + |
| 100 | + $response = $this->client->request('POST', $this->uriPrefix . 'db/' . $params['collectionName'] . '/files', $options); |
| 101 | + |
| 102 | + return $response['result']; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * @throws GuzzleException |
| 107 | + * @throws RequestValidationException |
| 108 | + */ |
| 109 | + public function uploadUserFile(array $params): array |
| 110 | + { |
| 111 | + $params = CodemashFileParams::prepUploadUserFileParams($params); |
| 112 | + |
| 113 | + $options = $this->prepUploadFileOptions($params); |
| 114 | + |
| 115 | + $response = $this->client->request('POST', $this->uriPrefix . 'membership/users/files', $options); |
| 116 | + |
| 117 | + return $response['result']; |
| 118 | + } |
| 119 | +} |
0 commit comments