aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShav Kinderlehrer <[email protected]>2023-10-16 14:08:23 -0400
committerShav Kinderlehrer <[email protected]>2023-10-16 14:08:23 -0400
commitc5d3db7993b5ca39b48d055a5b42a57113757839 (patch)
treefa0a8fdff5dc2e96779623711c61c6d24ecc1d6d
parent612e67d3dca483fa03b5f0e9d5778a0a693a7474 (diff)
downloadurl-shortener-c5d3db7993b5ca39b48d055a5b42a57113757839.tar.gz
url-shortener-c5d3db7993b5ca39b48d055a5b42a57113757839.zip
Shorten hash + add '+' operatorHEADmain
-rw-r--r--src/get.ts11
-rw-r--r--src/post.ts2
2 files changed, 11 insertions, 2 deletions
diff --git a/src/get.ts b/src/get.ts
index fb853a3..adba0fc 100644
--- a/src/get.ts
+++ b/src/get.ts
@@ -25,7 +25,14 @@ export async function get_id(
context: { params: Record<"id", string>, headers: Record<string, string | null> },
sql: Sql) {
- const id = context.params.id;
+ let id = context.params.id;
+
+ let get_query = false;
+ if (id.endsWith("+")) {
+ id = id.slice(0, id.length - 1);
+ get_query = true;
+ }
+
let db_res = await sql`
SELECT * FROM urls
WHERE id=${id}
@@ -33,6 +40,8 @@ export async function get_id(
if (!db_res.length) {
return new Response(`url for id '${id}' not found.`, { status: 404 });
+ } else if (get_query) {
+ return new Response(`trkt.in/${id} -> ${db_res[0]['url']}`);
} else {
await sql`
INSERT INTO tracking (id,clicks)
diff --git a/src/post.ts b/src/post.ts
index 538ff00..94643d4 100644
--- a/src/post.ts
+++ b/src/post.ts
@@ -20,7 +20,7 @@ export async function post_c(body: any, sql: Sql) {
custom_id = false;
const hasher = new Bun.CryptoHasher("md5");
hasher.update(form.url.href);
- form.id = hasher.digest("hex").slice(0, 11);
+ form.id = hasher.digest("hex").slice(0, 4);
}
const valid_re = /^[A-z0-9_-]+$/;