This commit is contained in:
Michael Freno
2025-12-18 15:03:13 -05:00
parent 5aecf6e555
commit fec58c4c17
12 changed files with 790 additions and 249 deletions

View File

@@ -1,6 +1,11 @@
import { createTRPCRouter, publicProcedure } from "../utils";
import { z } from "zod";
import { S3Client, GetObjectCommand, PutObjectCommand, DeleteObjectCommand } from "@aws-sdk/client-s3";
import {
S3Client,
GetObjectCommand,
PutObjectCommand,
DeleteObjectCommand
} from "@aws-sdk/client-s3";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
import { env } from "~/env/server";
import { TRPCError } from "@trpc/server";
@@ -11,49 +16,51 @@ import { getCookie, setCookie } from "vinxi/http";
const assets: Record<string, string> = {
"shapes-with-abigail": "shapes-with-abigail.apk",
"magic-delve": "magic-delve.apk",
cork: "Cork.zip",
cork: "Cork.zip"
};
export const miscRouter = createTRPCRouter({
// ============================================================
// Downloads endpoint
// ============================================================
getDownloadUrl: publicProcedure
.input(z.object({ asset_name: z.string() }))
.query(async ({ input }) => {
const bucket = "frenomedownloads";
const params = {
Bucket: bucket,
Key: assets[input.asset_name],
Key: assets[input.asset_name]
};
if (!assets[input.asset_name]) {
throw new TRPCError({
code: "NOT_FOUND",
message: "Asset not found",
message: "Asset not found"
});
}
const credentials = {
accessKeyId: env._AWS_ACCESS_KEY,
secretAccessKey: env._AWS_SECRET_KEY,
secretAccessKey: env._AWS_SECRET_KEY
};
try {
const client = new S3Client({
region: env.AWS_REGION,
credentials: credentials,
credentials: credentials
});
const command = new GetObjectCommand(params);
const signedUrl = await getSignedUrl(client, command, { expiresIn: 120 });
const signedUrl = await getSignedUrl(client, command, {
expiresIn: 120
});
return { downloadURL: signedUrl };
} catch (error) {
console.error(error);
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: "Failed to generate download URL",
message: "Failed to generate download URL"
});
}
}),
@@ -63,21 +70,23 @@ export const miscRouter = createTRPCRouter({
// ============================================================
getPreSignedURL: publicProcedure
.input(z.object({
type: z.string(),
title: z.string(),
filename: z.string(),
}))
.input(
z.object({
type: z.string(),
title: z.string(),
filename: z.string()
})
)
.mutation(async ({ input }) => {
const credentials = {
accessKeyId: env._AWS_ACCESS_KEY,
secretAccessKey: env._AWS_SECRET_KEY,
secretAccessKey: env._AWS_SECRET_KEY
};
try {
const client = new S3Client({
region: env.AWS_REGION,
credentials: credentials,
credentials: credentials
});
const Key = `${input.type}/${input.title}/${input.filename}`;
@@ -86,38 +95,42 @@ export const miscRouter = createTRPCRouter({
const s3params = {
Bucket: env.AWS_S3_BUCKET_NAME,
Key,
ContentType: `image/${ext![1]}`,
ContentType: `image/${ext![1]}`
};
const command = new PutObjectCommand(s3params);
const signedUrl = await getSignedUrl(client, command, { expiresIn: 120 });
const signedUrl = await getSignedUrl(client, command, {
expiresIn: 120
});
return { uploadURL: signedUrl, key: Key };
} catch (error) {
console.error(error);
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: "Failed to generate pre-signed URL",
message: "Failed to generate pre-signed URL"
});
}
}),
deleteImage: publicProcedure
.input(z.object({
key: z.string(),
newAttachmentString: z.string(),
type: z.string(),
id: z.number(),
}))
.input(
z.object({
key: z.string(),
newAttachmentString: z.string(),
type: z.string(),
id: z.number()
})
)
.mutation(async ({ input }) => {
try {
const s3params = {
Bucket: env.AWS_S3_BUCKET_NAME,
Key: input.key,
Key: input.key
};
const client = new S3Client({
region: env.AWS_REGION,
region: env.AWS_REGION
});
const command = new DeleteObjectCommand(s3params);
@@ -127,7 +140,7 @@ export const miscRouter = createTRPCRouter({
const query = `UPDATE ${input.type} SET attachments = ? WHERE id = ?`;
await conn.execute({
sql: query,
args: [input.newAttachmentString, input.id],
args: [input.newAttachmentString, input.id]
});
return res;
@@ -135,7 +148,7 @@ export const miscRouter = createTRPCRouter({
console.error(error);
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: "Failed to delete image",
message: "Failed to delete image"
});
}
}),
@@ -146,22 +159,22 @@ export const miscRouter = createTRPCRouter({
try {
const s3params = {
Bucket: env.AWS_S3_BUCKET_NAME,
Key: input.key,
Key: input.key
};
const client = new S3Client({
region: env.AWS_REGION,
region: env.AWS_REGION
});
const command = new DeleteObjectCommand(s3params);
const res = await client.send(command);
return res;
} catch (error) {
console.error(error);
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: "Failed to delete image",
message: "Failed to delete image"
});
}
}),
@@ -181,16 +194,18 @@ export const miscRouter = createTRPCRouter({
} catch (error) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: "Failed to hash password",
message: "Failed to hash password"
});
}
}),
checkPassword: publicProcedure
.input(z.object({
password: z.string(),
hash: z.string(),
}))
.input(
z.object({
password: z.string(),
hash: z.string()
})
)
.mutation(async ({ input }) => {
try {
const match = await bcrypt.compare(input.password, input.hash);
@@ -198,7 +213,78 @@ export const miscRouter = createTRPCRouter({
} catch (error) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: "Failed to check password",
message: "Failed to check password"
});
}
}),
// ============================================================
// Contact Form
// ============================================================
sendContactRequest: publicProcedure
.input(
z.object({
name: z.string().min(1),
email: z.string().email(),
message: z.string().min(1).max(500)
})
)
.mutation(async ({ input }) => {
// Check if contact request was recently sent
const contactExp = getCookie("contactRequestSent");
let remaining = 0;
if (contactExp) {
const expires = new Date(contactExp);
remaining = expires.getTime() - Date.now();
}
if (remaining > 0) {
throw new TRPCError({
code: "TOO_MANY_REQUESTS",
message: "countdown not expired"
});
}
const apiKey = env.SENDINBLUE_KEY;
const apiUrl = "https://api.sendinblue.com/v3/smtp/email";
const sendinblueData = {
sender: {
name: "freno.me",
email: "michael@freno.me"
},
to: [{ email: "michael@freno.me" }],
htmlContent: `<html><head></head><body><div>Request Name: ${input.name}</div><div>Request Email: ${input.email}</div><div>Request Message: ${input.message}</div></body></html>`,
subject: "freno.me Contact Request"
};
try {
await fetch(apiUrl, {
method: "POST",
headers: {
accept: "application/json",
"api-key": apiKey,
"content-type": "application/json"
},
body: JSON.stringify(sendinblueData)
});
// Set cookie to prevent spam (60 second cooldown)
const exp = new Date(Date.now() + 1 * 60 * 1000);
setCookie("contactRequestSent", exp.toUTCString(), {
expires: exp,
path: "/"
});
return { message: "email sent" };
} catch (error) {
console.error(error);
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message:
"SMTP server error: Sorry! You can reach me at michael@freno.me"
});
}
}),
@@ -213,7 +299,7 @@ export const miscRouter = createTRPCRouter({
// Check if deletion request was recently sent
const deletionExp = getCookie("deletionRequestSent");
let remaining = 0;
if (deletionExp) {
const expires = new Date(deletionExp);
remaining = expires.getTime() - Date.now();
@@ -222,7 +308,7 @@ export const miscRouter = createTRPCRouter({
if (remaining > 0) {
throw new TRPCError({
code: "TOO_MANY_REQUESTS",
message: "countdown not expired",
message: "countdown not expired"
});
}
@@ -233,22 +319,22 @@ export const miscRouter = createTRPCRouter({
const sendinblueMyData = {
sender: {
name: "freno.me",
email: "michael@freno.me",
email: "michael@freno.me"
},
to: [{ email: "michael@freno.me" }],
htmlContent: `<html><head></head><body><div>Request Name: Life and Lineage Account Deletion</div><div>Request Email: ${input.email}</div></body></html>`,
subject: "Life and Lineage Acct Deletion",
subject: "Life and Lineage Acct Deletion"
};
// Email to user
const sendinblueUserData = {
sender: {
name: "freno.me",
email: "michael@freno.me",
email: "michael@freno.me"
},
to: [{ email: input.email }],
htmlContent: `<html><head></head><body><div>Request Name: Life and Lineage Account Deletion</div><div>Account to delete: ${input.email}</div><div>You can email michael@freno.me in the next 24hrs to cancel the deletion, email with subject line "Account Deletion Cancellation"</div></body></html>`,
subject: "Life and Lineage Acct Deletion",
subject: "Life and Lineage Acct Deletion"
};
try {
@@ -258,9 +344,9 @@ export const miscRouter = createTRPCRouter({
headers: {
accept: "application/json",
"api-key": apiKey,
"content-type": "application/json",
"content-type": "application/json"
},
body: JSON.stringify(sendinblueMyData),
body: JSON.stringify(sendinblueMyData)
});
await fetch(apiUrl, {
@@ -268,16 +354,16 @@ export const miscRouter = createTRPCRouter({
headers: {
accept: "application/json",
"api-key": apiKey,
"content-type": "application/json",
"content-type": "application/json"
},
body: JSON.stringify(sendinblueUserData),
body: JSON.stringify(sendinblueUserData)
});
// Set cookie to prevent spam (60 second cooldown)
const exp = new Date(Date.now() + 1 * 60 * 1000);
setCookie("deletionRequestSent", exp.toUTCString(), {
expires: exp,
path: "/",
path: "/"
});
return { message: "request sent" };
@@ -285,8 +371,9 @@ export const miscRouter = createTRPCRouter({
console.error(error);
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: "SMTP server error: Sorry! You can reach me at michael@freno.me",
message:
"SMTP server error: Sorry! You can reach me at michael@freno.me"
});
}
}),
})
});