mirror of
https://github.com/mihonapp/mihon.git
synced 2024-11-21 20:47:03 -05:00
Rewrote IOHandler to Kotlin
This commit is contained in:
parent
4397a44b80
commit
fa5b64ce2e
3 changed files with 67 additions and 49 deletions
|
@ -1,45 +0,0 @@
|
|||
package eu.kanade.tachiyomi.data.io;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public class IOHandler {
|
||||
private static String getTempFilename(Context context) throws IOException {
|
||||
File outputDir = context.getCacheDir();
|
||||
File outputFile = File.createTempFile("temp_cover", "0", outputDir);
|
||||
return outputFile.getAbsolutePath();
|
||||
}
|
||||
|
||||
public static String downloadMediaAndReturnPath(FileInputStream input, Context context) {
|
||||
FileOutputStream output = null;
|
||||
try {
|
||||
|
||||
String tempFilename = getTempFilename(context);
|
||||
output = new FileOutputStream(tempFilename);
|
||||
|
||||
int read;
|
||||
byte[] bytes = new byte[4096];
|
||||
while ((read = input.read(bytes)) != -1) {
|
||||
output.write(bytes, 0, read);
|
||||
}
|
||||
return tempFilename;
|
||||
} catch (IOException ignored) {
|
||||
} finally {
|
||||
if (input != null) try {
|
||||
input.close();
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
if (output != null) try {
|
||||
output.close();
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
}
|
62
app/src/main/java/eu/kanade/tachiyomi/data/io/IOHandler.kt
Normal file
62
app/src/main/java/eu/kanade/tachiyomi/data/io/IOHandler.kt
Normal file
|
@ -0,0 +1,62 @@
|
|||
@file:JvmName("IOHandler")
|
||||
package eu.kanade.tachiyomi.data.io
|
||||
|
||||
import android.content.Context
|
||||
import eu.kanade.tachiyomi.R
|
||||
import eu.kanade.tachiyomi.util.ToastUtil
|
||||
import java.io.File
|
||||
import java.io.FileInputStream
|
||||
import java.io.FileOutputStream
|
||||
import java.io.IOException
|
||||
|
||||
/**
|
||||
* Returns temp file location
|
||||
*
|
||||
* @param context context of application
|
||||
* @throws IOException IO exception
|
||||
* @return location of temp file
|
||||
*/
|
||||
@Throws(IOException::class)
|
||||
private fun getTempFilename(context: Context): String {
|
||||
// Get output directory.
|
||||
val outputDir = context.cacheDir
|
||||
|
||||
// Create temporary file
|
||||
val outputFile = File.createTempFile("temp_cover", "0", outputDir)
|
||||
|
||||
// Return path of temporary file
|
||||
return outputFile.absolutePath
|
||||
}
|
||||
|
||||
/**
|
||||
* Download media to temp location and returns file path
|
||||
*
|
||||
* @param input input stream containing input file
|
||||
* @param context context of application
|
||||
* @throws IOException IO exception
|
||||
* @return location of temp file
|
||||
*/
|
||||
@Throws(IOException::class)
|
||||
fun downloadMediaAndReturnPath(input: FileInputStream, context: Context): String {
|
||||
var tempFilename = ""
|
||||
var output: FileOutputStream? = null
|
||||
try {
|
||||
// Get temp file name.
|
||||
tempFilename = getTempFilename(context)
|
||||
|
||||
output = FileOutputStream(tempFilename)
|
||||
// Copy input stream to temp location.
|
||||
input.copyTo(output)
|
||||
} catch (e: IOException) {
|
||||
// Show user something went wrong and print stackTrace.
|
||||
ToastUtil.showShort(context, R.string.notification_manga_update_failed)
|
||||
e.printStackTrace()
|
||||
} finally {
|
||||
// Close streams.
|
||||
input.close()
|
||||
output?.close()
|
||||
}
|
||||
|
||||
// Return temp name.
|
||||
return tempFilename
|
||||
}
|
|
@ -14,7 +14,6 @@ import eu.davidea.flexibleadapter.FlexibleAdapter
|
|||
import eu.kanade.tachiyomi.R
|
||||
import eu.kanade.tachiyomi.data.database.models.Category
|
||||
import eu.kanade.tachiyomi.data.database.models.Manga
|
||||
import eu.kanade.tachiyomi.data.io.IOHandler
|
||||
import eu.kanade.tachiyomi.data.library.LibraryUpdateService
|
||||
import eu.kanade.tachiyomi.event.LibraryMangasEvent
|
||||
import eu.kanade.tachiyomi.ui.base.fragment.BaseRxFragment
|
||||
|
@ -311,17 +310,18 @@ class LibraryFragment : BaseRxFragment<LibraryPresenter>(), ActionMode.Callback
|
|||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
|
||||
if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_IMAGE_OPEN) {
|
||||
selectedCoverManga?.let { manga ->
|
||||
|
||||
try {
|
||||
// Get the file's input stream from the incoming Intent
|
||||
val inputStream = context.contentResolver.openInputStream(data.data)
|
||||
|
||||
// Convert to absolute path to prevent FileNotFoundException
|
||||
val result = IOHandler.downloadMediaAndReturnPath(inputStream as FileInputStream,
|
||||
val result = eu.kanade.tachiyomi.data.io.downloadMediaAndReturnPath(inputStream as FileInputStream,
|
||||
context)
|
||||
|
||||
// Get file from filepath
|
||||
val picture = File(result ?: "")
|
||||
val picture = File(result)
|
||||
|
||||
try {
|
||||
// Update cover to selected file, show error if something went wrong
|
||||
if (presenter.editCoverWithLocalFile(picture, manga)) {
|
||||
adapter.refreshRegisteredAdapters()
|
||||
|
@ -330,6 +330,7 @@ class LibraryFragment : BaseRxFragment<LibraryPresenter>(), ActionMode.Callback
|
|||
}
|
||||
|
||||
} catch (e: IOException) {
|
||||
ToastUtil.showShort(context, R.string.notification_manga_update_failed)
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue