upload.ts 1.2 KB

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