Skip to content

Commit

Permalink
Merge pull request #11 from utopia-php/improve-firebase-stability
Browse files Browse the repository at this point in the history
Add FB BucketId Sanitise and improve getStatusCounters
  • Loading branch information
eldadfux authored Aug 16, 2023
2 parents 16ad161 + d93324d commit 8a1d4de
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
37 changes: 34 additions & 3 deletions src/Migration/Sources/Firebase.php
Original file line number Diff line number Diff line change
Expand Up @@ -535,10 +535,16 @@ private function exportBuckets(int $batchsize)
break;
}

$buckets = [];
foreach ($result['items'] as $bucket) {
$this->callback([new Bucket($bucket['id'], $bucket['name'], [], false)]);
$curBucket = new Bucket($this->sanitizeBucketId($bucket['id']), $bucket['name'], [], false);
$curBucket->setOriginalId($bucket['id']);

$buckets[] = $curBucket;
}

$this->callback($buckets);

if (! isset($result['nextPageToken'])) {
break;
}
Expand All @@ -547,13 +553,38 @@ private function exportBuckets(int $batchsize)
}
}

private function sanitizeBucketId($id)
{
// Step 1: Check if the ID looks like a URL (contains ".")
if (strpos($id, '.') !== false) {
// If it looks like a URL, try to extract the subdomain
$parts = explode('.', $id);
if (count($parts) > 0) {
$id = $parts[0];
}
}

// Step 2: Ensure the ID contains at most 36 characters
$id = substr($id, 0, 36);

// Step 3: Remove invalid characters using a regular expression
$id = preg_replace('/[^a-zA-Z0-9\._-]/', '', $id);

// Step 4: Ensure the ID doesn't start with a special character
if (preg_match('/^[._-]/', $id)) {
$id = 'a'.substr($id, 1);
}

return $id;
}

private function exportFiles(int $batchsize)
{
$buckets = $this->cache->get(Bucket::getName());

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

$nextPageToken = null;

Expand Down Expand Up @@ -588,7 +619,7 @@ private function exportFiles(int $batchsize)

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

Expand Down
15 changes: 15 additions & 0 deletions src/Migration/Transfer.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,21 @@ public function getStatusCounters()
}
}

// Remove all empty resources
foreach ($status as $resource => $data) {
$allEmpty = true;

foreach ($data as $count) {
if ($count > 0) {
$allEmpty = false;
}
}

if ($allEmpty) {
unset($status[$resource]);
}
}

return $status;
}

Expand Down

0 comments on commit 8a1d4de

Please sign in to comment.