Skip to content

Commit

Permalink
Fix handle both multipart form data and JSON data for S3 uploads
Browse files Browse the repository at this point in the history
- Update  method to handle JSON data and save it as a .json file with a version-timestamp filename.
- Modify  method to support the updated  method.
- Update  handler to correctly process both multipart form data and JSON data.
  • Loading branch information
vixhnuchandran committed Jun 5, 2024
1 parent 90be654 commit c225d12
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 14 deletions.
11 changes: 8 additions & 3 deletions src/routes/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,21 @@ switch (storeType) {

// Set Data
export const setData = async (req: Request, res: Response) => {
const { dataset, id, name, replace }: SetDataRequest = req.body
const {
dataset,
id,
name,
replace,
}: { dataset: string; id: string; name: string; replace: boolean } = req.body
let data: any
const isReplace =
replace && replace.toString().toLowerCase() === "true" ? true : false
const isReplace = replace && replace.toString().toLowerCase() === "true"

if (req.file) {
data = req.file
} else {
data = req.body.data
}

const validationResult = validateSetDataReq({
dataset,
id,
Expand Down
50 changes: 39 additions & 11 deletions src/utils/dbStore.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Client, ResultSet } from "@libsql/client"
import { S3 } from "aws-sdk"
import crypto from "crypto"

export class dbStore {
client: Client
Expand All @@ -18,22 +19,36 @@ export class dbStore {
dataset: string,
hashedId: string,
name: string,
data: any
data: any,
version?: string | number
): Promise<string> {
const originalName = data.originalname || ""
const currentDate = Date.now()
const s3Key = `${dataset}/${hashedId}/${name}/${currentDate}-${originalName}`
let s3Key: string
let body: Buffer
let contentType: string

if (Buffer.isBuffer(data.buffer)) {
const originalName = data.originalname ?? version
const currentDate = Date.now()
s3Key = `${dataset}/${hashedId}/${name}/${originalName}-${currentDate}`
body = data.buffer
contentType = data.mimetype
} else {
const currentDate = Date.now()
s3Key = `${dataset}/${hashedId}/${name}/${version}-${currentDate}.json`
body = Buffer.from(JSON.stringify(data))
contentType = "application/json"
}

const s3UploadParams = {
Bucket: process.env.AWS_S3_BUCKET_NAME,
Key: s3Key,
Body: data.buffer,
ContentType: data.mimetype,
Body: body,
ContentType: contentType,
}

try {
await this.s3.putObject(s3UploadParams).promise()
console.log(`Data file "${originalName}" saved to S3 successfully.`)
console.log(`Data file saved to S3 successfully with key "${s3Key}".`)
const s3Url = `https://${process.env.AWS_S3_BUCKET_NAME}.s3.${process.env.AWS_REGION}.amazonaws.com/${s3Key}`
return s3Url
} catch (error) {
Expand Down Expand Up @@ -109,7 +124,14 @@ export class dbStore {
let queryResult

if (!existingData.rows[0]) {
const s3Url = await this.uploadToS3(dataset, hashedId, name, data)
const s3Url = await this.uploadToS3(
dataset,
hashedId,
name,
data,
version
)

queryResult = await this.insertData(
dataset,
hashedId,
Expand All @@ -125,7 +147,13 @@ export class dbStore {
let s3Url
if (replace) {
const highestVersion = Math.max(...keys)
s3Url = await this.uploadToS3(dataset, hashedId, name, data)
s3Url = await this.uploadToS3(
dataset,
hashedId,
name,
data,
highestVersion
)
currDataJson[highestVersion] = s3Url
version = highestVersion
} else {
Expand All @@ -150,13 +178,13 @@ export class dbStore {
}

public async getData(
dataset: string,
hashedId: string,
id: string,
name: string,
version?: number
): Promise<any> {
try {
const result = await this.getExistingData(hashedId, id, name)
const result = await this.getExistingData(dataset, hashedId, name)
const currData = JSON.parse(String(result.rows[0].data))
let data

Expand Down

0 comments on commit c225d12

Please sign in to comment.