Skip to content

Commit cb7badd

Browse files
committed
Download to local
1 parent 42da0d3 commit cb7badd

1 file changed

Lines changed: 24 additions & 11 deletions

File tree

src/Migration/Sources/CSV.php

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Utopia\Database\Database as UtopiaDatabase;
66
use Utopia\Migration\Exception;
7-
use Utopia\Migration\Resource;
7+
use Utopia\Migration\Resource as UtopiaResource;
88
use Utopia\Migration\Resources\Database\Attribute;
99
use Utopia\Migration\Resources\Database\Collection;
1010
use Utopia\Migration\Resources\Database\Database;
@@ -15,6 +15,7 @@
1515
use Utopia\Migration\Sources\Appwrite\Reader\Database as DatabaseReader;
1616
use Utopia\Migration\Transfer;
1717
use Utopia\Storage\Device;
18+
use Utopia\Storage\Storage;
1819

1920
class CSV extends Source
2021
{
@@ -49,7 +50,7 @@ public static function getName(): string
4950
public static function getSupportedResources(): array
5051
{
5152
return [
52-
Resource::TYPE_DOCUMENT,
53+
UtopiaResource::TYPE_DOCUMENT,
5354
];
5455
}
5556

@@ -68,7 +69,7 @@ public function report(array $resources = []): array
6869
$file->seek(PHP_INT_MAX);
6970
$rowCount = max(0, $file->key());
7071

71-
$report[Resource::TYPE_DOCUMENT] = $rowCount;
72+
$report[UtopiaResource::TYPE_DOCUMENT] = $rowCount;
7273

7374
return $report;
7475
}
@@ -84,13 +85,13 @@ protected function exportGroupAuth(int $batchSize, array $resources): void
8485
protected function exportGroupDatabases(int $batchSize, array $resources): void
8586
{
8687
try {
87-
if (\in_array(Resource::TYPE_DOCUMENT, $resources)) {
88+
if (\in_array(UtopiaResource::TYPE_DOCUMENT, $resources)) {
8889
$this->exportDocuments($batchSize);
8990
}
9091
} catch (\Throwable $e) {
9192
$this->addError(
9293
new Exception(
93-
Resource::TYPE_DOCUMENT,
94+
UtopiaResource::TYPE_DOCUMENT,
9495
Transfer::GROUP_DATABASES,
9596
message: $e->getMessage(),
9697
code: $e->getCode(),
@@ -301,21 +302,33 @@ protected function exportGroupFunctions(int $batchSize, array $resources): void
301302
throw new \Exception('Not Implemented');
302303
}
303304

304-
private function withCsvStream(callable $fn): void
305+
/**
306+
* @param callable(resource $stream): void $callback
307+
* @return void
308+
*/
309+
private function withCsvStream(callable $callback): void
305310
{
306-
if (! $this->device->exists($this->filePath)) {
311+
if (!$this->device->exists($this->filePath)) {
307312
return;
308313
}
309314

310-
$stream = fopen($this->filePath, 'r');
311-
if (! $stream) {
315+
if ($this->device->getType() !== Storage::DEVICE_LOCAL) {
316+
$this->device->transfer(
317+
$this->filePath,
318+
$this->filePath,
319+
new Device\Local('/'),
320+
);
321+
}
322+
323+
$stream = \fopen($this->filePath, 'r');
324+
if (!$stream) {
312325
return;
313326
}
314327

315328
try {
316-
$fn($stream);
329+
$callback($stream);
317330
} finally {
318-
fclose($stream);
331+
\fclose($stream);
319332
}
320333
}
321334

0 commit comments

Comments
 (0)