|
5 | 5 | namespace IPFS\Client; |
6 | 6 |
|
7 | 7 | use IPFS\Exception\IPFSTransportException; |
| 8 | +use IPFS\Helper\FilesystemHelper; |
8 | 9 | use IPFS\Model\File; |
| 10 | +use IPFS\Model\Directory; |
9 | 11 | use IPFS\Model\ListFileEntry; |
10 | 12 | use IPFS\Model\Node; |
11 | 13 | use IPFS\Model\Peer; |
|
14 | 16 | use IPFS\Transformer\FileLinkTransformer; |
15 | 17 | use IPFS\Transformer\FileListTransformer; |
16 | 18 | use IPFS\Transformer\FileTransformer; |
| 19 | +use IPFS\Transformer\DirectoryTransformer; |
17 | 20 | use IPFS\Transformer\NodeTransformer; |
18 | 21 | use IPFS\Transformer\PeerIdentityTransformer; |
19 | 22 | use IPFS\Transformer\PeerStreamTransformer; |
20 | 23 | use IPFS\Transformer\PeerTransformer; |
21 | 24 | use IPFS\Transformer\PingTransformer; |
22 | 25 | use IPFS\Transformer\VersionTransformer; |
| 26 | +use Symfony\Component\Mime\Part\DataPart; |
| 27 | +use Symfony\Component\Mime\Part\Multipart\FormDataPart; |
23 | 28 |
|
24 | 29 | class IPFSClient |
25 | 30 | { |
@@ -63,6 +68,99 @@ public function add(string $file, array $parameters = []): File |
63 | 68 | return $transformer->transform($parsedResponse); |
64 | 69 | } |
65 | 70 |
|
| 71 | + public function addFile(string $path, array $parameters = []): File |
| 72 | + { |
| 73 | + if (is_file($path) === false) { |
| 74 | + throw new \InvalidArgumentException('Path must be a file.'); |
| 75 | + } |
| 76 | + $parameters['wrap-with-directory'] = false; |
| 77 | + |
| 78 | + $response = $this->httpClient->request('POST', '/api/v0/add', [ |
| 79 | + 'body' => [ |
| 80 | + 'file' => fopen($path, 'r'), |
| 81 | + ], |
| 82 | + 'query' => $parameters, |
| 83 | + 'headers' => [ |
| 84 | + 'Content-Type' => 'multipart/form-data', |
| 85 | + ], |
| 86 | + ]); |
| 87 | + |
| 88 | + $parsedResponse = json_decode($response, true); |
| 89 | + |
| 90 | + $transformer = new FileTransformer(); |
| 91 | + return $transformer->transform($parsedResponse); |
| 92 | + } |
| 93 | + |
| 94 | + public function addDirectory(string $path, array $parameters = []): Directory |
| 95 | + { |
| 96 | + if (is_dir($path) === false) { |
| 97 | + throw new \InvalidArgumentException('Path must be a directory.'); |
| 98 | + } |
| 99 | + $parameters['wrap-with-directory'] = true; |
| 100 | + |
| 101 | + $files = FilesystemHelper::listFiles($path); |
| 102 | + |
| 103 | + $dataParts = []; |
| 104 | + $basePath = realpath($path); |
| 105 | + if ($basePath === false) { |
| 106 | + throw new \UnexpectedValueException('Path ' . $path . ' does not exist.'); |
| 107 | + } |
| 108 | + |
| 109 | + foreach ($files as $filePath) { |
| 110 | + $filePathReal = realpath($filePath); |
| 111 | + if ($filePathReal === false) { |
| 112 | + throw new \UnexpectedValueException('Path ' . $path . ' does not exist.'); |
| 113 | + } |
| 114 | + |
| 115 | + if (str_starts_with($filePathReal, $basePath) === false) { |
| 116 | + throw new \RuntimeException("File $filePath is outside of base path."); |
| 117 | + } |
| 118 | + |
| 119 | + $relativePath = mb_substr($filePathReal, mb_strlen($basePath) + 1); // +1 to remove leading slash |
| 120 | + $relativePath = str_replace(\DIRECTORY_SEPARATOR, '/', $relativePath); // For Windows |
| 121 | + |
| 122 | + $fileHandle = fopen($filePath, 'r'); |
| 123 | + if ($fileHandle === false) { |
| 124 | + throw new \RuntimeException('Unable to open file ' . $filePath); |
| 125 | + } |
| 126 | + |
| 127 | + $dataParts[] = new DataPart($fileHandle, $relativePath); |
| 128 | + } |
| 129 | + |
| 130 | + // Use Symfony's FormDataPart to build the body + headers |
| 131 | + $formData = new FormDataPart([ |
| 132 | + 'file' => $dataParts, |
| 133 | + ]); |
| 134 | + |
| 135 | + $response = $this->httpClient->request('POST', '/api/v0/add', [ |
| 136 | + 'body' => $formData->bodyToString(), |
| 137 | + 'query' => $parameters, |
| 138 | + 'headers' => $formData->getPreparedHeaders()->toArray(), |
| 139 | + ]); |
| 140 | + |
| 141 | + $parts = explode("\n", $response); |
| 142 | + $filtered = array_filter($parts, function (string $value) { |
| 143 | + return mb_strlen(trim($value)) > 0; |
| 144 | + }); |
| 145 | + $deserializedParts = array_map(fn (string $part) => json_decode($part, true), $filtered); |
| 146 | + // Sort by size, larger first |
| 147 | + usort($deserializedParts, function ($a, $b) { |
| 148 | + return (int) $a['Size'] < (int) $b['Size'] ? 1 : -1; |
| 149 | + }); |
| 150 | + |
| 151 | + $directoryTransformer = new DirectoryTransformer(); |
| 152 | + |
| 153 | + $directoryData = array_shift($deserializedParts); |
| 154 | + $directory = $directoryTransformer->transform($directoryData); |
| 155 | + |
| 156 | + $fileTransformer = new FileTransformer(); |
| 157 | + foreach ($deserializedParts as $part) { |
| 158 | + $directory->addFile($fileTransformer->transform($part)); |
| 159 | + } |
| 160 | + |
| 161 | + return $directory; |
| 162 | + } |
| 163 | + |
66 | 164 | public function cat(string $hash, int $offset = 0, ?int $length = null): string |
67 | 165 | { |
68 | 166 | return $this->httpClient->request('POST', '/api/v0/cat', [ |
|
0 commit comments