Skip to content

Commit 8a1d4de

Browse files
authored
Merge pull request #11 from utopia-php/improve-firebase-stability
Add FB BucketId Sanitise and improve getStatusCounters
2 parents 16ad161 + d93324d commit 8a1d4de

2 files changed

Lines changed: 49 additions & 3 deletions

File tree

src/Migration/Sources/Firebase.php

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -535,10 +535,16 @@ private function exportBuckets(int $batchsize)
535535
break;
536536
}
537537

538+
$buckets = [];
538539
foreach ($result['items'] as $bucket) {
539-
$this->callback([new Bucket($bucket['id'], $bucket['name'], [], false)]);
540+
$curBucket = new Bucket($this->sanitizeBucketId($bucket['id']), $bucket['name'], [], false);
541+
$curBucket->setOriginalId($bucket['id']);
542+
543+
$buckets[] = $curBucket;
540544
}
541545

546+
$this->callback($buckets);
547+
542548
if (! isset($result['nextPageToken'])) {
543549
break;
544550
}
@@ -547,13 +553,38 @@ private function exportBuckets(int $batchsize)
547553
}
548554
}
549555

556+
private function sanitizeBucketId($id)
557+
{
558+
// Step 1: Check if the ID looks like a URL (contains ".")
559+
if (strpos($id, '.') !== false) {
560+
// If it looks like a URL, try to extract the subdomain
561+
$parts = explode('.', $id);
562+
if (count($parts) > 0) {
563+
$id = $parts[0];
564+
}
565+
}
566+
567+
// Step 2: Ensure the ID contains at most 36 characters
568+
$id = substr($id, 0, 36);
569+
570+
// Step 3: Remove invalid characters using a regular expression
571+
$id = preg_replace('/[^a-zA-Z0-9\._-]/', '', $id);
572+
573+
// Step 4: Ensure the ID doesn't start with a special character
574+
if (preg_match('/^[._-]/', $id)) {
575+
$id = 'a'.substr($id, 1);
576+
}
577+
578+
return $id;
579+
}
580+
550581
private function exportFiles(int $batchsize)
551582
{
552583
$buckets = $this->cache->get(Bucket::getName());
553584

554585
foreach ($buckets as $bucket) {
555586
/** @var Bucket $bucket */
556-
$endpoint = 'https://storage.googleapis.com/storage/v1/b/'.$bucket->getId().'/o';
587+
$endpoint = 'https://storage.googleapis.com/storage/v1/b/'.$bucket->getOriginalId().'/o';
557588

558589
$nextPageToken = null;
559590

@@ -588,7 +619,7 @@ private function exportFiles(int $batchsize)
588619

589620
private function exportFile(File $file)
590621
{
591-
$endpoint = 'https://storage.googleapis.com/storage/v1/b/'.$file->getBucket()->getId().'/o/'.$file->getId().'?alt=media';
622+
$endpoint = 'https://storage.googleapis.com/storage/v1/b/'.$file->getBucket()->getOriginalId().'/o/'.$file->getId().'?alt=media';
592623
$start = 0;
593624
$end = Transfer::STORAGE_MAX_CHUNK_SIZE - 1;
594625

src/Migration/Transfer.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,21 @@ public function getStatusCounters()
103103
}
104104
}
105105

106+
// Remove all empty resources
107+
foreach ($status as $resource => $data) {
108+
$allEmpty = true;
109+
110+
foreach ($data as $count) {
111+
if ($count > 0) {
112+
$allEmpty = false;
113+
}
114+
}
115+
116+
if ($allEmpty) {
117+
unset($status[$resource]);
118+
}
119+
}
120+
106121
return $status;
107122
}
108123

0 commit comments

Comments
 (0)