mirror of
https://github.com/appwrite/appwrite
synced 2026-05-22 16:38:32 +00:00
Use branch domain in preview urls in builds worker
This commit is contained in:
parent
9cf15da482
commit
f3204d2520
1 changed files with 21 additions and 7 deletions
|
|
@ -1554,16 +1554,30 @@ class Builds extends Action
|
|||
default => throw new \Exception('Invalid resource type')
|
||||
};
|
||||
|
||||
$rule = Authorization::skip(fn () => $dbForPlatform->findOne('rules', [
|
||||
Query::equal("projectInternalId", [$project->getSequence()]),
|
||||
Query::equal("type", ["deployment"]),
|
||||
Query::equal("deploymentInternalId", [$deployment->getSequence()]),
|
||||
]));
|
||||
|
||||
$protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS') == 'disabled' ? 'http' : 'https';
|
||||
$previewUrl = match($resource->getCollection()) {
|
||||
'functions' => '',
|
||||
'sites' => !empty($rule) ? ("{$protocol}://" . $rule->getAttribute('domain', '')) : '',
|
||||
'sites' => (function () use ($deployment, $project, $dbForPlatform, $protocol, $resource) {
|
||||
$providerBranch = $deployment->getAttribute('providerBranch', '');
|
||||
if (!empty($providerBranch)) {
|
||||
$sitesDomain = System::getEnv('_APP_DOMAIN_SITES', '');
|
||||
$branchPrefix = substr($providerBranch, 0, 16);
|
||||
if (strlen($providerBranch) > 16) {
|
||||
$remainingChars = substr($providerBranch, 16);
|
||||
$branchPrefix .= '-' . substr(hash('sha256', $remainingChars), 0, 7);
|
||||
}
|
||||
$resourceProjectHash = substr(hash('sha256', $resource->getId() . $project->getId()), 0, 7);
|
||||
$domain = "branch-{$branchPrefix}-{$resourceProjectHash}.{$sitesDomain}";
|
||||
$ruleId = md5($domain);
|
||||
|
||||
$branchRule = Authorization::skip(fn () => $dbForPlatform->getDocument('rules', $ruleId));
|
||||
|
||||
if (!empty($branchRule) && !$branchRule->isEmpty()) {
|
||||
return "{$protocol}://" . $branchRule->getAttribute('domain', '');
|
||||
}
|
||||
}
|
||||
return '';
|
||||
})(),
|
||||
default => throw new \Exception('Invalid resource type')
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue