increase screenshot ttl to outdated resumes

This commit is contained in:
Amruth Pillai
2026-01-25 00:27:58 +01:00
parent 92f6a6a16b
commit b5f4e9af46
4 changed files with 21 additions and 11 deletions
+1 -1
View File
@@ -8,7 +8,7 @@ import { create } from "zustand/react";
import { orpc, type RouterOutput } from "@/integrations/orpc/client";
import type { ResumeData } from "@/schema/resume/data";
type Resume = Omit<RouterOutput["resume"]["getById"], "hasPassword">;
type Resume = Pick<RouterOutput["resume"]["getByIdForPrinter"], "id" | "name" | "slug" | "tags" | "data" | "isLocked">;
type ResumeStoreState = {
resume: Resume;
+4 -4
View File
@@ -15,8 +15,8 @@ export const printerRouter = {
.input(z.object({ id: z.string() }))
.output(z.object({ url: z.string() }))
.handler(async ({ input, context }) => {
const resume = await resumeService.getByIdForPrinter({ id: input.id });
const url = await printerService.printResumeAsPDF(resume);
const { id, data, userId } = await resumeService.getByIdForPrinter({ id: input.id });
const url = await printerService.printResumeAsPDF({ id, data, userId });
if (!context.user) {
await resumeService.statistics.increment({ id: input.id, downloads: true });
@@ -36,8 +36,8 @@ export const printerRouter = {
.input(z.object({ id: z.string() }))
.output(z.object({ url: z.string() }))
.handler(async ({ input }) => {
const resume = await resumeService.getByIdForPrinter({ id: input.id });
const url = await printerService.getResumeScreenshot(resume);
const { id, data, userId, updatedAt } = await resumeService.getByIdForPrinter({ id: input.id });
const url = await printerService.getResumeScreenshot({ id, data, userId, updatedAt });
return { url };
}),
+14 -4
View File
@@ -64,9 +64,9 @@ export const printerService = {
* 7. Upload to storage and return the URL
*/
printResumeAsPDF: async (
input: Pick<InferSelectModel<typeof schema.resume>, "userId" | "id" | "data">,
input: Pick<InferSelectModel<typeof schema.resume>, "id" | "data" | "userId">,
): Promise<string> => {
const { id, userId, data } = input;
const { id, data, userId } = input;
// Step 1: Delete any existing PDF for this resume to ensure fresh generation
const storageService = getStorageService();
@@ -190,15 +190,16 @@ export const printerService = {
},
getResumeScreenshot: async (
input: Pick<InferSelectModel<typeof schema.resume>, "userId" | "id" | "data">,
input: Pick<InferSelectModel<typeof schema.resume>, "userId" | "id" | "data" | "updatedAt">,
): Promise<string> => {
const { id, userId, data } = input;
const { id, userId, data, updatedAt } = input;
const storageService = getStorageService();
const screenshotPrefix = `uploads/${userId}/screenshots/${id}`;
const existingScreenshots = await storageService.list(screenshotPrefix);
const now = Date.now();
const resumeUpdatedAt = updatedAt.getTime();
if (existingScreenshots.length > 0) {
const sortedFiles = existingScreenshots
@@ -214,8 +215,17 @@ export const printerService = {
const latest = sortedFiles[0];
const age = now - latest.timestamp;
// Return existing screenshot if it's still fresh (within TTL)
if (age < SCREENSHOT_TTL) return new URL(latest.path, env.APP_URL).toString();
// Screenshot is stale (past TTL), but only regenerate if the resume
// was updated after the screenshot was taken. If the resume hasn't
// changed, keep using the existing screenshot to avoid unnecessary work.
if (resumeUpdatedAt <= latest.timestamp) {
return new URL(latest.path, env.APP_URL).toString();
}
// Resume was updated after the screenshot - delete old ones and regenerate
await Promise.all(sortedFiles.map((file) => storageService.delete(file.path)));
}
}
+2 -2
View File
@@ -135,13 +135,13 @@ export const resumeService = {
const [resume] = await db
.select({
id: schema.resume.id,
userId: schema.resume.userId,
name: schema.resume.name,
slug: schema.resume.slug,
tags: schema.resume.tags,
data: schema.resume.data,
isPublic: schema.resume.isPublic,
userId: schema.resume.userId,
isLocked: schema.resume.isLocked,
updatedAt: schema.resume.updatedAt,
})
.from(schema.resume)
.where(eq(schema.resume.id, input.id));