Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FB BucketId Sanitise and improve getStatusCounters #11

Merged
merged 3 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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