Skip to content

Commit

Permalink
align fileManager implementation with other PRs
Browse files Browse the repository at this point in the history
  • Loading branch information
MalpenZibo committed Sep 18, 2024
1 parent da4ed48 commit 9922170
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 35 deletions.
66 changes: 32 additions & 34 deletions packages/commons/src/file-manager/fileManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ export type FileManager = {
},
logger: Logger
) => Promise<string>;
storeBytesByPath: (
storeBytesByKey: (
bucket: string,
path: string,
key: string,
fileContent: Buffer,
logger: Logger
) => Promise<string>;
Expand Down Expand Up @@ -85,6 +85,25 @@ export function initFileManager(
): string =>
[path, resourceId, fileName].filter((s) => s && s.length > 0).join("/");

const store = async (
bucket: string,
key: string,
fileContent: Buffer
): Promise<string> => {
try {
await client.send(
new PutObjectCommand({
Bucket: bucket,
Key: key,
Body: fileContent,
})
);
return key;
} catch (error) {
throw fileManagerStoreBytesError(key, bucket, error);
}
};

return {
buildS3Key: (
path: string,
Expand Down Expand Up @@ -172,6 +191,15 @@ export function initFileManager(
throw fileManagerListFilesError(bucket, error);
}
},
storeBytesByKey: async (
bucket: string,
key: string,
fileContent: Buffer,
logger: Logger
): Promise<string> => {
logger.info(`Storing file ${key} in bucket ${bucket}`);
return store(bucket, key, fileContent);
},
storeBytes: async (
s3File: {
bucket: string;
Expand All @@ -184,18 +212,8 @@ export function initFileManager(
): Promise<string> => {
const key = buildS3Key(s3File.path, s3File.resourceId, s3File.name);
logger.info(`Storing file ${key} in bucket ${s3File.bucket}`);
try {
await client.send(
new PutObjectCommand({
Bucket: s3File.bucket,
Key: key,
Body: s3File.content,
})
);
return key;
} catch (error) {
throw fileManagerStoreBytesError(key, s3File.bucket, error);
}

return store(s3File.bucket, key, s3File.content);
},
generateGetPresignedUrl: async (
bucketName: string,
Expand All @@ -217,25 +235,5 @@ export function initFileManager(
const command = new PutObjectCommand({ Bucket: bucketName, Key: key });
return getSignedUrl(client, command, { expiresIn: durationInMinutes });
},
storeBytesByPath: async (
bucket: string,
path: string,
fileContent: Buffer,
logger: Logger
): Promise<string> => {
logger.info(`Storing file ${path} in bucket ${bucket}`);
try {
await client.send(
new PutObjectCommand({
Bucket: bucket,
Key: path,
Body: fileContent,
})
);
return path;
} catch (error) {
throw fileManagerStoreBytesError(path, bucket, error);
}
},
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export const downloadCSV = async (
const { blob, filename } = await downloadFile(sourceUrl);

const zipFile = Buffer.from(await blob.arrayBuffer());
await fileManager.storeBytesByPath(
await fileManager.storeBytesByKey(
bucket,
`organizations/${filename}`,
zipFile,
Expand Down

0 comments on commit 9922170

Please sign in to comment.