quick fix
This commit is contained in:
@@ -27,8 +27,8 @@ vi.mock("@aws-sdk/s3-request-presigner", () => ({
|
||||
|
||||
// Mock environment variables
|
||||
process.env.AWS_REGION = "us-east-1";
|
||||
process.env._AWS_ACCESS_KEY = "test-access-key";
|
||||
process.env._AWS_SECRET_KEY = "test-secret-key";
|
||||
process.env.MY_AWS_ACCESS_KEY = "test-access-key";
|
||||
process.env.MY_AWS_SECRET_KEY = "test-secret-key";
|
||||
process.env.VITE_DOWNLOAD_BUCKET_STRING = "test-bucket";
|
||||
|
||||
describe("downloads router", () => {
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
import { createTRPCRouter, publicProcedure } from "../utils";
|
||||
import { z } from "zod";
|
||||
import { S3Client, GetObjectCommand, ListObjectsV2Command } from "@aws-sdk/client-s3";
|
||||
import {
|
||||
S3Client,
|
||||
GetObjectCommand,
|
||||
ListObjectsV2Command
|
||||
} from "@aws-sdk/client-s3";
|
||||
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
|
||||
import { env } from "~/env/server";
|
||||
import { TRPCError } from "@trpc/server";
|
||||
@@ -14,7 +18,10 @@ const assets: Record<string, string> = {
|
||||
/**
|
||||
* Get the latest Gaze DMG from S3 by finding the most recent file in downloads/ folder
|
||||
*/
|
||||
async function getLatestGazeDMG(client: S3Client, bucket: string): Promise<string> {
|
||||
async function getLatestGazeDMG(
|
||||
client: S3Client,
|
||||
bucket: string
|
||||
): Promise<string> {
|
||||
try {
|
||||
const listCommand = new ListObjectsV2Command({
|
||||
Bucket: bucket,
|
||||
@@ -23,19 +30,19 @@ async function getLatestGazeDMG(client: S3Client, bucket: string): Promise<strin
|
||||
});
|
||||
|
||||
const response = await client.send(listCommand);
|
||||
|
||||
|
||||
if (!response.Contents || response.Contents.length === 0) {
|
||||
throw new Error("No Gaze DMG files found in S3");
|
||||
}
|
||||
|
||||
// Filter for .dmg files only and sort by LastModified (newest first)
|
||||
const dmgFiles = response.Contents
|
||||
.filter((obj) => obj.Key?.endsWith(".dmg"))
|
||||
.sort((a, b) => {
|
||||
const dateA = a.LastModified?.getTime() || 0;
|
||||
const dateB = b.LastModified?.getTime() || 0;
|
||||
return dateB - dateA; // Descending order (newest first)
|
||||
});
|
||||
const dmgFiles = response.Contents.filter((obj) =>
|
||||
obj.Key?.endsWith(".dmg")
|
||||
).sort((a, b) => {
|
||||
const dateA = a.LastModified?.getTime() || 0;
|
||||
const dateB = b.LastModified?.getTime() || 0;
|
||||
return dateB - dateA; // Descending order (newest first)
|
||||
});
|
||||
|
||||
if (dmgFiles.length === 0) {
|
||||
throw new Error("No .dmg files found in downloads/Gaze-* prefix");
|
||||
@@ -55,10 +62,10 @@ export const downloadsRouter = createTRPCRouter({
|
||||
.input(z.object({ asset_name: z.string() }))
|
||||
.query(async ({ input }) => {
|
||||
const bucket = env.VITE_DOWNLOAD_BUCKET_STRING;
|
||||
|
||||
|
||||
const credentials = {
|
||||
accessKeyId: env._AWS_ACCESS_KEY,
|
||||
secretAccessKey: env._AWS_SECRET_KEY
|
||||
accessKeyId: env.MY_AWS_ACCESS_KEY,
|
||||
secretAccessKey: env.MY_AWS_SECRET_KEY
|
||||
};
|
||||
|
||||
const client = new S3Client({
|
||||
@@ -75,7 +82,7 @@ export const downloadsRouter = createTRPCRouter({
|
||||
} else {
|
||||
// Use static mapping for other assets
|
||||
fileKey = assets[input.asset_name];
|
||||
|
||||
|
||||
if (!fileKey) {
|
||||
throw new TRPCError({
|
||||
code: "NOT_FOUND",
|
||||
@@ -98,7 +105,10 @@ export const downloadsRouter = createTRPCRouter({
|
||||
console.error(error);
|
||||
throw new TRPCError({
|
||||
code: "INTERNAL_SERVER_ERROR",
|
||||
message: error instanceof Error ? error.message : "Failed to generate download URL"
|
||||
message:
|
||||
error instanceof Error
|
||||
? error.message
|
||||
: "Failed to generate download URL"
|
||||
});
|
||||
}
|
||||
})
|
||||
|
||||
@@ -46,8 +46,8 @@ export const miscRouter = createTRPCRouter({
|
||||
}
|
||||
|
||||
const credentials = {
|
||||
accessKeyId: env._AWS_ACCESS_KEY,
|
||||
secretAccessKey: env._AWS_SECRET_KEY
|
||||
accessKeyId: env.MY_AWS_ACCESS_KEY,
|
||||
secretAccessKey: env.MY_AWS_SECRET_KEY
|
||||
};
|
||||
|
||||
try {
|
||||
@@ -80,8 +80,8 @@ export const miscRouter = createTRPCRouter({
|
||||
)
|
||||
.mutation(async ({ input }) => {
|
||||
const credentials = {
|
||||
accessKeyId: env._AWS_ACCESS_KEY,
|
||||
secretAccessKey: env._AWS_SECRET_KEY
|
||||
accessKeyId: env.MY_AWS_ACCESS_KEY,
|
||||
secretAccessKey: env.MY_AWS_SECRET_KEY
|
||||
};
|
||||
|
||||
try {
|
||||
@@ -135,8 +135,8 @@ export const miscRouter = createTRPCRouter({
|
||||
.query(async ({ input }) => {
|
||||
try {
|
||||
const credentials = {
|
||||
accessKeyId: env._AWS_ACCESS_KEY,
|
||||
secretAccessKey: env._AWS_SECRET_KEY
|
||||
accessKeyId: env.MY_AWS_ACCESS_KEY,
|
||||
secretAccessKey: env.MY_AWS_SECRET_KEY
|
||||
};
|
||||
|
||||
const client = new S3Client({
|
||||
@@ -195,8 +195,8 @@ export const miscRouter = createTRPCRouter({
|
||||
.mutation(async ({ input }) => {
|
||||
try {
|
||||
const credentials = {
|
||||
accessKeyId: env._AWS_ACCESS_KEY,
|
||||
secretAccessKey: env._AWS_SECRET_KEY
|
||||
accessKeyId: env.MY_AWS_ACCESS_KEY,
|
||||
secretAccessKey: env.MY_AWS_SECRET_KEY
|
||||
};
|
||||
|
||||
const s3params = {
|
||||
@@ -234,8 +234,8 @@ export const miscRouter = createTRPCRouter({
|
||||
.mutation(async ({ input }) => {
|
||||
try {
|
||||
const credentials = {
|
||||
accessKeyId: env._AWS_ACCESS_KEY,
|
||||
secretAccessKey: env._AWS_SECRET_KEY
|
||||
accessKeyId: env.MY_AWS_ACCESS_KEY,
|
||||
secretAccessKey: env.MY_AWS_SECRET_KEY
|
||||
};
|
||||
|
||||
const s3params = {
|
||||
|
||||
Reference in New Issue
Block a user