|
@@ -1,15 +1,27 @@
|
|
|
package com.kdanmobile.reader.thumb
|
|
|
|
|
|
-import android.os.Environment
|
|
|
-import java.io.File
|
|
|
+import java.io.*
|
|
|
import kotlin.collections.ArrayList
|
|
|
|
|
|
object FileUtil {
|
|
|
- val myFolder = "KdanPDFReader"
|
|
|
- val EXTENSION_SEPARATOR = '.'
|
|
|
- private val UNIX_SEPARATOR = '/'
|
|
|
- private val NOT_FOUND = -1
|
|
|
- private val WINDOWS_SEPARATOR = '\\'
|
|
|
+ private var readerFolder: File? = null
|
|
|
+ private const val EXTENSION_SEPARATOR = '.'
|
|
|
+ private const val UNIX_SEPARATOR = '/'
|
|
|
+ private const val NOT_FOUND = -1
|
|
|
+ private const val WINDOWS_SEPARATOR = '\\'
|
|
|
+
|
|
|
+ @Synchronized
|
|
|
+ fun init(folder: File) {
|
|
|
+ if (!folder.exists() || !folder.isDirectory) {
|
|
|
+ folder.mkdirs()
|
|
|
+ }
|
|
|
+ readerFolder = folder
|
|
|
+ }
|
|
|
+
|
|
|
+ @Synchronized
|
|
|
+ fun getKdanPDFReaderFolder(): File? {
|
|
|
+ return readerFolder
|
|
|
+ }
|
|
|
|
|
|
fun generateSelectedPagesString(selectPage: ArrayList<Int>): String {
|
|
|
val pages = StringBuffer()
|
|
@@ -33,27 +45,54 @@ object FileUtil {
|
|
|
return file
|
|
|
}
|
|
|
|
|
|
- @Synchronized
|
|
|
- fun getMyFile(): File? {
|
|
|
- var myFile: File? = null
|
|
|
- if (myFile == null) {
|
|
|
- myFile = File(getSdCardFile(), myFolder)
|
|
|
+ private fun generateUniqueFile(fileName: String): File {
|
|
|
+ val name = removeExtension(fileName)
|
|
|
+ val extension = getExtension(fileName)
|
|
|
+ var file = File(readerFolder, "$name.$extension")
|
|
|
+ var index = 2
|
|
|
+ while (file.exists()) {
|
|
|
+ file = File(readerFolder, "$name($index).$extension")
|
|
|
+ index++
|
|
|
}
|
|
|
- if (myFile.exists()) {
|
|
|
- myFile.mkdirs()
|
|
|
+ return file
|
|
|
+ }
|
|
|
+
|
|
|
+ enum class CopyMode {
|
|
|
+ OVERWRITE, DO_NOTHING_IF_FILE_EXIST, DO_NOTHING, CREATE_NEW_FILE
|
|
|
+ }
|
|
|
+
|
|
|
+ fun copyFileToKdanPdfReaderFolder(srcFilePath: String, mode: CopyMode = CopyMode.DO_NOTHING_IF_FILE_EXIST): String {
|
|
|
+ if (mode == CopyMode.DO_NOTHING) return srcFilePath
|
|
|
+ if (null == readerFolder) return srcFilePath
|
|
|
+ if (srcFilePath.startsWith(readerFolder!!.absolutePath)) return srcFilePath
|
|
|
+
|
|
|
+ val src = File(srcFilePath)
|
|
|
+ val srcFilename = src.name
|
|
|
+ var dstFilePath = File(readerFolder, srcFilename)
|
|
|
+ if (dstFilePath.exists()) {
|
|
|
+ when (mode) {
|
|
|
+ CopyMode.DO_NOTHING_IF_FILE_EXIST -> return srcFilePath
|
|
|
+ CopyMode.CREATE_NEW_FILE -> dstFilePath = generateUniqueFile(srcFilename)
|
|
|
+ else -> {}
|
|
|
+ }
|
|
|
}
|
|
|
- return myFile
|
|
|
+ try {
|
|
|
+ copyFile(FileInputStream(srcFilePath), FileOutputStream(dstFilePath))
|
|
|
+ } catch (e: Exception) {}
|
|
|
+ return dstFilePath.absolutePath
|
|
|
}
|
|
|
|
|
|
- private fun getSdCardFile(): File? {
|
|
|
- var sdcardFile: File? = null
|
|
|
- if (sdcardFile == null) {
|
|
|
- sdcardFile = Environment.getExternalStorageDirectory()
|
|
|
+ private fun copyFile(inputStream: InputStream, outputStream: OutputStream) {
|
|
|
+ val buffer = ByteArray(1024)
|
|
|
+ var read: Int
|
|
|
+ while (true) {
|
|
|
+ read = inputStream.read(buffer)
|
|
|
+ if (read == -1) break
|
|
|
+ outputStream.write(buffer, 0, read)
|
|
|
}
|
|
|
- return sdcardFile
|
|
|
}
|
|
|
|
|
|
- fun getExtension(filename: String?): String? {
|
|
|
+ private fun getExtension(filename: String?): String? {
|
|
|
if (filename == null) {
|
|
|
return null
|
|
|
}
|
|
@@ -65,7 +104,7 @@ object FileUtil {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- fun indexOfExtension(filename: String?): Int {
|
|
|
+ private fun indexOfExtension(filename: String?): Int {
|
|
|
if (filename == null) {
|
|
|
return NOT_FOUND
|
|
|
}
|
|
@@ -74,7 +113,7 @@ object FileUtil {
|
|
|
return if (lastSeparator > extensionPos) NOT_FOUND else extensionPos
|
|
|
}
|
|
|
|
|
|
- fun indexOfLastSeparator(filename: String?): Int {
|
|
|
+ private fun indexOfLastSeparator(filename: String?): Int {
|
|
|
if (filename == null) {
|
|
|
return NOT_FOUND
|
|
|
}
|
|
@@ -83,7 +122,7 @@ object FileUtil {
|
|
|
return Math.max(lastUnixPos, lastWindowsPos)
|
|
|
}
|
|
|
|
|
|
- private fun removeExtension(filename: String?): String? {
|
|
|
+ private fun removeExtension(filename: String?): String? {
|
|
|
if (filename == null) {
|
|
|
return null
|
|
|
}
|