-
Notifications
You must be signed in to change notification settings - Fork 0
[Feat/#145] 이미지 최적화 및 이미지 전송 구조 개편 #146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
doyeon0307
wants to merge
10
commits into
develop
Choose a base branch
from
feat/#145-image
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a22b57d
[Refactor/#145] Qaulifier 이름 변경
doyeon0307 21d81d2
[Feat/#145] 이미지 최적화 위한 FileLocalDataSource 구현
doyeon0307 232e10c
[Feat/#145] 이미지 파일 업로드 위한 FileUploadRepository 구현
doyeon0307 fa9cf53
[Feat/#145] 이미지 업로드 유스케이스 생성 (V2)
doyeon0307 88b81cf
[Refactor/#145] FileUploadRemoteDataSource로 이름 변경
doyeon0307 6d69b09
[Fix/#143] 오탈자 수정
doyeon0307 1b19a90
[Fix/#145] 오탈자 수정
doyeon0307 ae9c4de
[Fix/#145] 이미지 업로드 유스케이스 수정
doyeon0307 181453f
[Fix/#145] clearDirectory 에러처리 수정
doyeon0307 d2252e4
[Fix/#145] 유스케이스 에러 처리 수정
doyeon0307 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
app/src/main/java/com/poti/android/core/common/constant/ImageConstants.kt
This file contains hidden or 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 com.poti.android.core.common.constant | ||
|
|
||
| object ImageConstants { | ||
| const val IMAGE_EXTENSION = "jpg" | ||
| const val IMAGE_CONTENT_TYPE = "image/jpeg" | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
91 changes: 91 additions & 0 deletions
91
app/src/main/java/com/poti/android/data/local/datasource/FileLocalDataSource.kt
This file contains hidden or 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,91 @@ | ||
| package com.poti.android.data.local.datasource | ||
|
|
||
| import android.content.Context | ||
| import android.graphics.Bitmap | ||
| import android.graphics.ImageDecoder | ||
| import android.net.Uri | ||
| import android.util.Size | ||
| import androidx.core.net.toUri | ||
| import com.poti.android.core.common.constant.ImageConstants.IMAGE_EXTENSION | ||
| import dagger.hilt.android.qualifiers.ApplicationContext | ||
| import java.io.ByteArrayOutputStream | ||
| import java.io.File | ||
| import java.util.UUID | ||
| import javax.inject.Inject | ||
| import kotlin.math.max | ||
|
|
||
| class FileLocalDataSource @Inject constructor( | ||
| @param:ApplicationContext private val context: Context, | ||
| ) { | ||
| fun createImageFile(uriString: String): File { | ||
| val uri = uriString.toUri() | ||
|
|
||
| val directory = getDirectory() | ||
| val compressedImage = compressImage(uri, directory) | ||
|
|
||
| return compressedImage | ||
| } | ||
|
|
||
| fun clearDirectory() { | ||
| val directory = getDirectory() | ||
| directory.listFiles()?.forEach { it.delete() } | ||
| } | ||
|
|
||
| private fun getDirectory(): File = File(context.cacheDir, DIRECTORY).apply { | ||
| mkdirs() | ||
| } | ||
|
|
||
| private fun compressImage( | ||
| uri: Uri, | ||
| dir: File, | ||
| ): File { | ||
| val source = ImageDecoder.createSource(context.contentResolver, uri) | ||
| val bitmap = ImageDecoder.decodeBitmap(source) { decoder, info, _ -> | ||
| // 디코더 설정 | ||
| decoder.allocator = ImageDecoder.ALLOCATOR_SOFTWARE | ||
| decoder.isMutableRequired = true | ||
|
|
||
| // 리사이징 크기 설정 | ||
| val targetSize = calculateTargetSize(info.size.width, info.size.height) | ||
| decoder.setTargetSize(targetSize.width, targetSize.height) | ||
| } | ||
|
|
||
| // bitmap을 리사이징(압축)한 jpeg byteArray 생성 | ||
| val compressedImage = ByteArrayOutputStream().use { stream -> | ||
| bitmap.compress(Bitmap.CompressFormat.JPEG, QUALITY, stream) | ||
| bitmap.recycle() | ||
| stream.toByteArray() | ||
| } | ||
|
|
||
| // 임시 파일 객체 생성 | ||
| val tempFile = File( | ||
| dir, | ||
| "${UUID.randomUUID()}.$IMAGE_EXTENSION", | ||
| ) | ||
|
|
||
| // 파일 객채에 리사이징 byteArray 덮어씌워 최종 이미지 파일 생성 | ||
| tempFile.outputStream().use { stream -> | ||
| stream.write(compressedImage) | ||
| } | ||
|
|
||
| return tempFile | ||
| } | ||
|
|
||
| private fun calculateTargetSize( | ||
| width: Int, | ||
| height: Int, | ||
| ): Size { | ||
| if (width <= MAX_WIDTH && height <= MAX_HEIGHT) return Size(width, height) | ||
|
|
||
| val ratio = max(width.toFloat() / MAX_WIDTH, height.toFloat() / MAX_HEIGHT) | ||
|
|
||
| return Size((width / ratio).toInt(), (height / ratio).toInt()) | ||
| } | ||
|
|
||
| companion object { | ||
| private const val MAX_WIDTH = 1024 | ||
| private const val MAX_HEIGHT = 1024 | ||
| private const val QUALITY = 80 | ||
| private const val DIRECTORY = "compressed" | ||
| } | ||
| } |
37 changes: 37 additions & 0 deletions
37
app/src/main/java/com/poti/android/data/remote/datasource/FileUploadRemoteDataSource.kt
This file contains hidden or 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,37 @@ | ||
| package com.poti.android.data.remote.datasource | ||
|
|
||
| import com.poti.android.core.common.constant.ImageConstants.IMAGE_CONTENT_TYPE | ||
| import com.poti.android.data.di.FileUploadClient | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.withContext | ||
| import okhttp3.MediaType.Companion.toMediaType | ||
| import okhttp3.OkHttpClient | ||
| import okhttp3.Request | ||
| import okhttp3.RequestBody.Companion.asRequestBody | ||
| import java.io.File | ||
| import javax.inject.Inject | ||
|
|
||
| class FileUploadRemoteDataSource @Inject constructor( | ||
| @param:FileUploadClient private val okHttpClient: OkHttpClient, | ||
| ) { | ||
| suspend fun uploadImage( | ||
| uploadUrl: String, | ||
| file: File, | ||
| ) = withContext(Dispatchers.IO) { | ||
| val requestBody = file.asRequestBody(IMAGE_CONTENT_TYPE.toMediaType()) | ||
|
|
||
| val request = Request.Builder() | ||
| .url(uploadUrl) | ||
| .put(requestBody) | ||
| .header("Content-Type", IMAGE_CONTENT_TYPE) | ||
| .build() | ||
|
|
||
| okHttpClient.newCall(request).execute().use { response -> | ||
| if (!response.isSuccessful) { | ||
| throw IllegalStateException( | ||
| "File upload failed: ${response.code}", | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } |
39 changes: 39 additions & 0 deletions
39
app/src/main/java/com/poti/android/data/repository/FileUploadRepositoryImpl.kt
This file contains hidden or 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,39 @@ | ||
| package com.poti.android.data.repository | ||
|
|
||
| import com.poti.android.core.network.util.HttpResponseHandler | ||
| import com.poti.android.data.local.datasource.FileLocalDataSource | ||
| import com.poti.android.data.remote.datasource.FileUploadRemoteDataSource | ||
| import com.poti.android.domain.repository.FileUploadRepository | ||
| import java.io.File | ||
| import javax.inject.Inject | ||
|
|
||
| class FileUploadRepositoryImpl @Inject constructor( | ||
| private val httpResponseHandler: HttpResponseHandler, | ||
| private val fileUploadRemoteDataSource: FileUploadRemoteDataSource, | ||
| private val fileLocalDataSource: FileLocalDataSource, | ||
| ) : FileUploadRepository { | ||
| override suspend fun uploadImage( | ||
| uploadUrl: String, | ||
| file: File, | ||
| ): Result<Unit> = httpResponseHandler.safeApiCall { | ||
| fileUploadRemoteDataSource.uploadImage(uploadUrl, file) | ||
| } | ||
|
|
||
| override fun createImage(uriString: String): Result<File> { | ||
| try { | ||
| val file = fileLocalDataSource.createImageFile(uriString) | ||
| return Result.success(file) | ||
| } catch (exception: Throwable) { | ||
| return Result.failure(exception) | ||
| } | ||
| } | ||
|
|
||
| override fun clearDirectory(): Result<Unit> { | ||
| try { | ||
| fileLocalDataSource.clearDirectory() | ||
| return Result.success(Unit) | ||
| } catch (exception: Throwable) { | ||
| return Result.failure(exception) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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
14 changes: 14 additions & 0 deletions
14
app/src/main/java/com/poti/android/domain/repository/FileUploadRepository.kt
This file contains hidden or 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 com.poti.android.domain.repository | ||
|
|
||
| import java.io.File | ||
|
|
||
| interface FileUploadRepository { | ||
| suspend fun uploadImage( | ||
| uploadUrl: String, | ||
| file: File, | ||
| ): Result<Unit> | ||
|
|
||
| fun createImage(uriString: String): Result<File> | ||
|
|
||
| fun clearDirectory(): Result<Unit> | ||
| } | ||
|
doyeon0307 marked this conversation as resolved.
|
||
61 changes: 61 additions & 0 deletions
61
app/src/main/java/com/poti/android/domain/usecase/image/UploadImagesUseCaseV2.kt
This file contains hidden or 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,61 @@ | ||
| package com.poti.android.domain.usecase.image | ||
|
|
||
| import com.poti.android.core.common.constant.ImageConstants.IMAGE_EXTENSION | ||
| import com.poti.android.domain.model.image.PresignedUploadInfo | ||
| import com.poti.android.domain.repository.FileUploadRepository | ||
| import com.poti.android.domain.repository.ImageRepository | ||
| import java.io.File | ||
| import javax.inject.Inject | ||
| import kotlin.coroutines.cancellation.CancellationException | ||
|
|
||
| class UploadImagesUseCaseV2 @Inject constructor( | ||
| private val imageRepository: ImageRepository, | ||
| private val fileUploadRepository: FileUploadRepository, | ||
| ) { | ||
| suspend operator fun invoke( | ||
| uploadType: String, | ||
| uriStrings: List<String>, | ||
| ): Result<List<String>> { | ||
| try { | ||
| val uploadInfos = getUploadUrls(uploadType, uriStrings.size) | ||
| val (urls, fileNames) = uploadInfos.map { it.url to it.fileName }.unzip() | ||
| val files = createImages(uriStrings) | ||
|
|
||
| uploadImages(urls, files) | ||
|
|
||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| return Result.success(fileNames) | ||
| } catch (t: Throwable) { | ||
| if (t is CancellationException) throw t | ||
| return Result.failure(t) | ||
| } finally { | ||
| fileUploadRepository.clearDirectory() | ||
| } | ||
|
doyeon0307 marked this conversation as resolved.
|
||
| } | ||
|
|
||
| private suspend fun getUploadUrls( | ||
| uploadType: String, | ||
| size: Int, | ||
| ): List<PresignedUploadInfo> = imageRepository.getPresignedUrls( | ||
| type = uploadType, | ||
| extensions = List(size) { IMAGE_EXTENSION }, | ||
| ).getOrThrow() | ||
|
|
||
| private fun createImages( | ||
| uriStrings: List<String>, | ||
| ): List<File> = uriStrings.map { uri -> | ||
| fileUploadRepository.createImage(uri).getOrThrow() | ||
| } | ||
|
|
||
| private suspend fun uploadImages( | ||
| urls: List<String>, | ||
| files: List<File>, | ||
| ) { | ||
| if (urls.size != files.size) { | ||
| throw IllegalStateException("Upload URL count and file count must match") | ||
| } | ||
|
|
||
| for (i in urls.indices) { | ||
| fileUploadRepository.uploadImage(urls[i], files[i]).getOrThrow() | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.