Skip to content

Commit

Permalink
feat: CloudFront invalidation for reupload. + misc changes
Browse files Browse the repository at this point in the history
  • Loading branch information
richiemcilroy committed Nov 27, 2024
1 parent c6065d2 commit 638ddae
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
23 changes: 22 additions & 1 deletion apps/desktop/src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2005,7 +2005,28 @@ pub async fn run() {
.expect("error while running tauri application")
.run(|handle, event| match event {
#[cfg(target_os = "macos")]
tauri::RunEvent::Reopen { .. } => open_main_window(handle.clone()),
tauri::RunEvent::Reopen { .. } => {
// Check if any editor window is open
let has_editor = handle
.webview_windows()
.iter()
.any(|(label, _)| label.starts_with("editor-"));

if has_editor {
// Find and focus the editor window
if let Some(editor_window) = handle
.webview_windows()
.iter()
.find(|(label, _)| label.starts_with("editor-"))
.map(|(_, window)| window.clone())
{
editor_window.set_focus().ok();
}
} else {
// No editor window open, show main window
open_main_window(handle.clone());
}
}
_ => {}
});
}
Expand Down
38 changes: 36 additions & 2 deletions apps/web/app/api/upload/signed/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import {
createPresignedPost,
type PresignedPost,
} from "@aws-sdk/s3-presigned-post";
import { CloudFrontClient, CreateInvalidationCommand } from "@aws-sdk/client-cloudfront";
import { db } from "@cap/database";
import { getCurrentUser } from "@cap/database/auth/session";
import { s3Buckets } from "@cap/database/schema";
import { eq } from "drizzle-orm";
import { cookies } from "next/headers";
import type { NextRequest } from "next/server";
import { decrypt } from "@cap/database/crypto";

export async function POST(request: NextRequest) {
try {
Expand Down Expand Up @@ -66,6 +66,41 @@ export async function POST(request: NextRequest) {
secretAccessKey: bucket.secretAccessKey,
} : null;

if (!bucket || !s3Config || bucket.bucketName !== process.env.CAP_S3_BUCKET) {
const distributionId = process.env.CAP_CLOUDFRONT_DISTRIBUTION_ID;
if (distributionId) {
console.log("Creating CloudFront invalidation for", fileKey);

const cloudfront = new CloudFrontClient({
region: process.env.NEXT_PUBLIC_CAP_AWS_REGION || "us-east-1",
credentials: {
accessKeyId: process.env.CAP_AWS_ACCESS_KEY || "",
secretAccessKey: process.env.CAP_AWS_SECRET_KEY || "",
}
});

const pathToInvalidate = "/" + fileKey;

try {
const invalidation = await cloudfront.send(
new CreateInvalidationCommand({
DistributionId: distributionId,
InvalidationBatch: {
CallerReference: `${Date.now()}`,
Paths: {
Quantity: 1,
Items: [pathToInvalidate],
},
},
})
);
console.log("CloudFront invalidation created:", invalidation);
} catch (error) {
console.error("Failed to create CloudFront invalidation:", error);
}
}
}

console.log("Creating S3 client with config:", {
hasEndpoint: !!s3Config?.endpoint,
hasRegion: !!s3Config?.region,
Expand Down Expand Up @@ -98,7 +133,6 @@ export async function POST(request: NextRequest) {
};

const bucketName = await getS3Bucket(bucket);
console.log("Using bucket:", bucketName);

const presignedPostData: PresignedPost = await createPresignedPost(
s3Client,
Expand Down
2 changes: 1 addition & 1 deletion crates/flags/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ pub const FLAGS: Flags = Flags {
split: false, // cfg!(debug_assertions),
pause_resume: false, // cfg!(debug_assertions),
zoom: false, // cfg!(debug_assertions),
custom_s3: false,
custom_s3: true,
};

1 comment on commit 638ddae

@vercel
Copy link

@vercel vercel bot commented on 638ddae Nov 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.