All files / react-app/src/components/Inputs upload.utilities.ts

100% Statements 17/17
80% Branches 4/5
100% Functions 3/3
100% Lines 17/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43    1x 1x     1x     1x 1x           1x           2x 2x   1x 1x 1x   1x 1x       1x   1x   1x 1x      
import { API } from "aws-amplify";
 
export const getPresignedUrl = async (fileName: string): Promise<string> => {
  const response = await API.post("os", "/getUploadUrl", {
    body: { fileName },
  });
  return response.url;
};
 
export const uploadToS3 = async (file: File, url: string): Promise<void> => {
  await fetch(url, {
    body: file,
    method: "PUT",
  });
};
 
export const extractBucketAndKeyFromUrl = (
  url: string,
): {
  bucket: string | null;
  key: string | null;
} => {
  try {
    const parsedUrl = new URL(url);
 
    const hostnameParts = parsedUrl.hostname.split(".");
    let bucket: string | null = null;
    let key: string | null = null;
 
    Eif (hostnameParts.length > 3 && hostnameParts[1] === "s3" && hostnameParts[2] === "us-east-1") {
      bucket = hostnameParts[0]; // The bucket name is the first part of the hostname
    }
 
    // Extract key from the pathname
    key = parsedUrl.pathname.slice(1); // Remove the leading slash
 
    return { bucket, key };
  } catch (error) {
    console.error("Invalid URL format:", error);
    return { bucket: null, key: null };
  }
};