Skip to content

Commit

Permalink
Try more stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
jamdelion committed Nov 27, 2024
1 parent 270eaaf commit 3fa7a5a
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 12 deletions.
3 changes: 2 additions & 1 deletion api.planx.uk/modules/file/service/deleteFile.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { DeleteObjectsCommandInput } from "@aws-sdk/client-s3";
import type { DeleteObjectsRequest } from "aws-sdk/clients/s3.js";
import { getS3KeyFromURL, s3Factory } from "./utils.js";

Expand All @@ -10,7 +11,7 @@ export const deleteFilesByURL = async (

export const deleteFilesByKey = async (keys: string[]): Promise<string[]> => {
const s3 = s3Factory();
const params = getDeleteFilesParams(keys);
const params = getDeleteFilesParams(keys) as DeleteObjectsCommandInput;
try {
s3.deleteObjects(params);
return keys;
Expand Down
4 changes: 2 additions & 2 deletions api.planx.uk/modules/file/service/getFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ export const getFileFromS3 = async (fileId: string) => {
const file = await s3.getObject(params);

return {
body: file.Body as Buffer,
body: file.Body,
isPrivate: file.Metadata?.is_private === "true",
headers: {
"Content-Type": file.ContentType,
"Content-Length": file.ContentLength,
"Content-Disposition": file.ContentDisposition,
"Content-Encoding": file.ContentEncoding,
"Cache-Control": file.CacheControl,
Expires: file.Expires,
Expires: file.ExpiresString,
"Last-Modified": file.LastModified,
ETag: file.ETag,
"cross-origin-resource-policy": "cross-site",
Expand Down
23 changes: 14 additions & 9 deletions api.planx.uk/modules/file/service/uploadFile.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import type { PutObjectCommandInput } from "@aws-sdk/client-s3";
import { customAlphabet } from "nanoid";
import {
GetObjectCommand,
type PutObjectCommandInput,
} from "@aws-sdk/client-s3";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
import mime from "mime";
import { s3Factory } from "./utils.js";
import { customAlphabet } from "nanoid";
import { isLiveEnv } from "../../../helpers.js";
import { s3Factory } from "./utils.js";
const nanoid = customAlphabet("1234567890abcdefghijklmnopqrstuvwxyz", 8);

export const uploadPublicFile = async (
Expand Down Expand Up @@ -46,16 +50,17 @@ export const uploadPrivateFile = async (
};

// Construct an API URL for the uploaded file
const buildFileUrl = (key: string, path: "public" | "private") => {
const buildFileUrl = async (key: string, path: "public" | "private") => {
const s3 = s3Factory();
const s3Url = new URL(s3.getSignedUrl("getObject", { Key: key }));
let s3Pathname = s3Url.pathname;
const s3Url = await getSignedUrl(
s3,
new GetObjectCommand({ Key: key, Bucket: process.env.AWS_S3_BUCKET }),
);
let s3Pathname = s3Url;
// Minio returns a pathname with bucket name prepended, remove this
if (!isLiveEnv())
s3Pathname = s3Pathname.replace(`/${process.env.AWS_S3_BUCKET}`, "");
// URL.pathname has a leading "/"
const fileUrl = `${process.env.API_URL_EXT}/file/${path}${s3Pathname}`;
return fileUrl;
return `${process.env.API_URL_EXT}/file/${path}${s3Pathname}`;
};

export function generateFileParams(
Expand Down
1 change: 1 addition & 0 deletions api.planx.uk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"dependencies": {
"@airbrake/node": "^2.1.8",
"@aws-sdk/client-s3": "^3.696.0",
"@aws-sdk/s3-request-presigner": "^3.701.0",
"@opensystemslab/planx-core": "git+https://github.com/theopensystemslab/planx-core#ccf9ac3",
"@types/isomorphic-fetch": "^0.0.36",
"adm-zip": "^0.5.10",
Expand Down
27 changes: 27 additions & 0 deletions api.planx.uk/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3fa7a5a

Please sign in to comment.