-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2eed923
commit 43c9bc8
Showing
3 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import * as fs from 'fs'; | ||
import * as mime from 'mime-lite'; | ||
import { ClientInterface } from './client'; | ||
|
||
type ImageInputWithReferenceId = { | ||
id: string; | ||
filename: string; | ||
mimeType: string; | ||
}; | ||
|
||
type UploadHandler = ImageInputWithReferenceId & { | ||
buffer: Buffer; | ||
stats: fs.Stats; | ||
apiClient: ClientInterface; | ||
}; | ||
|
||
const MUTATION_UPLOAD_FILE = `#graphql | ||
mutation UPLOAD_FILE ($tenantId: ID!, $filename: String!, $mimeType: String!) { | ||
fileUpload { | ||
generatePresignedRequest( | ||
tenantId: $tenantId | ||
filename: $filename | ||
contentType: $mimeType | ||
type: MEDIA | ||
) { | ||
url | ||
fields { | ||
name | ||
value | ||
} | ||
} | ||
} | ||
}`; | ||
|
||
export async function uploadImageToTenant({ | ||
id, | ||
mimeType, | ||
filename, | ||
buffer, | ||
stats, | ||
apiClient, | ||
}: UploadHandler): Promise<string | false> { | ||
const signedRequestResult = await apiClient.pimApi(MUTATION_UPLOAD_FILE, { | ||
tenantId: id, | ||
filename, | ||
mimeType, | ||
}); | ||
|
||
const payload = signedRequestResult.fileUpload.generatePresignedRequest; | ||
const formData: FormData = new FormData(); | ||
payload.fields.forEach((field: { name: string; value: string }) => { | ||
formData.append(field.name, field.value); | ||
}); | ||
formData.append('file', new Blob([buffer])); | ||
|
||
const response = await fetch(payload.url, { | ||
method: 'POST', | ||
headers: new Headers({ 'Content-Length': String(stats.size) }), | ||
body: formData, | ||
}); | ||
|
||
return response.status === 201 ? (formData.get('key') as string) : false; | ||
} | ||
|
||
export async function handleImageUpload(imagePath: any, apiClient: ClientInterface, tenantId: string): Promise<any> { | ||
if (!imagePath) return; | ||
|
||
const extension = imagePath.split('.').pop() as string; | ||
const mimeType = mime.getType(extension); | ||
const filename = imagePath.split('T/').pop() as string; | ||
|
||
if (!mimeType) { | ||
console.log('Could not find mime type for file. Halting upload'); | ||
return null; | ||
} | ||
|
||
const stats = fs.statSync(imagePath); | ||
const buffer = fs.readFileSync(imagePath); | ||
|
||
const data: Omit<UploadHandler, 'id'> = { | ||
mimeType, | ||
filename, | ||
stats, | ||
buffer, | ||
apiClient, | ||
}; | ||
|
||
const imageKey = await uploadImageToTenant({ | ||
id: tenantId, | ||
...data, | ||
}); | ||
|
||
return imageKey; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters