-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from LanPet-dev/feat/upload_resource
Feat/upload resource
- Loading branch information
Showing
14 changed files
with
384 additions
and
96 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
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
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
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
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
27 changes: 27 additions & 0 deletions
27
data/repository/src/main/java/com/lanpet/data/repository/S3UploadRepositoryImpl.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,27 @@ | ||
package com.lanpet.data.repository | ||
|
||
import com.lanpet.data.service.S3UploadApiService | ||
import com.lanpet.domain.repository.S3UploadRepository | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.flow | ||
import kotlinx.coroutines.flow.flowOn | ||
import okhttp3.MediaType.Companion.toMediaTypeOrNull | ||
import okhttp3.RequestBody.Companion.toRequestBody | ||
import javax.inject.Inject | ||
|
||
class S3UploadRepositoryImpl | ||
@Inject | ||
constructor( | ||
private val s3UploadApiService: S3UploadApiService, | ||
) : S3UploadRepository { | ||
override fun uploadImageResource( | ||
url: String, | ||
byteArray: ByteArray, | ||
): Flow<Unit> = | ||
flow { | ||
val requestBody = byteArray.toRequestBody("image/jpeg".toMediaTypeOrNull()) | ||
val res = s3UploadApiService.uploadImage(url = url, body = requestBody) | ||
emit(res) | ||
}.flowOn(Dispatchers.IO) | ||
} |
60 changes: 60 additions & 0 deletions
60
data/service/src/main/java/com/lanpet/data/service/S3UploadApiClient.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,60 @@ | ||
package com.lanpet.data.service | ||
|
||
import com.google.gson.GsonBuilder | ||
import com.lanpet.core.manager.AuthStateHolder | ||
import com.lanpet.domain.model.AuthState | ||
import okhttp3.Interceptor | ||
import okhttp3.OkHttpClient | ||
import retrofit2.Retrofit | ||
import retrofit2.converter.gson.GsonConverterFactory | ||
import javax.inject.Inject | ||
|
||
/** | ||
* S3UploadApiClient는 S3 업로드를 위한 API 클라이언트입니다. | ||
* 현재 페이지에서 사용하는 `baseUrl`은 S3UploadApiClient 빌드에만 사용되며, | ||
* 실제 API 통신에는 사용되지 않습니다. | ||
*/ | ||
class S3UploadApiClient | ||
@Inject | ||
constructor( | ||
private val authStateHolder: AuthStateHolder, | ||
private val baseUrl: String = "https://lanpet-resource.s3.ap-northeast-2.amazonaws.com/", | ||
) { | ||
private val headerInterceptor = | ||
Interceptor { chain -> | ||
if (authStateHolder.authState.value !is AuthState.Loading && | ||
authStateHolder.authState.value !is AuthState.Success | ||
) { | ||
throw SecurityException("x-access-token is required") | ||
} | ||
|
||
val request = | ||
chain | ||
.request() | ||
.newBuilder() | ||
.addHeader("Content-Type", "image/jpeg") | ||
.build() | ||
chain.proceed(request) | ||
} | ||
|
||
private val gson = | ||
GsonBuilder() | ||
.create() | ||
|
||
private val okHttpClient = | ||
OkHttpClient | ||
.Builder() | ||
.addInterceptor(headerInterceptor) | ||
.build() | ||
|
||
private fun apiService() = | ||
Retrofit | ||
.Builder() | ||
.baseUrl(baseUrl) | ||
.client(okHttpClient) | ||
.addConverterFactory(GsonConverterFactory.create(gson)) | ||
.build() | ||
.create(S3UploadApiService::class.java) | ||
|
||
fun getService(): S3UploadApiService = apiService() | ||
} |
16 changes: 16 additions & 0 deletions
16
data/service/src/main/java/com/lanpet/data/service/S3UploadApiService.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,16 @@ | ||
package com.lanpet.data.service | ||
|
||
import okhttp3.RequestBody | ||
import retrofit2.http.Body | ||
import retrofit2.http.Header | ||
import retrofit2.http.PUT | ||
import retrofit2.http.Url | ||
|
||
interface S3UploadApiService { | ||
@PUT | ||
suspend fun uploadImage( | ||
@Url url: String, | ||
@Header("Content-Type") contentType: String = "image/jpeg", | ||
@Body body: RequestBody, | ||
) | ||
} |
10 changes: 10 additions & 0 deletions
10
domain/repository/src/main/java/com/lanpet/domain/repository/S3UploadRepository.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,10 @@ | ||
package com.lanpet.domain.repository | ||
|
||
import kotlinx.coroutines.flow.Flow | ||
|
||
interface S3UploadRepository { | ||
fun uploadImageResource( | ||
url: String, | ||
byteArray: ByteArray, | ||
): Flow<Unit> | ||
} |
15 changes: 0 additions & 15 deletions
15
.../usecase/src/main/java/com/lanpet/domain/usecase/freeboard/GetResourceUploadUrlUseCase.kt
This file was deleted.
Oops, something went wrong.
48 changes: 48 additions & 0 deletions
48
...n/usecase/src/main/java/com/lanpet/domain/usecase/freeboard/UploadImageResourceUseCase.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,48 @@ | ||
package com.lanpet.domain.usecase.freeboard | ||
|
||
import com.lanpet.domain.repository.FreeBoardRepository | ||
import com.lanpet.domain.repository.S3UploadRepository | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.FlowPreview | ||
import kotlinx.coroutines.async | ||
import kotlinx.coroutines.awaitAll | ||
import kotlinx.coroutines.coroutineScope | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.coroutines.flow.flatMapConcat | ||
import kotlinx.coroutines.flow.flow | ||
import kotlinx.coroutines.flow.flowOn | ||
import javax.inject.Inject | ||
|
||
class UploadImageResourceUseCase | ||
@Inject | ||
constructor( | ||
private val freeBoardRepository: FreeBoardRepository, | ||
private val s3UploadRepository: S3UploadRepository, | ||
) { | ||
@OptIn(FlowPreview::class) | ||
operator fun invoke( | ||
sarangbangId: String, | ||
imageList: List<ByteArray>, | ||
) = freeBoardRepository | ||
.getResourceUploadUrl(sarangbangId, imageList.size) | ||
.flatMapConcat { urlItems -> | ||
flow { | ||
coroutineScope { | ||
val results = urlItems.items.mapIndexed { index, url -> | ||
async { | ||
s3UploadRepository | ||
.uploadImageResource( | ||
url = url, | ||
byteArray = imageList[index] | ||
) | ||
.first() | ||
} | ||
} | ||
delay(2000) | ||
emit(results.awaitAll()) | ||
} | ||
} | ||
} | ||
.flowOn(Dispatchers.IO) | ||
} |
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
Oops, something went wrong.