Skip to content

Commit

Permalink
fix: remove downloadFilePublic, since the bucket is public
Browse files Browse the repository at this point in the history
  • Loading branch information
Harrish92 committed Dec 18, 2024
1 parent e818901 commit 24f37a2
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 100 deletions.
13 changes: 0 additions & 13 deletions app/src/main/java/com/android/periodpals/model/user/UserModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,6 @@ interface UserRepository {
* @param onSuccess Callback function to be called on success.
* @param onFailure Callback function to be called when there is an exception.
*/
suspend fun downloadFilePublic(
filePath: String,
onSuccess: (bytes: ByteArray) -> Unit,
onFailure: (Exception) -> Unit
)

/**
* Downloads a file from the storage for the authenticated user.
*
* @param filePath The path of the file to be downloaded.
* @param onSuccess Callback function to be called on success.
* @param onFailure Callback function to be called when there is an exception.
*/
suspend fun downloadFile(
filePath: String,
onSuccess: (bytes: ByteArray) -> Unit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,31 +133,14 @@ class UserRepositorySupabase(private val supabase: SupabaseClient) : UserReposit
}
}

override suspend fun downloadFilePublic(
filePath: String,
onSuccess: (bytes: ByteArray) -> Unit,
onFailure: (Exception) -> Unit,
) {
try {
withContext(Dispatchers.Main) {
val file = supabase.storage.from("avatars").downloadPublic("$filePath.jpg")
Log.d(TAG, "downloadFilePublic: Success")
onSuccess(file)
}
} catch (e: Exception) {
Log.d(TAG, "downloadFilePublic: fail to download file: ${e.message}")
onFailure(e)
}
}

override suspend fun downloadFile(
filePath: String,
onSuccess: (bytes: ByteArray) -> Unit,
onFailure: (Exception) -> Unit,
) {
try {
withContext(Dispatchers.Main) {
val file = supabase.storage.from("avatars").downloadAuthenticated("$filePath.jpg")
val file = supabase.storage.from("avatars").downloadPublic("$filePath.jpg")
Log.d(TAG, "downloadFile: Success")
onSuccess(file)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,35 +240,6 @@ class UserViewModel(private val userRepository: UserRepositorySupabase) : ViewMo
* @param onSuccess Callback function to be called on success.
* @param onFailure Callback function to be called when there is an exception.
*/
fun downloadFilePublic(
filePath: String,
onSuccess: () -> Unit = { Log.d(TAG, "downloadFile success callback") },
onFailure: (Exception) -> Unit = { e: Exception ->
Log.d(TAG, "downloadFile failure callback: ${e.message}")
}
) {
viewModelScope.launch {
userRepository.downloadFilePublic(
filePath,
onSuccess = { bytes ->
Log.d(TAG, "downloadFile: Success")
_avatar.value = bytes
onSuccess()
},
onFailure = { e: Exception ->
Log.d(TAG, "downloadFile: fail to download file: ${e.message}")
onFailure(e)
})
}
}

/**
* Downloads a file from the storage for the authenticated user.
*
* @param filePath The path of the file to be downloaded.
* @param onSuccess Callback function to be called on success.
* @param onFailure Callback function to be called when there is an exception.
*/
fun downloadFile(
filePath: String,
onSuccess: () -> Unit = { Log.d(TAG, "downloadFile success callback") },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,21 +239,6 @@ class UserRepositorySupabaseTest {
}
}

@Test
fun downloadFilePublicHasFailed() {
var result = false

runTest {
val userRepositorySupabase = UserRepositorySupabase(supabaseClientFailure)
userRepositorySupabase.downloadFilePublic(
"test",
{ fail("should not call onSuccess") },
{ result = true },
)
assert(result)
}
}

@Test
fun downloadFileHasFailed() {
var result = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,18 +211,6 @@ class UserViewModelTest {
assert(test)
}

@Test
fun downloadFilePublicIsSuccessful() = runTest {
val expected = byteArrayOf(1)
doAnswer { it.getArgument<(ByteArray) -> Unit>(1)(expected) }
.`when`(userModel)
.downloadFilePublic(any(), any<(ByteArray) -> Unit>(), any<(Exception) -> Unit>())

userViewModel.downloadFilePublic("test")

assertEquals(expected, userViewModel.avatar.value)
}

@Test
fun downloadFileIsSuccessful() = runTest {
val expected = byteArrayOf(1)
Expand All @@ -235,17 +223,6 @@ class UserViewModelTest {
assertEquals(expected, userViewModel.avatar.value)
}

@Test
fun downloadFilePublicHasFailed() = runTest {
doAnswer { it.getArgument<(Exception) -> Unit>(2)(Exception("failed")) }
.`when`(userModel)
.downloadFilePublic(any(), any<(ByteArray) -> Unit>(), any<(Exception) -> Unit>())

userViewModel.downloadFilePublic("test")

assertNull(userViewModel.avatar.value)
}

@Test
fun downloadFileHasFailed() = runTest {
doAnswer { it.getArgument<(Exception) -> Unit>(2)(Exception("failed")) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,15 @@ class ProfileScreenTest {

@Test
fun initVmSuccess() {
`when`(init(userViewModel, authenticationViewModel, chatViewModel, userViewModel.user.value))
`when`(
init(
userViewModel,
authenticationViewModel,
chatViewModel,
userViewModel.user.value,
{},
{},
))
.thenAnswer({
val onSuccess = it.arguments[2] as () -> Unit
onSuccess()
Expand All @@ -184,7 +192,15 @@ class ProfileScreenTest {

@Test
fun initVmFailure() {
`when`(init(userViewModel, authenticationViewModel, chatViewModel, userViewModel.user.value))
`when`(
init(
userViewModel,
authenticationViewModel,
chatViewModel,
userViewModel.user.value,
{},
{},
))
.thenAnswer({
val onFailure = it.arguments[1] as () -> Unit
onFailure()
Expand Down

0 comments on commit 24f37a2

Please sign in to comment.