upload.ts45 lines · main
| 1 | import { createClient } from '@supabase/supabase-js' |
| 2 | |
| 3 | const SUPPORT_API_URL = process.env.NEXT_PUBLIC_SUPPORT_API_URL || '' |
| 4 | const SUPPORT_API_KEY = process.env.NEXT_PUBLIC_SUPPORT_ANON_KEY || '' |
| 5 | |
| 6 | // [Joshen TODO] Feedback form and support attachments should use this |
| 7 | export const uploadAttachment = async ( |
| 8 | bucket: string, |
| 9 | fileName: string, |
| 10 | image: File, |
| 11 | getUrl: boolean = true |
| 12 | ) => { |
| 13 | const brivenClient = createClient(SUPPORT_API_URL, SUPPORT_API_KEY, { |
| 14 | auth: { |
| 15 | persistSession: false, |
| 16 | autoRefreshToken: false, |
| 17 | // @ts-ignore |
| 18 | multiTab: false, |
| 19 | detectSessionInUrl: false, |
| 20 | localStorage: { |
| 21 | getItem: (_key: string) => undefined, |
| 22 | setItem: (_key: string, _value: string) => {}, |
| 23 | removeItem: (_key: string) => {}, |
| 24 | }, |
| 25 | }, |
| 26 | }) |
| 27 | |
| 28 | const options = { cacheControl: '3600' } |
| 29 | |
| 30 | const { data: file, error } = await brivenClient.storage |
| 31 | .from(bucket) |
| 32 | .upload(fileName, image, options) |
| 33 | |
| 34 | if (error) { |
| 35 | console.error('Failed to upload:', error) |
| 36 | return undefined |
| 37 | } |
| 38 | |
| 39 | if (file && getUrl) { |
| 40 | const { data } = await brivenClient.storage.from(bucket).getPublicUrl(file.path) |
| 41 | return data?.publicUrl |
| 42 | } |
| 43 | |
| 44 | return undefined |
| 45 | } |