Remove unneeded class

This commit is contained in:
len 2016-03-30 17:19:02 +02:00
parent e04596c668
commit 723c0e99a5
5 changed files with 32 additions and 85 deletions

View File

@ -12,6 +12,7 @@ import com.bumptech.glide.signature.StringSignature
import eu.kanade.tachiyomi.util.DiskUtils import eu.kanade.tachiyomi.util.DiskUtils
import java.io.File import java.io.File
import java.io.IOException import java.io.IOException
import java.io.InputStream
/** /**
* Class used to create cover cache. * Class used to create cover cache.
@ -64,19 +65,32 @@ class CoverCache(private val context: Context) {
} }
/** /**
* Copy the cover from Glide's cache to this cache. * Copy the given file to this cache.
* @param thumbnailUrl url of thumbnail. * @param thumbnailUrl url of thumbnail.
* @param sourceFile the source file of the cover image. * @param sourceFile the source file of the cover image.
* @throws IOException exception returned * @throws IOException if there's any error.
*/ */
@Throws(IOException::class) @Throws(IOException::class)
fun copyToLocalCache(thumbnailUrl: String, sourceFile: File) { fun copyToLocalCache(thumbnailUrl: String, sourceFile: File) {
// Get destination file. // Get destination file.
val destFile = File(cacheDir, DiskUtils.hashKeyForDisk(thumbnailUrl)) val destFile = getCoverFromCache(thumbnailUrl)
sourceFile.copyTo(destFile, overwrite = true) sourceFile.copyTo(destFile, overwrite = true)
} }
/**
* Copy the given stream to this cache.
* @param thumbnailUrl url of the thumbnail.
* @param inputStream the stream to copy.
* @throws IOException if there's any error.
*/
@Throws(IOException::class)
fun copyToLocalCache(thumbnailUrl: String, inputStream: InputStream) {
// Get destination file.
val destFile = getCoverFromCache(thumbnailUrl)
destFile.outputStream().use { inputStream.copyTo(it) }
}
/** /**
* Returns the cover from cache. * Returns the cover from cache.

View File

@ -1,55 +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
/**
* 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 output: FileOutputStream? = null
try {
// Get temp file name.
val tempFilename = getTempFilename(context)
output = FileOutputStream(tempFilename)
// Copy input stream to temp location.
input.copyTo(output)
return tempFilename
} finally {
// Close streams.
input.close()
output?.close()
}
}

View File

@ -97,8 +97,9 @@ class BackupFragment : BaseRxFragment<BackupPresenter>() {
.progress(true, 0) .progress(true, 0)
.show() .show()
val stream = context.contentResolver.openInputStream(data.data) context.contentResolver.openInputStream(data.data).use {
presenter.restoreBackup(stream) presenter.restoreBackup(it)
}
} }
} }

View File

@ -13,7 +13,6 @@ import eu.davidea.flexibleadapter.FlexibleAdapter
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.models.Category import eu.kanade.tachiyomi.data.database.models.Category
import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.io.downloadMediaAndReturnPath
import eu.kanade.tachiyomi.data.library.LibraryUpdateService import eu.kanade.tachiyomi.data.library.LibraryUpdateService
import eu.kanade.tachiyomi.event.LibraryMangasEvent import eu.kanade.tachiyomi.event.LibraryMangasEvent
import eu.kanade.tachiyomi.ui.base.fragment.BaseRxFragment import eu.kanade.tachiyomi.ui.base.fragment.BaseRxFragment
@ -25,8 +24,6 @@ import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.fragment_library.* import kotlinx.android.synthetic.main.fragment_library.*
import nucleus.factory.RequiresPresenter import nucleus.factory.RequiresPresenter
import org.greenrobot.eventbus.EventBus import org.greenrobot.eventbus.EventBus
import java.io.File
import java.io.FileInputStream
import java.io.IOException import java.io.IOException
/** /**
@ -376,21 +373,14 @@ class LibraryFragment : BaseRxFragment<LibraryPresenter>(), ActionMode.Callback
try { try {
// Get the file's input stream from the incoming Intent // Get the file's input stream from the incoming Intent
val inputStream = context.contentResolver.openInputStream(data.data) context.contentResolver.openInputStream(data.data).use {
// Convert to absolute path to prevent FileNotFoundException
val result = downloadMediaAndReturnPath(inputStream as FileInputStream, context)
// Get file from filepath
val picture = File(result)
// Update cover to selected file, show error if something went wrong // Update cover to selected file, show error if something went wrong
if (presenter.editCoverWithLocalFile(picture, manga)) { if (presenter.editCoverWithStream(it, manga)) {
adapter.refreshRegisteredAdapters() adapter.refreshRegisteredAdapters()
} else { } else {
context.toast(R.string.notification_manga_update_failed) context.toast(R.string.notification_manga_update_failed)
} }
}
} catch (e: IOException) { } catch (e: IOException) {
context.toast(R.string.notification_manga_update_failed) context.toast(R.string.notification_manga_update_failed)
e.printStackTrace() e.printStackTrace()

View File

@ -17,8 +17,8 @@ import org.greenrobot.eventbus.EventBus
import rx.Observable import rx.Observable
import rx.android.schedulers.AndroidSchedulers import rx.android.schedulers.AndroidSchedulers
import rx.subjects.BehaviorSubject import rx.subjects.BehaviorSubject
import java.io.File
import java.io.IOException import java.io.IOException
import java.io.InputStream
import java.util.* import java.util.*
import javax.inject.Inject import javax.inject.Inject
@ -273,17 +273,14 @@ class LibraryPresenter : BasePresenter<LibraryFragment>() {
/** /**
* Update cover with local file. * Update cover with local file.
* *
* @param file the new cover. * @param inputStream the new cover.
* @param manga the manga edited. * @param manga the manga edited.
* @return true if the cover is updated, false otherwise * @return true if the cover is updated, false otherwise
*/ */
@Throws(IOException::class) @Throws(IOException::class)
fun editCoverWithLocalFile(file: File, manga: Manga): Boolean { fun editCoverWithStream(inputStream: InputStream, manga: Manga): Boolean {
if (!manga.initialized) if (manga.thumbnail_url != null && manga.favorite) {
return false coverCache.copyToLocalCache(manga.thumbnail_url, inputStream)
if (manga.favorite) {
coverCache.copyToLocalCache(manga.thumbnail_url, file)
return true return true
} }
return false return false