1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-11-21 22:27:29 +00:00

convert downloadutils to kotlin

This commit is contained in:
Austin Huang 2021-06-22 16:06:14 -04:00
parent a7cb82354e
commit 3dcc791cba
No known key found for this signature in database
GPG Key ID: 84C23AA04587A91F
4 changed files with 460 additions and 484 deletions

View File

@ -193,9 +193,9 @@ object BitmapUtils {
}
@Throws(IOException::class)
fun convertToJpegAndSaveToFile(contentResolver: ContentResolver, bitmap: Bitmap, file: DocumentFile?): DocumentFile {
fun convertToJpegAndSaveToFile(contentResolver: ContentResolver, bitmap: Bitmap, file: DocumentFile?): DocumentFile? {
val tempFile = file ?: DownloadUtils.getTempFile(null, "jpg")
contentResolver.openOutputStream(tempFile.uri).use { output ->
contentResolver.openOutputStream(tempFile!!.uri).use { output ->
val compressResult = bitmap.compress(Bitmap.CompressFormat.JPEG, 100, output)
if (!compressResult) {
throw RuntimeException("Compression failed!")

File diff suppressed because it is too large Load Diff

View File

@ -36,8 +36,8 @@ object MediaUploader {
contentResolver: ContentResolver,
bitmap: Bitmap,
): MediaUploadResponse = withContext(Dispatchers.IO) {
val file: DocumentFile = BitmapUtils.convertToJpegAndSaveToFile(contentResolver, bitmap, null)
val byteLength: Long = file.length()
val file: DocumentFile? = BitmapUtils.convertToJpegAndSaveToFile(contentResolver, bitmap, null)
val byteLength: Long = file!!.length()
val options = createUploadPhotoOptions(byteLength)
val headers = getUploadPhotoHeaders(options)
val url = HOST + "/rupload_igphoto/" + options.name + "/"

View File

@ -131,7 +131,7 @@ class DownloadWorker(context: Context, workerParams: WorkerParameters) : Corouti
var totalRead = 0f
try {
BufferedInputStream(urlConnection.getInputStream()).use { bis ->
contentResolver.openOutputStream(outFile.uri).use { fos ->
contentResolver.openOutputStream(outFile!!.uri).use { fos ->
val buffer = ByteArray(0x2000)
var count: Int
while (bis.read(buffer, 0, 0x2000).also { count = it } != -1) {
@ -146,11 +146,11 @@ class DownloadWorker(context: Context, workerParams: WorkerParameters) : Corouti
}
}
} catch (e: Exception) {
Log.e(TAG, "Error while writing data from url: " + url + " to file: " + outFile.name, e)
Log.e(TAG, "Error while writing data from url: " + url + " to file: " + outFile!!.name, e)
}
if (isJpg) {
try {
contentResolver.openInputStream(outFile.uri).use { fis ->
contentResolver.openInputStream(outFile!!.uri).use { fis ->
contentResolver.openOutputStream(filePath.uri).use { fos ->
val jpegIptcRewriter = JpegIptcRewriter()
jpegIptcRewriter.removeIPTC(fis, fos)
@ -158,10 +158,10 @@ class DownloadWorker(context: Context, workerParams: WorkerParameters) : Corouti
}
} catch (e: Exception) {
Log.e(TAG, "Error while removing iptc: url: " + url
+ ", tempFile: " + outFile.name
+ ", tempFile: " + outFile!!.name
+ ", finalFile: " + filePath.name, e)
}
val deleted = outFile.delete()
val deleted = outFile!!.delete()
if (!deleted) {
Log.w(TAG, "download: tempFile not deleted!")
}
@ -393,7 +393,7 @@ class DownloadWorker(context: Context, workerParams: WorkerParameters) : Corouti
class Builder {
private var urlToFilePathMap: MutableMap<String, String> = mutableMapOf()
fun setUrlToFilePathMap(urlToFilePathMap: MutableMap<String, DocumentFile>): Builder {
fun setUrlToFilePathMap(urlToFilePathMap: Map<String, DocumentFile>): Builder {
this.urlToFilePathMap = urlToFilePathMap
.mapValues { it.value.uri.toString() }
.toMutableMap()