diff --git a/retrofitLib/build.gradle b/retrofitLib/build.gradle index bcd845c..5750b7f 100644 --- a/retrofitLib/build.gradle +++ b/retrofitLib/build.gradle @@ -11,8 +11,6 @@ android { minSdkVersion 14 targetSdkVersion 30 - testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner' - compileOptions { sourceCompatibility = '1.8' targetCompatibility = '1.8' @@ -46,7 +44,6 @@ dependencies { implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.1" - implementation 'com.github.zeropercenthappy:ZPHAndroidUtils:1.4.1' api "com.squareup.okhttp3:okhttp:$okhttp_version" api "com.squareup.retrofit2:retrofit:$retrofit_version" api "com.squareup.retrofit2:converter-gson:$retrofit_version" diff --git a/retrofitLib/src/main/java/com/zeropercenthappy/retrofitutil/RequestBodyBuilder.kt b/retrofitLib/src/main/java/com/zeropercenthappy/retrofitutil/RequestBodyBuilder.kt index c3c1dcd..7ec73b1 100644 --- a/retrofitLib/src/main/java/com/zeropercenthappy/retrofitutil/RequestBodyBuilder.kt +++ b/retrofitLib/src/main/java/com/zeropercenthappy/retrofitutil/RequestBodyBuilder.kt @@ -1,6 +1,6 @@ package com.zeropercenthappy.retrofitutil -import com.zeropercenthappy.utilslibrary.utils.FileUtils +import com.zeropercenthappy.retrofitutil.ext.mimeType import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.MultipartBody import okhttp3.RequestBody @@ -18,15 +18,11 @@ object RequestBodyBuilder { fun createMultipartBodyPartList(fileMap: Map): List { val partList = arrayListOf() for (key in fileMap.keys) { - val file = fileMap[key] - file?.apply { - val mimeType = FileUtils.getFileMimeType(file) - mimeType?.apply { - val requestBody = file.asRequestBody(mimeType.toMediaTypeOrNull()) - val part = MultipartBody.Part.createFormData(key, file.name, requestBody) - partList.add(part) - } - } + val file = fileMap[key] ?: continue + val mimeType = file.mimeType.takeIf { it.isNotEmpty() } ?: continue + val requestBody = file.asRequestBody(mimeType.toMediaTypeOrNull()) + val part = MultipartBody.Part.createFormData(key, file.name, requestBody) + partList.add(part) } return partList } diff --git a/retrofitLib/src/main/java/com/zeropercenthappy/retrofitutil/ext/FileExt.kt b/retrofitLib/src/main/java/com/zeropercenthappy/retrofitutil/ext/FileExt.kt new file mode 100644 index 0000000..db62659 --- /dev/null +++ b/retrofitLib/src/main/java/com/zeropercenthappy/retrofitutil/ext/FileExt.kt @@ -0,0 +1,20 @@ +package com.zeropercenthappy.retrofitutil.ext + +import android.webkit.MimeTypeMap +import java.io.File +import java.io.InputStream + +val File.mimeType: String + get() = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension) ?: "" + +fun File.writeFromInputStream(inputStream: InputStream): Boolean { + return try { + this.outputStream().buffered().use { output -> + inputStream.copyTo(output) + } + true + } catch (e: Exception) { + e.printStackTrace() + false + } +} \ No newline at end of file diff --git a/retrofitLib/src/main/java/com/zeropercenthappy/retrofitutil/service/download/DownloadService.kt b/retrofitLib/src/main/java/com/zeropercenthappy/retrofitutil/service/download/DownloadService.kt index 52cc40d..7d04452 100644 --- a/retrofitLib/src/main/java/com/zeropercenthappy/retrofitutil/service/download/DownloadService.kt +++ b/retrofitLib/src/main/java/com/zeropercenthappy/retrofitutil/service/download/DownloadService.kt @@ -5,7 +5,7 @@ import android.app.IntentService import android.content.Context import android.content.Intent import com.zeropercenthappy.retrofitutil.RetrofitBuilder -import com.zeropercenthappy.utilslibrary.utils.FileUtils +import com.zeropercenthappy.retrofitutil.ext.writeFromInputStream import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.runBlocking import java.io.File @@ -54,7 +54,8 @@ class DownloadService : IntentService("DownloadService") { @Suppress("UNCHECKED_CAST") override fun onHandleIntent(intent: Intent?) { - downloadMap = intent?.getSerializableExtra(EXTRA_DOWNLOAD_MAP) as Map? ?: return + downloadMap = + intent?.getSerializableExtra(EXTRA_DOWNLOAD_MAP) as Map? ?: return download() } @@ -67,8 +68,7 @@ class DownloadService : IntentService("DownloadService") { continue } val responseBody = api.download(url) - val result = FileUtils.writeFileByIS(cacheFile, responseBody.byteStream()) - + val result = cacheFile.writeFromInputStream(responseBody.byteStream()) // Download success, move cache file to destination file if (result) { cacheFile.renameTo(destinationFile)