Skip to content

Commit

Permalink
refactor: Move to utils, only use in s3 upload
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr committed Dec 7, 2024
1 parent 96b5b8e commit 96087e6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 28 deletions.
20 changes: 1 addition & 19 deletions api.planx.uk/modules/file/service/uploadFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import mime from "mime";
import { customAlphabet } from "nanoid";
import { isLiveEnv } from "../../../helpers.js";
import { s3Factory } from "./utils.js";
import { Readable } from "stream";
const nanoid = customAlphabet("1234567890abcdefghijklmnopqrstuvwxyz", 8);

export const uploadPublicFile = async (
Expand All @@ -29,14 +28,10 @@ export const uploadPublicFile = async (
};

export const uploadPrivateFile = async (
file: Express.Multer.File | Buffer,
file: Express.Multer.File,
filename: string,
filekey?: string,
) => {
if (file instanceof Buffer) {
file = convertToMulterFile(file);
}

const s3 = s3Factory();

const { params, key, fileType } = generateFileParams(file, filename, filekey);
Expand Down Expand Up @@ -68,19 +63,6 @@ const buildFileUrl = async (key: string, path: "public" | "private") => {
return `${process.env.API_URL_EXT}/file/${path}${s3Pathname}`;
};

const convertToMulterFile = (buffer: Buffer): Express.Multer.File => ({
buffer: buffer,
originalname: "${data.id}.json",
mimetype: "application/json",
size: buffer.length,
fieldname: "file",
encoding: "7bit",
stream: Readable.from(buffer),
destination: "",
filename: "",
path: "",
});

export function generateFileParams(
file: Express.Multer.File,
filename: string,
Expand Down
21 changes: 21 additions & 0 deletions api.planx.uk/modules/file/service/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { S3 } from "@aws-sdk/client-s3";
import { isLiveEnv } from "../../../helpers.js";
import { Readable } from "stream";

export function s3Factory() {
return new S3({
Expand Down Expand Up @@ -40,3 +41,23 @@ export function getS3KeyFromURL(fileURL: string): string {
const key = [folder, file].map(decodeURIComponent).join("/");
return key;
}

export const convertObjectToMulterJSONFile = (
data: Record<string, unknown>,
fileName: string,
): Express.Multer.File => {
const buffer = Buffer.from(JSON.stringify(data));

return {
buffer: buffer,
originalname: fileName,
mimetype: "application/json",
size: buffer.length,
fieldname: "file",
encoding: "7bit",
stream: Readable.from(buffer),
destination: "",
filename: "",
path: "",
};
};
17 changes: 8 additions & 9 deletions api.planx.uk/modules/send/s3/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ import { uploadPrivateFile } from "../../file/service/uploadFile.js";
import { markSessionAsSubmitted } from "../../saveAndReturn/service/utils.js";
import { isApplicationTypeSupported } from "../utils/helpers.js";
import type { SendIntegrationController } from "../types.js";

// TS fails to validate this complex type when awaited
// Represent as a simple object to allow enough typechecking to kick in
type DigitalPlanningPayload = Record<string, unknown>;
import { convertObjectToMulterJSONFile } from "../../file/service/utils.js";

interface CreateS3Application {
insertS3Application: {
Expand Down Expand Up @@ -49,13 +46,15 @@ const sendToS3: SendIntegrationController = async (_req, res, next) => {

// Generate the ODP Schema JSON, skipping validation if not a supported application type
const doValidation = isApplicationTypeSupported(passport);
const exportData: DigitalPlanningPayload =
await $api.export.digitalPlanningDataPayload(sessionId, doValidation);

const buffer = Buffer.from(JSON.stringify(exportData));
const exportData = await $api.export.digitalPlanningDataPayload(
sessionId,
doValidation,
);

// Create and upload the data as an S3 file
const { fileUrl } = await uploadPrivateFile(buffer, `${sessionId}.json`);
const filename = `${sessionId}.json`;
const file = convertObjectToMulterJSONFile(exportData, filename);
const { fileUrl } = await uploadPrivateFile(file, filename);

// Send a notification with the file URL to the Power Automate webhook
const webhookRequest: AxiosRequestConfig = {
Expand Down

0 comments on commit 96087e6

Please sign in to comment.