Created
June 7, 2018 06:36
-
-
Save RadiationX/cbb6dd8094c7eccc805e6148ed5088d2 to your computer and use it in GitHub Desktop.
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
package ru.istra.rentspec.data.repository.media | |
import android.graphics.Bitmap | |
import android.graphics.BitmapFactory | |
import android.graphics.Matrix | |
import io.reactivex.Completable | |
import io.reactivex.Single | |
import okhttp3.MultipartBody | |
import okhttp3.RequestBody | |
import ru.istra.rentspec.data.entity.media.ImageItem | |
import ru.istra.rentspec.data.mappers.MediaMapper | |
import ru.istra.rentspec.data.net.api.MediaApi | |
import ru.istra.rentspec.data.net.response.ApiResponse | |
import ru.istra.rentspec.data.net.response.ApiResponseEmpty | |
import ru.istra.rentspec.data.net.response.media.MediaResponse | |
import ru.istra.rentspec.data.system.SchedulersProvider | |
import java.io.ByteArrayOutputStream | |
import java.io.File | |
import javax.inject.Inject | |
class MediaRepository @Inject constructor( | |
private val mediaApi: MediaApi, | |
private val schedulers: SchedulersProvider | |
) { | |
fun upload(file: File, type: String, inTemp: Boolean = true): Single< ImageItem> { | |
val compress = true | |
val bitmap = getBitmap(file, compress, 2560, 2560) | |
return Single | |
.defer { | |
val requestBodyBuilder = MultipartBody.Builder().setType(MultipartBody.FORM) | |
val requestFile = if (compress) { | |
RequestBody.create(MultipartBody.FORM, bitmapToByteArray(bitmap)) | |
} else { | |
RequestBody.create(MultipartBody.FORM, file) | |
} | |
requestBodyBuilder.addFormDataPart("image", file.name, requestFile) | |
requestBodyBuilder.addFormDataPart("type", type) | |
/*orderId?.let { | |
requestBodyBuilder.addFormDataPart("order_id", it.toString()) | |
}*/ | |
if (inTemp) { | |
mediaApi | |
.uploadTemp(requestBodyBuilder.build()) | |
} else { | |
mediaApi | |
.upload(requestBodyBuilder.build()) | |
} | |
} | |
.compose(ApiResponse.fetchResult()) | |
.map { | |
MediaMapper.mapToEntity(it).apply { | |
localFile = file | |
} | |
} | |
.subscribeOn(schedulers.io()) | |
.observeOn(schedulers.ui()) | |
} | |
fun delete(id: Long): Completable = mediaApi | |
.delete(id) | |
.compose(ApiResponseEmpty.fetchResult()) | |
.toCompletable() | |
.subscribeOn(schedulers.io()) | |
.observeOn(schedulers.ui()) | |
private fun getBitmap(file: File, compress: Boolean, maxWidth: Int, maxHeight: Int): Bitmap { | |
if (compress) { | |
val options = BitmapFactory.Options() | |
options.inJustDecodeBounds = true | |
BitmapFactory.decodeFile(file.absolutePath, options) | |
val sourceWidth = options.outWidth | |
val sourceHeight = options.outHeight | |
val rate = Math.max(sourceWidth / maxWidth.toFloat(), sourceHeight / maxHeight.toFloat()) | |
options.inJustDecodeBounds = false | |
options.inSampleSize = rate.toInt() | |
val bitmap = BitmapFactory.decodeFile(file.absolutePath, options) | |
val w0 = bitmap.width | |
val h0 = bitmap.height | |
val scaleWidth = maxWidth / w0.toFloat() | |
val scaleHeight = maxHeight / h0.toFloat() | |
val maxScale = Math.min(scaleWidth, scaleHeight) | |
val matrix = Matrix() | |
matrix.reset() | |
if (maxScale < 1) { | |
matrix.postScale(maxScale, maxScale) | |
} | |
val result = Bitmap.createBitmap(bitmap, 0, 0, w0, h0, matrix, true) | |
return result | |
} else { | |
return BitmapFactory.decodeFile(file.absolutePath) | |
} | |
} | |
private fun bitmapToByteArray(bitmap: Bitmap): ByteArray { | |
val outStream = ByteArrayOutputStream() | |
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, outStream) | |
return outStream.use { it.toByteArray() } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment