Skip to content

Commit

Permalink
feat: file upload done
Browse files Browse the repository at this point in the history
  • Loading branch information
ashup99 committed Jul 13, 2024
2 parents 5c6cfd8 + 35ca745 commit 61f6763
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 66 deletions.
27 changes: 0 additions & 27 deletions .github/workflows/main.yml

This file was deleted.

1 change: 1 addition & 0 deletions packages/upload/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
},
"dependencies": {
"@aws-sdk/client-s3": "3.47.0",
"@fastify/cors": "^9.0.1",
"@fastify/multipart": "^8.3.0",
"@medihacks/prisma": "workspace:^",
"fastify": "^4.28.1"
Expand Down
3 changes: 1 addition & 2 deletions packages/upload/src/filebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ export const uploadFile = async ({ file, user }: { file: MultipartFile; user: Us
Bucket: user.id,
ContentType: file.mimetype
})
const out = await headObject({ fileName: file.filename, bucketName: user.id })
return out
return headObject({ fileName: file.filename, bucketName: user.id })
}

export const headObject = ({ bucketName, fileName }: { bucketName: string; fileName: string }) =>
Expand Down
5 changes: 5 additions & 0 deletions packages/upload/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ import FastifyMultipart from '@fastify/multipart'
import { isTokenValid } from './utils'
import { uploadFile } from './filebase'
import { prisma } from './prisma'
import FastifyCors from '@fastify/cors'

const fastify = Fastify({
logger: true
})
fastify.register(FastifyMultipart)
await fastify.register(FastifyCors, {
origin: true,
methods: ['POST']
})

fastify.post('/upload', async (req, res) => {
const token = (req.query as Record<string, string>).token
Expand Down
22 changes: 0 additions & 22 deletions packages/web/src/app/(protected)/doctor/dashboard/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,13 @@ const PatientDashboard = () => {
}
`

const GENERATE_PRE_SIGNED_URL = gql`
mutation GeneratePreSignedUploadUrl($userId: ID!) {
generatePreSignedUploadUrl(userId: $userId)
}
`

const [addPatient, { data: addPatientData, loading: addPatientLoading, error: addPatientError }] = useMutation(
ADD_PATIENT,
{
context: { appTokenName: APP_NAME }
}
)

const [
generatePreSignedUrl,
{ data: generatePreSignedUrlData, loading: generatePreSignedUrlLoading, error: generatePreSignedUrlError }
] = useMutation(GENERATE_PRE_SIGNED_URL, {
context: { appTokenName: APP_NAME }
})

const onAddingThePatient = async () => {
setSuccessMessage('')
setErrorMessage('')
Expand All @@ -64,15 +51,6 @@ const PatientDashboard = () => {
})
setSuccessMessage('')
setErrorMessage('')
await generatePreSignedUrl({ variables: { userId: selectedPatientId } })
.then(async (gen_result) => {
console.log({ gen_result })
setSuccessMessage('Presigned URL generated successfully')
})
.catch((error) => {
console.error({ error })
setErrorMessage(error?.message)
})
}

const onPatientChange = (event: React.ChangeEvent<HTMLSelectElement>) => {
Expand Down
29 changes: 14 additions & 15 deletions packages/web/src/app/(protected)/doctor/documents/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,23 +138,22 @@ function Documents() {
.then(async (res) => {
const uploadUrlApi = res?.data?.generatePreSignedUploadUrl
if (uploadUrlApi) {
// let formData = new FormData()
// currentFileUploadData?.arrayBuffer().then((arrayBufferData)=>{
// const blobData=new Blob()
// })
// formData.append('file', currentFileUploadData)
// formData.append('password', 'John123')
let formData = new FormData()
currentFileUploadData?.arrayBuffer().then(async (arrayBufferData) => {
const blobData = new Blob([arrayBufferData], { type: currentFileUploadData?.type })
formData.append('file', blobData, currentFileUploadData?.name)

await fetch(uploadUrlApi, {
body: currentFileUploadData,
method: 'POST'
})
.then((uploadRes) => {
console.log({ uploadRes })
})
.catch((uploadError) => {
console.log({ uploadError })
await fetch(uploadUrlApi, {
body: formData,
method: 'POST'
})
.then((uploadRes) => {
console.log({ uploadRes })
})
.catch((uploadError) => {
console.log({ uploadError })
})
})
} else {
setErrorMessage('Upload URL is not valid')
}
Expand Down
23 changes: 23 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit 61f6763

Please sign in to comment.