-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCSV.php
More file actions
327 lines (289 loc) · 9.81 KB
/
CSV.php
File metadata and controls
327 lines (289 loc) · 9.81 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<?php
namespace Utopia\Migration\Destinations;
use Utopia\Database\Exception\Authorization;
use Utopia\Database\Exception\Conflict;
use Utopia\Database\Exception\Structure;
use Utopia\Migration\Destination;
use Utopia\Migration\Resource as UtopiaResource;
use Utopia\Migration\Resources\Database\Row;
use Utopia\Migration\Transfer;
use Utopia\Migration\Warning;
use Utopia\Storage\Device;
use Utopia\Storage\Device\Local;
class CSV extends Destination
{
protected Device $deviceForFiles;
protected string $resourceId;
protected string $directory;
protected string $outputFile;
protected Local $local;
protected array $allowedColumns = [];
/**
* @throws Authorization
* @throws Structure
* @throws Conflict
* @throws \Exception
*/
public function __construct(
Device $deviceForFiles,
string $resourceId,
string $directory,
string $filename,
array $allowedColumns = [],
private readonly string $delimiter = ',',
private readonly string $enclosure = '"',
private readonly string $escape = '"',
private readonly bool $includeHeaders = true,
) {
$this->deviceForFiles = $deviceForFiles;
$this->resourceId = $resourceId;
$this->directory = $directory;
$this->outputFile = $this->sanitizeFilename($filename);
$this->local = new Local(\sys_get_temp_dir() . '/csv_export_' . uniqid());
$this->local->setTransferChunkSize(Transfer::STORAGE_MAX_CHUNK_SIZE);
$this->createDirectory($this->local->getRoot());
foreach ($allowedColumns as $attribute) {
$this->allowedColumns[$attribute] = true;
}
}
public static function getName(): string
{
return 'CSV';
}
public static function getSupportedResources(): array
{
return [
UtopiaResource::TYPE_ROW,
];
}
public function report(array $resources = [], array $resourceIds = []): array
{
return [];
}
/**
* @param array<Row> $resources
* @throws \JsonException
* @throws \Exception
*/
protected function import(array $resources, callable $callback): void
{
$handle = null; // file handle
$buffer = ['lines' => [], 'size' => 0]; // Buffer for batching writes
$bufferBytes = 1024 * 1024; // 1MB
$log = $this->local->getRoot() . '/' . $this->outputFile . '.csv';
$flushBuffer = function () use ($log, &$handle, &$buffer) {
if (empty($buffer['lines'])) {
return;
}
try {
if (!isset($handle)) {
$handle = \fopen($log, 'a');
if ($handle === false) {
throw new \Exception("Failed to open file for writing: $log");
}
}
foreach ($buffer['lines'] as $line) {
if (!$this->writeCSVLine($handle, $line)) {
throw new \Exception("Failed to write CSV line to file: $log");
}
}
$buffer = [
'lines' => [],
'size' => 0
];
} catch (\Exception $e) {
// Close handle on error
if (isset($handle)) {
\fclose($handle);
unset($handle);
}
throw $e;
}
};
try {
foreach ($resources as $resource) {
if (!($resource instanceof Row)) {
continue;
}
$csvData = $this->resourceToCSVData($resource);
// Write headers if this is the first row of the file
if (!isset($csvHeader) && $this->includeHeaders) {
$headers = \array_keys($csvData);
$buffer['lines'][] = $headers;
$buffer['size'] += \strlen(\implode($this->delimiter, $headers)) + 2; // Approximate size
$csvHeader = true;
}
$dataValues = \array_values($csvData);
$buffer['lines'][] = $dataValues;
$buffer['size'] += \strlen(\implode($this->delimiter, $dataValues)) + 2; // Approximate size
if ($buffer['size'] >= $bufferBytes) {
$flushBuffer();
}
$resource->setStatus(UtopiaResource::STATUS_SUCCESS);
if (isset($this->cache)) {
$this->cache->update($resource);
}
}
// Flush any remaining buffered lines
if (!empty($buffer['lines'])) {
$flushBuffer();
}
} finally {
if (\is_resource($handle)) {
\fclose($handle);
}
}
$callback($resources);
}
/**
* @throws \Exception
*/
public function shutdown(): void
{
$filename = $this->outputFile . '.csv';
$sourcePath = $this->local->getPath($filename);
$destPath = $this->deviceForFiles->getPath($this->directory . '/' . $filename);
if (!$this->local->exists($sourcePath)) {
throw new \Exception("No data to export for resource: $this->resourceId");
}
try {
$result = $this->local->transfer(
$sourcePath,
$destPath,
$this->deviceForFiles
);
if ($result === false) {
throw new \Exception('Error transferring to ' . $this->deviceForFiles->getRoot() . '/' . $filename);
}
if (!$this->deviceForFiles->exists($destPath)) {
throw new \Exception('File not found on destination: ' . $destPath);
}
} finally {
// Clean up the temporary directory
if (!$this->local->deletePath('') || $this->local->exists($this->local->getRoot())) {
$this->addWarning(new Warning(
UtopiaResource::TYPE_ROW,
Transfer::GROUP_DATABASES,
'Error cleaning up: ' . $this->local->getRoot(),
$this->resourceId
));
}
}
}
/**
* Write a CSV line with RFC 4180 compliant escaping (double-quote method)
*
* @param resource $handle
* @param array $fields
* @return bool
*/
protected function writeCSVLine($handle, array $fields): bool
{
$parts = [];
foreach ($fields as $field) {
$field = (string)$field;
if (\strpbrk($field, $this->delimiter . "\n\r" . $this->enclosure) !== false) {
$parts[] = $this->enclosure . \str_replace($this->enclosure, $this->enclosure . $this->enclosure, $field) . $this->enclosure;
} else {
$parts[] = $field;
}
}
return \fwrite($handle, \implode($this->delimiter, $parts) . "\n") !== false;
}
/**
* Helper to ensure a directory exists.
* @throws \Exception
*/
protected function createDirectory(string $path): void
{
if (!\file_exists($path)) {
if (!\mkdir($path, 0755, true)) {
throw new \Exception('Error creating directory: ' . $path);
}
}
}
/**
* Sanitize a filename to make it filesystem-safe
*/
protected function sanitizeFilename(string $filename): string
{
// Replace problematic characters with underscores
$sanitized = \preg_replace('/[:\\/<>"|*?]/', '_', $filename);
$sanitized = \preg_replace('/[^\x20-\x7E]/', '_', $sanitized);
$sanitized = \trim($sanitized);
return empty($sanitized) ? 'export' : $sanitized;
}
/**
* Convert a resource to CSV-compatible data
*/
protected function resourceToCSVData(Row $resource): array
{
$rowData = $resource->getData();
$data = [
'$id' => $resource->getId(),
'$permissions' => $resource->getPermissions(),
'$createdAt' => $rowData['$createdAt'] ?? '',
'$updatedAt' => $rowData['$updatedAt'] ?? '',
];
unset(
$rowData['$createdAt'],
$rowData['$updatedAt'],
);
// Add all attributes if no filter specified, otherwise only allowed ones
if (empty($this->allowedColumns)) {
$data = \array_merge($data, $rowData);
} else {
foreach ($rowData as $key => $value) {
if (isset($this->allowedColumns[$key])) {
$data[$key] = $value;
}
}
}
foreach ($data as $key => $value) {
$data[$key] = $this->convertValueToCSV($value);
}
return $data;
}
/**
* Convert a single value to CSV-compatible format
*/
protected function convertValueToCSV(mixed $value): string
{
if (\is_null($value)) {
return 'null';
}
if (\is_bool($value)) {
return $value ? 'true' : 'false';
}
if (\is_array($value)) {
return $this->convertArrayToCSV($value);
}
if (\is_object($value)) {
return $this->convertObjectToCSV($value);
}
return (string)$value;
}
/**
* Convert array to CSV format
*/
protected function convertArrayToCSV(array $value): string
{
if (empty($value)) {
return '';
}
if (isset($value['$id'])) {
return $value['$id'];
}
return \json_encode($value);
}
/**
* Convert object to CSV format
*/
protected function convertObjectToCSV($value): string
{
if ($value instanceof Row) {
return $value->getId();
}
return \json_encode($value);
}
}