diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/cache/CoverCache.kt b/app/src/main/java/eu/kanade/tachiyomi/data/cache/CoverCache.kt index 3b1f385f82..fe91ca5fd0 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/cache/CoverCache.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/cache/CoverCache.kt @@ -12,6 +12,7 @@ import com.bumptech.glide.signature.StringSignature import eu.kanade.tachiyomi.util.DiskUtils import java.io.File import java.io.IOException +import java.io.InputStream /** * 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 sourceFile the source file of the cover image. - * @throws IOException exception returned + * @throws IOException if there's any error. */ @Throws(IOException::class) fun copyToLocalCache(thumbnailUrl: String, sourceFile: File) { // Get destination file. - val destFile = File(cacheDir, DiskUtils.hashKeyForDisk(thumbnailUrl)) + val destFile = getCoverFromCache(thumbnailUrl) 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. diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/io/IOHandler.kt b/app/src/main/java/eu/kanade/tachiyomi/data/io/IOHandler.kt deleted file mode 100644 index 6eec2a078d..0000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/data/io/IOHandler.kt +++ /dev/null @@ -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() - } - -} \ No newline at end of file diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/backup/BackupFragment.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/backup/BackupFragment.kt index 15f4b0c04e..fd57965c26 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/backup/BackupFragment.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/backup/BackupFragment.kt @@ -97,8 +97,9 @@ class BackupFragment : BaseRxFragment() { .progress(true, 0) .show() - val stream = context.contentResolver.openInputStream(data.data) - presenter.restoreBackup(stream) + context.contentResolver.openInputStream(data.data).use { + presenter.restoreBackup(it) + } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryFragment.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryFragment.kt index 4708ca9ed5..a1ea3c26ee 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryFragment.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryFragment.kt @@ -13,7 +13,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.downloadMediaAndReturnPath import eu.kanade.tachiyomi.data.library.LibraryUpdateService import eu.kanade.tachiyomi.event.LibraryMangasEvent 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 nucleus.factory.RequiresPresenter import org.greenrobot.eventbus.EventBus -import java.io.File -import java.io.FileInputStream import java.io.IOException /** @@ -376,21 +373,14 @@ class LibraryFragment : BaseRxFragment(), ActionMode.Callback 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 = downloadMediaAndReturnPath(inputStream as FileInputStream, context) - - // Get file from filepath - val picture = File(result) - - // Update cover to selected file, show error if something went wrong - if (presenter.editCoverWithLocalFile(picture, manga)) { - adapter.refreshRegisteredAdapters() - } else { - context.toast(R.string.notification_manga_update_failed) + context.contentResolver.openInputStream(data.data).use { + // Update cover to selected file, show error if something went wrong + if (presenter.editCoverWithStream(it, manga)) { + adapter.refreshRegisteredAdapters() + } else { + context.toast(R.string.notification_manga_update_failed) + } } - } catch (e: IOException) { context.toast(R.string.notification_manga_update_failed) e.printStackTrace() diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt index f64ac12ef2..e3bb2254b0 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt @@ -17,8 +17,8 @@ import org.greenrobot.eventbus.EventBus import rx.Observable import rx.android.schedulers.AndroidSchedulers import rx.subjects.BehaviorSubject -import java.io.File import java.io.IOException +import java.io.InputStream import java.util.* import javax.inject.Inject @@ -273,17 +273,14 @@ class LibraryPresenter : BasePresenter() { /** * Update cover with local file. * - * @param file the new cover. + * @param inputStream the new cover. * @param manga the manga edited. * @return true if the cover is updated, false otherwise */ @Throws(IOException::class) - fun editCoverWithLocalFile(file: File, manga: Manga): Boolean { - if (!manga.initialized) - return false - - if (manga.favorite) { - coverCache.copyToLocalCache(manga.thumbnail_url, file) + fun editCoverWithStream(inputStream: InputStream, manga: Manga): Boolean { + if (manga.thumbnail_url != null && manga.favorite) { + coverCache.copyToLocalCache(manga.thumbnail_url, inputStream) return true } return false