Handle URLs with weird characters

This commit is contained in:
Regalijan 2024-11-02 00:46:02 -04:00
parent e55259357f
commit 60eb7bca64
Signed by: regalijan
GPG Key ID: 5D4196DA269EF520
2 changed files with 15 additions and 6 deletions

View File

@ -54,9 +54,12 @@ export default function () {
const toast = useToast(); const toast = useToast();
async function deleteLink(path: string) { async function deleteLink(path: string) {
const deleteResp = await fetch(`/api/short-links/${path}`, { const deleteResp = await fetch(
`/api/short-links/${encodeURIComponent(path)}`,
{
method: "DELETE", method: "DELETE",
}); },
);
if (!deleteResp.ok) { if (!deleteResp.ok) {
let error = "Unknown error"; let error = "Unknown error";
@ -103,7 +106,9 @@ export default function () {
<Td <Td
onClick={() => { onClick={() => {
navigator.clipboard.writeText( navigator.clipboard.writeText(
encodeURIComponent(
`https://carcrushe.rs/${entry.path}`, `https://carcrushe.rs/${entry.path}`,
),
); );
alert("Link copied"); alert("Link copied");
}} }}

View File

@ -1,7 +1,7 @@
import { jsonError } from "../../common.js"; import { jsonError } from "../../common.js";
export async function onRequestDelete(context: RequestContext) { export async function onRequestDelete(context: RequestContext) {
const path = context.params.id; const path = decodeURIComponent(context.params.id as string);
if (typeof path !== "string") return jsonError("Invalid path", 400); if (typeof path !== "string") return jsonError("Invalid path", 400);
@ -42,7 +42,11 @@ export async function onRequestPatch(context: RequestContext) {
await context.env.D1.prepare( await context.env.D1.prepare(
"UPDATE short_links SET path = ? WHERE path = ? AND user = ?;", "UPDATE short_links SET path = ? WHERE path = ? AND user = ?;",
) )
.bind(path, context.params.id, context.data.current_user.id) .bind(
path,
decodeURIComponent(context.params.id as string),
context.data.current_user.id,
)
.run(); .run();
return new Response(null, { return new Response(null, {