generated from ajou4095/template-android
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature] PreSigned 관련 API / Repository 연결 (#38)
- Loading branch information
Showing
7 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
data/src/main/kotlin/ac/dnd/bookkeeping/android/data/remote/network/api/FileApi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package ac.dnd.bookkeeping.android.data.remote.network.api | ||
|
||
import ac.dnd.bookkeeping.android.data.remote.network.di.AuthHttpClient | ||
import ac.dnd.bookkeeping.android.data.remote.network.di.NoAuthHttpClient | ||
import ac.dnd.bookkeeping.android.data.remote.network.environment.BaseUrlProvider | ||
import ac.dnd.bookkeeping.android.data.remote.network.environment.ErrorMessageMapper | ||
import ac.dnd.bookkeeping.android.data.remote.network.model.file.GetPreSignedUrlRes | ||
import ac.dnd.bookkeeping.android.data.remote.network.util.convert | ||
import android.net.Uri | ||
import android.webkit.MimeTypeMap | ||
import io.ktor.client.HttpClient | ||
import io.ktor.client.request.forms.formData | ||
import io.ktor.client.request.forms.submitFormWithBinaryData | ||
import io.ktor.client.request.get | ||
import io.ktor.client.request.parameter | ||
import io.ktor.http.Headers | ||
import io.ktor.http.HttpHeaders | ||
import java.io.File | ||
import javax.inject.Inject | ||
|
||
|
||
class FileApi @Inject constructor( | ||
@NoAuthHttpClient private val noAuthClient: HttpClient, | ||
@AuthHttpClient private val client: HttpClient, | ||
private val baseUrlProvider: BaseUrlProvider, | ||
private val errorMessageMapper: ErrorMessageMapper | ||
) { | ||
private val baseUrl: String | ||
get() = baseUrlProvider.get() | ||
|
||
suspend fun getPreSignedUrl( | ||
fileName: String | ||
): Result<GetPreSignedUrlRes> { | ||
return client.get("$baseUrl/api/v1/files/presigned") { | ||
parameter("fileName", fileName) | ||
}.convert(errorMessageMapper::map) | ||
} | ||
|
||
suspend fun upload( | ||
preSignedUrl: String, | ||
imageUri: String, | ||
fileName: String? = null | ||
): Result<Unit> { | ||
val image = Uri.parse(imageUri)?.path ?: let { | ||
return Result.failure(IllegalArgumentException("Invalid imageUri")) | ||
} | ||
val file = File(image) | ||
val name = fileName ?: file.name | ||
val contentType = getContentType(file.path) | ||
|
||
return noAuthClient.submitFormWithBinaryData( | ||
url = preSignedUrl, | ||
formData = formData { | ||
append( | ||
"image", | ||
file.readBytes(), | ||
Headers.build { | ||
contentType?.let { append(HttpHeaders.ContentType, it) } | ||
append(HttpHeaders.ContentDisposition, "filename=$name") | ||
} | ||
) | ||
} | ||
).convert(errorMessageMapper::map) | ||
} | ||
|
||
private fun getContentType( | ||
url: String | ||
): String? { | ||
return MimeTypeMap.getFileExtensionFromUrl(url)?.let { fileExtension -> | ||
MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension) | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...in/kotlin/ac/dnd/bookkeeping/android/data/remote/network/model/file/GetPreSignedUrlRes.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package ac.dnd.bookkeeping.android.data.remote.network.model.file | ||
|
||
import ac.dnd.bookkeeping.android.data.remote.mapper.DataMapper | ||
import ac.dnd.bookkeeping.android.domain.model.file.PreSignedUrl | ||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class GetPreSignedUrlRes( | ||
@SerialName("preSignedUrl") | ||
val preSignedUrl: String, | ||
@SerialName("uploadFileUrl") | ||
val uploadFileUrl: String | ||
) : DataMapper<PreSignedUrl> { | ||
override fun toDomain(): PreSignedUrl { | ||
return PreSignedUrl( | ||
preSignedUrl = preSignedUrl, | ||
uploadFileUrl = uploadFileUrl | ||
) | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
data/src/main/kotlin/ac/dnd/bookkeeping/android/data/repository/file/MockFileRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package ac.dnd.bookkeeping.android.data.repository.file | ||
|
||
import ac.dnd.bookkeeping.android.domain.model.file.PreSignedUrl | ||
import ac.dnd.bookkeeping.android.domain.repository.FileRepository | ||
import kotlinx.coroutines.delay | ||
import javax.inject.Inject | ||
|
||
class MockFileRepository @Inject constructor() : FileRepository { | ||
override suspend fun getPreSignedUrl( | ||
fileName: String | ||
): Result<PreSignedUrl> { | ||
randomShortDelay() | ||
return Result.success( | ||
PreSignedUrl( | ||
preSignedUrl = "https://example.com", | ||
uploadFileUrl = "https://example.com" | ||
) | ||
) | ||
} | ||
|
||
override suspend fun upload( | ||
preSignedUrl: String, | ||
imageUri: String | ||
): Result<Unit> { | ||
randomLongDelay() | ||
return Result.success(Unit) | ||
} | ||
|
||
private suspend fun randomShortDelay() { | ||
delay(LongRange(100, 500).random()) | ||
} | ||
|
||
private suspend fun randomLongDelay() { | ||
delay(LongRange(500, 2000).random()) | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
data/src/main/kotlin/ac/dnd/bookkeeping/android/data/repository/file/RealFileRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package ac.dnd.bookkeeping.android.data.repository.file | ||
|
||
import ac.dnd.bookkeeping.android.data.remote.local.SharedPreferencesManager | ||
import ac.dnd.bookkeeping.android.data.remote.network.api.FileApi | ||
import ac.dnd.bookkeeping.android.data.remote.network.util.toDomain | ||
import ac.dnd.bookkeeping.android.domain.model.file.PreSignedUrl | ||
import ac.dnd.bookkeeping.android.domain.repository.FileRepository | ||
import javax.inject.Inject | ||
|
||
class RealFileRepository @Inject constructor( | ||
private val fileApi: FileApi, | ||
private val sharedPreferencesManager: SharedPreferencesManager | ||
) : FileRepository { | ||
override suspend fun getPreSignedUrl( | ||
fileName: String | ||
): Result<PreSignedUrl> { | ||
return fileApi.getPreSignedUrl(fileName).toDomain() | ||
} | ||
|
||
override suspend fun upload( | ||
preSignedUrl: String, | ||
imageUri: String | ||
): Result<Unit> { | ||
return fileApi.upload( | ||
preSignedUrl = preSignedUrl, | ||
imageUri = imageUri | ||
) | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
domain/src/main/kotlin/ac/dnd/bookkeeping/android/domain/model/file/PreSignedUrl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package ac.dnd.bookkeeping.android.domain.model.file | ||
|
||
data class PreSignedUrl( | ||
val preSignedUrl: String, | ||
val uploadFileUrl: String | ||
) |
14 changes: 14 additions & 0 deletions
14
domain/src/main/kotlin/ac/dnd/bookkeeping/android/domain/repository/FileRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package ac.dnd.bookkeeping.android.domain.repository | ||
|
||
import ac.dnd.bookkeeping.android.domain.model.file.PreSignedUrl | ||
|
||
interface FileRepository { | ||
suspend fun getPreSignedUrl( | ||
fileName: String | ||
): Result<PreSignedUrl> | ||
|
||
suspend fun upload( | ||
preSignedUrl: String, | ||
imageUri: String | ||
): Result<Unit> | ||
} |