-
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.
Merge pull request #174 from Semoban/feature/fix_image_upload_logic
- Loading branch information
Showing
98 changed files
with
1,414 additions
and
1,143 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
25 changes: 25 additions & 0 deletions
25
app/src/main/java/com/project/meongcare/aws/model/data/remote/AWSS3Api.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,25 @@ | ||
package com.project.meongcare.aws.model.data.remote | ||
|
||
import com.project.meongcare.aws.model.entities.AWSS3Response | ||
import okhttp3.RequestBody | ||
import retrofit2.Response | ||
import retrofit2.http.Body | ||
import retrofit2.http.GET | ||
import retrofit2.http.Header | ||
import retrofit2.http.PUT | ||
import retrofit2.http.Query | ||
import retrofit2.http.Url | ||
|
||
interface AWSS3Api { | ||
@GET("aws/s3") | ||
suspend fun getPreSignedUrl( | ||
@Header("AccessToken") accessToken: String, | ||
@Query("fileName") fileName: String, | ||
): Response<AWSS3Response> | ||
|
||
@PUT | ||
suspend fun uploadImageToS3( | ||
@Url preSignedUrl: String, | ||
@Body image: RequestBody, | ||
): Response<Int> | ||
} |
43 changes: 43 additions & 0 deletions
43
app/src/main/java/com/project/meongcare/aws/model/data/remote/AWSS3RetrofitClient.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,43 @@ | ||
package com.project.meongcare.aws.model.data.remote | ||
|
||
import com.project.meongcare.BuildConfig | ||
import okhttp3.ResponseBody | ||
import retrofit2.Converter | ||
import retrofit2.Retrofit | ||
import retrofit2.converter.gson.GsonConverterFactory | ||
import java.lang.reflect.Type | ||
import javax.inject.Inject | ||
|
||
class AWSS3RetrofitClient | ||
@Inject | ||
constructor() { | ||
val awsS3Api: AWSS3Api by lazy { | ||
Retrofit.Builder() | ||
.baseUrl(BuildConfig.SEMOBAN_DOMAIN) | ||
.addConverterFactory(nullOnEmptyConverterFactory) | ||
.addConverterFactory(GsonConverterFactory.create()) | ||
.build() | ||
.create(AWSS3Api::class.java) | ||
} | ||
|
||
private val nullOnEmptyConverterFactory = | ||
object : Converter.Factory() { | ||
fun converterFactory() = this | ||
|
||
override fun responseBodyConverter( | ||
type: Type, | ||
annotations: Array<out Annotation>, | ||
retrofit: Retrofit, | ||
) = object : Converter<ResponseBody, Any?> { | ||
val nextResponseBodyConverter = | ||
retrofit.nextResponseBodyConverter<Any?>( | ||
converterFactory(), | ||
type, | ||
annotations, | ||
) | ||
|
||
override fun convert(value: ResponseBody) = | ||
if (value.contentLength() != 0L) nextResponseBodyConverter.convert(value) else null | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
app/src/main/java/com/project/meongcare/aws/model/data/repository/AWSS3Module.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,15 @@ | ||
package com.project.meongcare.aws.model.data.repository | ||
|
||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object AWSS3Module { | ||
@Provides | ||
fun provideAWSS3Repository(awsS3RepositoryImpl: AWSS3RepositoryImpl): AWSS3Repository { | ||
return awsS3RepositoryImpl | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
app/src/main/java/com/project/meongcare/aws/model/data/repository/AWSS3Repository.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.project.meongcare.aws.model.data.repository | ||
|
||
import com.project.meongcare.aws.model.entities.AWSS3Response | ||
import okhttp3.RequestBody | ||
|
||
interface AWSS3Repository { | ||
suspend fun getPreSignedUrl( | ||
accessToken: String, | ||
fileName: String, | ||
): AWSS3Response? | ||
|
||
suspend fun uploadImageToS3( | ||
preSignedUrl: String, | ||
image: RequestBody, | ||
): Int? | ||
} |
53 changes: 53 additions & 0 deletions
53
app/src/main/java/com/project/meongcare/aws/model/data/repository/AWSS3RepositoryImpl.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,53 @@ | ||
package com.project.meongcare.aws.model.data.repository | ||
|
||
import android.util.Log | ||
import com.project.meongcare.aws.model.data.remote.AWSS3RetrofitClient | ||
import com.project.meongcare.aws.model.entities.AWSS3Response | ||
import okhttp3.RequestBody | ||
import org.json.JSONObject | ||
import java.lang.Exception | ||
import javax.inject.Inject | ||
|
||
class AWSS3RepositoryImpl | ||
@Inject | ||
constructor(private val awsS3RetrofitClient: AWSS3RetrofitClient) : AWSS3Repository { | ||
override suspend fun getPreSignedUrl( | ||
accessToken: String, | ||
fileName: String, | ||
): AWSS3Response? { | ||
return try { | ||
val response = awsS3RetrofitClient.awsS3Api.getPreSignedUrl(accessToken, fileName) | ||
if (response.isSuccessful) { | ||
Log.d("AWSS3Repo-getPreUrl", "통신 성공 ${response.code()}") | ||
response.body() | ||
} else { | ||
val stringToJson = JSONObject(response.errorBody()?.string()!!) | ||
Log.e("AWSS3Repo-getPreUrl", "통신 실패 ${response.code()}\n$stringToJson") | ||
null | ||
} | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
null | ||
} | ||
} | ||
|
||
override suspend fun uploadImageToS3( | ||
preSignedUrl: String, | ||
image: RequestBody, | ||
): Int? { | ||
return try { | ||
val response = awsS3RetrofitClient.awsS3Api.uploadImageToS3(preSignedUrl, image) | ||
if (response.isSuccessful) { | ||
Log.d("AWSS3Repo-upload", "통신 성공 ${response.code()}") | ||
response.code() | ||
} else { | ||
val stringToJson = JSONObject(response.errorBody()?.string()!!) | ||
Log.e("AWSS3Repo-upload", "통신 실패 ${response.code()}\n$stringToJson") | ||
response.code() | ||
} | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
null | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
app/src/main/java/com/project/meongcare/aws/model/entities/AWSS3Response.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,5 @@ | ||
package com.project.meongcare.aws.model.entities | ||
|
||
data class AWSS3Response( | ||
val preSignedUrl: String, | ||
) |
12 changes: 12 additions & 0 deletions
12
app/src/main/java/com/project/meongcare/aws/util/AWSS3Constants.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,12 @@ | ||
package com.project.meongcare.aws.util | ||
|
||
// Parent Folder | ||
const val PARENT_FOLDER_PATH = "meongcare/" | ||
|
||
// Sub Folder | ||
const val DOG_FOLDER_PATH = "dog/" | ||
const val EXCRETA_FOLDER_PATH = "excreta/" | ||
const val FEED_FOLDER_PATH = "feed/" | ||
const val MEDICAL_RECORD_FOLDER_PATH = "medical-record/" | ||
const val MEMBER_FOLDER_PATH = "member/" | ||
const val SUPPLEMENTS_FOLDER_PATH = "supplements/" |
30 changes: 30 additions & 0 deletions
30
app/src/main/java/com/project/meongcare/aws/util/AWSS3ImageUtils.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,30 @@ | ||
package com.project.meongcare.aws.util | ||
|
||
import android.content.Context | ||
import android.net.Uri | ||
import java.io.File | ||
import java.io.FileOutputStream | ||
import java.util.UUID | ||
|
||
object AWSS3ImageUtils { | ||
private fun createUUID(): String { | ||
return UUID.randomUUID().toString() | ||
} | ||
|
||
fun convertUriToFile( | ||
context: Context, | ||
uri: Uri, | ||
): File { | ||
val contentResolver = context.contentResolver | ||
val file = File(context.cacheDir, createUUID()) | ||
file.deleteOnExit() | ||
|
||
contentResolver.openInputStream(uri).use { inputStream -> | ||
FileOutputStream(file).use { outputStream -> | ||
inputStream?.copyTo(outputStream) | ||
} | ||
} | ||
|
||
return file | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
app/src/main/java/com/project/meongcare/aws/viewmodel/AWSS3ViewModel.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,43 @@ | ||
package com.project.meongcare.aws.viewmodel | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.project.meongcare.aws.model.data.repository.AWSS3Repository | ||
import com.project.meongcare.aws.model.entities.AWSS3Response | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.launch | ||
import okhttp3.RequestBody | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class AWSS3ViewModel | ||
@Inject | ||
constructor(private val awsS3Repository: AWSS3Repository) : ViewModel() { | ||
private val _preSignedUrl = MutableLiveData<AWSS3Response?>() | ||
val preSignedUrl: LiveData<AWSS3Response?> | ||
get() = _preSignedUrl | ||
|
||
private val _uploadImageResponse = MutableLiveData<Int?>() | ||
val uploadImageResponse: LiveData<Int?> | ||
get() = _uploadImageResponse | ||
|
||
fun getPreSignedUrl( | ||
accessToken: String, | ||
fileName: String, | ||
) { | ||
viewModelScope.launch { | ||
_preSignedUrl.value = awsS3Repository.getPreSignedUrl(accessToken, fileName) | ||
} | ||
} | ||
|
||
fun uploadImageToS3( | ||
preSignedUrl: String, | ||
image: RequestBody, | ||
) { | ||
viewModelScope.launch { | ||
_uploadImageResponse.value = awsS3Repository.uploadImageToS3(preSignedUrl, image) | ||
} | ||
} | ||
} |
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
Oops, something went wrong.