44
55use Utopia \Database \Database as UtopiaDatabase ;
66use Utopia \Migration \Exception ;
7- use Utopia \Migration \Resource ;
7+ use Utopia \Migration \Resource as UtopiaResource ;
88use Utopia \Migration \Resources \Database \Attribute ;
99use Utopia \Migration \Resources \Database \Collection ;
1010use Utopia \Migration \Resources \Database \Database ;
1515use Utopia \Migration \Sources \Appwrite \Reader \Database as DatabaseReader ;
1616use Utopia \Migration \Transfer ;
1717use Utopia \Storage \Device ;
18+ use Utopia \Storage \Storage ;
1819
1920class 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,42 @@ 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+ * @throws \Exception
309+ */
310+ private function withCsvStream (callable $ callback ): void
305311 {
306- if (! $ this ->device ->exists ($ this ->filePath )) {
312+ if (!$ this ->device ->exists ($ this ->filePath )) {
307313 return ;
308314 }
309315
310- $ stream = fopen ($ this ->filePath , 'r ' );
311- if (! $ stream ) {
316+ if ($ this ->device ->getType () !== Storage::DEVICE_LOCAL ) {
317+ try {
318+ $ success = $ this ->device ->transfer (
319+ $ this ->filePath ,
320+ $ this ->filePath ,
321+ new Device \Local ('/ ' ),
322+ );
323+ } catch (\Exception $ e ) {
324+ $ success = false ;
325+ }
326+
327+ if (!$ success ) {
328+ throw new \Exception ('Failed to transfer CSV file from device to local storage. ' , previous: $ e ?? null );
329+ }
330+ }
331+
332+ $ stream = \fopen ($ this ->filePath , 'r ' );
333+ if (!$ stream ) {
312334 return ;
313335 }
314336
315337 try {
316- $ fn ($ stream );
338+ $ callback ($ stream );
317339 } finally {
318- fclose ($ stream );
340+ \ fclose ($ stream );
319341 }
320342 }
321343
0 commit comments