Skip to content

Commit

Permalink
feat: update to supabase storage (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyochan authored May 25, 2024
1 parent 7d5247b commit b1f24bf
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
18 changes: 18 additions & 0 deletions app/audio.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {css} from '@emotion/native';
import {IconButton, Typography} from 'dooboo-ui';
import {Audio} from 'expo-av';

import {uploadFileToSupabase} from '../supabase';

export default function AudioPage(): JSX.Element {
const [permissionResponse, requestPermission] = Audio.usePermissions();
const [recording, setRecording] = useState<Audio.Recording>();
Expand Down Expand Up @@ -77,6 +79,20 @@ export default function AudioPage(): JSX.Element {
}
};

const uploadFile = async (): Promise<void> => {
try {
const path = await uploadFileToSupabase({
file: uri!,
bucket: 'audios',
destPath: 'audio.m4a',
});

console.log('Uploaded to', path);

Check warning on line 90 in app/audio.tsx

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement
} catch (err) {
console.error('Failed to upload file', err);

Check warning on line 92 in app/audio.tsx

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement
}
};

return (
<SafeAreaView
style={css`
Expand All @@ -98,6 +114,8 @@ export default function AudioPage(): JSX.Element {
onPress={uri ? (isPlaying ? sound?.stopAsync : playSound) : undefined}
size={40}
/>
<Typography.Heading3>Upload</Typography.Heading3>
{uri ? <IconButton icon="Upload" onPress={uploadFile} size={40} /> : null}
<View
style={css`
height: 80px;
Expand Down
20 changes: 20 additions & 0 deletions supabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,23 @@ export const supabase = createClient<Database>(supabaseUrl, supabaseAnonKey, {
detectSessionInUrl: false,
},
});

export const uploadFileToSupabase = async ({
file,
bucket,
destPath,
}: {
file: string;
bucket: string;
destPath: string;
}): Promise<string> => {
const {data, error} = await supabase.storage
.from(bucket)
.upload(destPath, file, {});

if (error) {
throw error;
}

return data.path;
};

0 comments on commit b1f24bf

Please sign in to comment.