Skip to content

Commit 7a9c8be

Browse files
committed
Add dir + file name
1 parent 8931241 commit 7a9c8be

1 file changed

Lines changed: 19 additions & 13 deletions

File tree

src/Migration/Destinations/CSV.php

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515

1616
class CSV extends Destination
1717
{
18-
protected Device $deviceForMigrations;
18+
protected Device $deviceForFiles;
1919
protected string $resourceId;
20+
protected string $directory;
2021
protected string $outputFile;
2122
protected Local $local;
2223

@@ -29,17 +30,20 @@ class CSV extends Destination
2930
* @throws \Exception
3031
*/
3132
public function __construct(
32-
Device $deviceForExports,
33+
Device $deviceForFiles,
3334
string $resourceId,
35+
string $directory,
36+
string $filename,
3437
array $allowedColumns = [],
3538
private readonly string $delimiter = ',',
3639
private readonly string $enclosure = '"',
3740
private readonly string $escape = '\\',
3841
private readonly bool $includeHeaders = true,
3942
) {
40-
$this->deviceForMigrations = $deviceForExports;
43+
$this->deviceForFiles = $deviceForFiles;
4144
$this->resourceId = $resourceId;
42-
$this->outputFile = $this->sanitizeFilename($resourceId);
45+
$this->directory = $directory;
46+
$this->outputFile = $this->sanitizeFilename($filename);
4347
$this->local = new Local(\sys_get_temp_dir() . '/csv_export_' . uniqid());
4448
$this->local->setTransferChunkSize(Transfer::STORAGE_MAX_CHUNK_SIZE);
4549
$this->createDirectory($this->local->getRoot());
@@ -159,26 +163,26 @@ protected function import(array $resources, callable $callback): void
159163
public function shutdown(): void
160164
{
161165
$filename = $this->outputFile . '.csv';
162-
$sourceFilePath = $this->local->getPath($filename);
163-
$destFilePath = $this->deviceForMigrations->getPath($filename);
166+
$sourcePath = $this->local->getPath($filename);
167+
$destPath = $this->deviceForFiles->getPath($this->directory . '/' . $filename);
164168

165169
// Check if the CSV file was actually created
166-
if (!$this->local->exists($sourceFilePath)) {
170+
if (!$this->local->exists($sourcePath)) {
167171
throw new \Exception("No data to export for resource: $this->resourceId");
168172
}
169173

170174
try {
171175
// Transfer expects absolute paths within each device
172176
$result = $this->local->transfer(
173-
$filename,
174-
$filename,
175-
$this->deviceForMigrations
177+
$sourcePath,
178+
$destPath,
179+
$this->deviceForFiles
176180
);
177181
if ($result === false) {
178-
throw new \Exception('Error transferring to ' . $this->deviceForMigrations->getRoot() . '/' . $filename);
182+
throw new \Exception('Error transferring to ' . $this->deviceForFiles->getRoot() . '/' . $filename);
179183
}
180-
if (!$this->deviceForMigrations->exists($destFilePath)) {
181-
throw new \Exception('File not found on destination: ' . $destFilePath);
184+
if (!$this->deviceForFiles->exists($destPath)) {
185+
throw new \Exception('File not found on destination: ' . $destPath);
182186
}
183187
} finally {
184188
// Clean up the temporary directory
@@ -221,6 +225,8 @@ protected function resourceToCSVData(Row $resource): array
221225
$data = [
222226
'$id' => $resource->getId(),
223227
'$permissions' => $resource->getPermissions(),
228+
'$createdAt' => $resource->getCreatedAt(),
229+
'$updatedAt' => $resource->getUpdatedAt(),
224230
];
225231

226232
// Add all attributes if no filter specified, otherwise only allowed ones

0 commit comments

Comments
 (0)