Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.
TypeScript ai-agentsbill-of-materialserpinventory-managementleanmanufacturingmanufacturing-execution-systemmcp-servermesmrpopen-source-erppostgresqlproduction-planningqmsquality-management-systemreact-routerself-hostedsupabasetraceabilitytypescript
feat: check s3 for the file before downloading/uploading it
Brad Barbina854c9a90117
diff --git a/packages/database/supabase/functions/textract/index.ts b/packages/database/supabase/functions/textract/index.tsindex c7cff08b92..cdec154fdc 100644--- a/packages/database/supabase/functions/textract/index.ts+++ b/packages/database/supabase/functions/textract/index.ts@@ -1,5 +1,6 @@import { serve } from "https://deno.land/[email protected]/http/server.ts";import {+ HeadObjectCommand,PutObjectCommand,S3Client,} from "https://deno.land/x/[email protected]/client-s3/mod.ts";@@ -12,19 +13,33 @@ import z from "https://deno.land/x/[email protected]/index.ts";import { corsHeaders } from "../lib/headers.ts";import { getSupabaseServiceRole } from "../lib/supabase.ts";+const AWS_REGION = Deno.env.get("AWS_REGION");+const AWS_ACCESS_KEY_ID = Deno.env.get("AWS_ACCESS_KEY_ID");+const AWS_SECRET_ACCESS_KEY = Deno.env.get("AWS_SECRET_ACCESS_KEY");+const AWS_S3_BUCKET = Deno.env.get("AWS_S3_BUCKET");++if (+ !AWS_REGION ||+ !AWS_ACCESS_KEY_ID ||+ !AWS_SECRET_ACCESS_KEY ||+ !AWS_S3_BUCKET+) {+ throw new Error("Missing required AWS environment variables");+}+const s3Client = new S3Client({- region: Deno.env.get("AWS_REGION"),+ region: AWS_REGION,credentials: {- accessKeyId: Deno.env.get("AWS_ACCESS_KEY_ID")!,- secretAccessKey: Deno.env.get("AWS_SECRET_ACCESS_KEY")!,+ accessKeyId: AWS_ACCESS_KEY_ID,+ secretAccessKey: AWS_SECRET_ACCESS_KEY,},});const textractClient = new TextractClient({- region: Deno.env.get("AWS_REGION"),+ region: AWS_REGION,credentials: {- accessKeyId: Deno.env.get("AWS_ACCESS_KEY_ID")!,- secretAccessKey: Deno.env.get("AWS_SECRET_ACCESS_KEY")!,+ accessKeyId: AWS_ACCESS_KEY_ID,+ secretAccessKey: AWS_SECRET_ACCESS_KEY,},});@@ -48,30 +63,48 @@ serve(async (req: Request) => {});const supabase = getSupabaseServiceRole(req.headers.get("Authorization"));+ const s3Key = `textract/${Date.now()}-${path.split("/").pop()}`;- // Download file from Supabase Storage- const { data, error } = await supabase.storage- .from("documents")- .download(path);+ // Check if file already exists in S3+ try {+ await s3Client.send(+ new HeadObjectCommand({+ Bucket: AWS_S3_BUCKET,+ Key: s3Key,+ })+ );+ console.log("File already exists in S3, skipping upload");+ } catch (error) {+ if (error.name === "NotFound") {+ // File doesn't exist, proceed with download and upload+ // Download file from Supabase Storage+ const { data, error } = await supabase.storage+ .from("documents")+ .download(path);- if (error) throw error;+ if (error) throw error;- // Upload file to S3- const s3Key = `textract/${Date.now()}-${path.split("/").pop()}`;- await s3Client.send(- new PutObjectCommand({- Bucket: Deno.env.get("AWS_S3_BUCKET"),- Key: s3Key,- Body: data,- })- );+ // Upload file to S3+ await s3Client.send(+ new PutObjectCommand({+ Bucket: AWS_S3_BUCKET,+ Key: s3Key,+ Body: data,+ })+ );+ console.log("File uploaded to S3");+ } else {+ // Unexpected error+ throw error;+ }+ }// Analyze document with Textractconst textractResponse = await textractClient.send(new AnalyzeDocumentCommand({Document: {S3Object: {- Bucket: Deno.env.get("AWS_S3_BUCKET"),+ Bucket: AWS_S3_BUCKET,Name: s3Key,},},