File size: 813 Bytes
7f6dd09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import fs from "fs";
import path from "path";
import config from "../config.js";
import supabase from "../lib/supabase.js";

function saveFileToDisk(jobId, fileBuffer, filename) {
  const folderPath = path.join(config.sharedTmpPath, jobId);
  fs.mkdirSync(folderPath, { recursive: true });
  const filePath = path.join(folderPath, filename);
  fs.writeFileSync(filePath, fileBuffer);
  return filePath;
}

async function uploadToSupabase(jobId, filePath, fileName) {
  const fileBuffer = fs.readFileSync(filePath);
  const storagePath = `${jobId}/${fileName}`;

  const { error } = await supabase.storage
    .from("intelli-credit-uploads")
    .upload(storagePath, fileBuffer, { upsert: true });

  if (error) throw new Error(error.message);
  return storagePath;
}

export { saveFileToDisk, uploadToSupabase };