You've already forked pages
forked from didirus/AstralRinth
* refactor: migrate to common eslint+prettier configs * fix: prettier frontend * feat: config changes * fix: lint issues * fix: lint * fix: type imports * fix: cyclical import issue * fix: lockfile * fix: missing dep * fix: switch to tabs * fix: continue switch to tabs * fix: rustfmt parity * fix: moderation lint issue * fix: lint issues * fix: ui intl * fix: lint issues * Revert "fix: rustfmt parity" This reverts commit cb99d2376c321d813d4b7fc7e2a213bb30a54711. * feat: revert last rs
47 lines
1.2 KiB
TypeScript
47 lines
1.2 KiB
TypeScript
type ImageUploadContext = {
|
|
projectID?: string
|
|
context: 'project' | 'version' | 'thread_message' | 'report'
|
|
}
|
|
|
|
interface ImageUploadResponse {
|
|
id: string
|
|
url: string
|
|
}
|
|
|
|
export const useImageUpload = async (file: File, ctx: ImageUploadContext) => {
|
|
// Make sure file is of type image/png, image/jpeg, image/gif, or image/webp
|
|
if (
|
|
!file.type.startsWith('image/') ||
|
|
!['png', 'jpeg', 'gif', 'webp'].includes(file.type.split('/')[1])
|
|
) {
|
|
throw new Error('File is not an accepted image type')
|
|
}
|
|
|
|
// Make sure file is less than 1MB
|
|
if (file.size > 1024 * 1024) {
|
|
throw new Error('File exceeds the 1MiB size limit')
|
|
}
|
|
|
|
const qs = new URLSearchParams()
|
|
if (ctx.projectID) qs.set('project_id', ctx.projectID)
|
|
qs.set('context', ctx.context)
|
|
qs.set('ext', file.type.split('/')[1])
|
|
const url = `image?${qs.toString()}`
|
|
|
|
const response = (await useBaseFetch(url, {
|
|
method: 'POST',
|
|
body: file,
|
|
apiVersion: 3,
|
|
})) as ImageUploadResponse
|
|
|
|
// Type check to see if response has a url property and an id property
|
|
if (!response?.id || typeof response.id !== 'string') {
|
|
throw new Error('Unexpected response from server')
|
|
}
|
|
if (!response?.url || typeof response.url !== 'string') {
|
|
throw new Error('Unexpected response from server')
|
|
}
|
|
|
|
return response
|
|
}
|