mirror of
https://github.com/sigmasternchen/drnk.me
synced 2025-03-15 09:48:54 +00:00
feat: Add return existing slug if available
This commit is contained in:
parent
96ca76cd93
commit
f9a1419608
2 changed files with 33 additions and 9 deletions
|
@ -86,6 +86,10 @@ function generateSlug(URLs $repository) {
|
||||||
return $candidates[0];
|
return $candidates[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function generateAccessKey(string $slug, string $url) {
|
||||||
|
return sha1($url . "-" . $url . "-" . microtime() . "-" . rand());
|
||||||
|
}
|
||||||
|
|
||||||
return function (array &$context) {
|
return function (array &$context) {
|
||||||
$url = $_POST["url"] ?? "";
|
$url = $_POST["url"] ?? "";
|
||||||
$url = validateInput($url);
|
$url = validateInput($url);
|
||||||
|
@ -94,19 +98,25 @@ return function (array &$context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
$repository = $context[REPOSITORIES]->urls;
|
$repository = $context[REPOSITORIES]->urls;
|
||||||
$slug = generateSlug($repository);
|
|
||||||
|
|
||||||
$accessKey = sha1($url . "-" . $url . "-" . microtime() . "-" . rand());
|
$result = $repository->getByUrl($url);
|
||||||
|
if ($result) {
|
||||||
|
// don't leak existing access key
|
||||||
|
$result->accessKey = "";
|
||||||
|
} else {
|
||||||
|
$slug = generateSlug($repository);
|
||||||
|
$accessKey = generateAccessKey($slug, $url);
|
||||||
|
|
||||||
$result = $context[REPOSITORIES]->urls->add(new URL(
|
$result = $repository->add(new URL(
|
||||||
$slug,
|
$slug,
|
||||||
$url,
|
$url,
|
||||||
$accessKey
|
$accessKey
|
||||||
));
|
));
|
||||||
|
}
|
||||||
|
|
||||||
$data = [
|
$data = [
|
||||||
"url" => "https://drnk.me/$slug",
|
"url" => "https://drnk.me/" . $result->slug,
|
||||||
"accessKey" => $accessKey,
|
"accessKey" => $result->accessKey,
|
||||||
];
|
];
|
||||||
require(ROOT . "/templates/pages/components/creation-successful.php");
|
require(ROOT . "/templates/pages/components/creation-successful.php");
|
||||||
};
|
};
|
||||||
|
|
|
@ -71,4 +71,18 @@ class URLs {
|
||||||
return $this->entityFromRow($statement->fetch());
|
return $this->entityFromRow($statement->fetch());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getByUrl(string $url) {
|
||||||
|
$statement = $this->connection->prepare(<<<EOF
|
||||||
|
SELECT * FROM `$this->table`
|
||||||
|
WHERE `url` = ?
|
||||||
|
EOF);
|
||||||
|
$statement->execute([$url]);
|
||||||
|
|
||||||
|
if ($statement->rowCount() == 0) {
|
||||||
|
return null;
|
||||||
|
} else {
|
||||||
|
return $this->entityFromRow($statement->fetch());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue