|
|
@ -6,12 +6,19 @@ import io.legado.app.App |
|
|
|
import io.legado.app.data.entities.Book |
|
|
|
import io.legado.app.data.entities.Book |
|
|
|
import io.legado.app.help.BookHelp |
|
|
|
import io.legado.app.help.BookHelp |
|
|
|
import io.legado.app.utils.FileUtils |
|
|
|
import io.legado.app.utils.FileUtils |
|
|
|
|
|
|
|
import io.legado.app.utils.isContentPath |
|
|
|
|
|
|
|
import java.io.File |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
object LocalBook { |
|
|
|
object LocalBook { |
|
|
|
|
|
|
|
|
|
|
|
fun importFile(doc: DocumentFile) { |
|
|
|
fun importFile(path: String) { |
|
|
|
doc.name?.let { fileName -> |
|
|
|
val fileName = if (path.isContentPath()) { |
|
|
|
|
|
|
|
val doc = DocumentFile.fromSingleUri(App.INSTANCE, Uri.parse(path)) |
|
|
|
|
|
|
|
doc?.name ?: "" |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
File(path).name |
|
|
|
|
|
|
|
} |
|
|
|
val str = fileName.substringBeforeLast(".") |
|
|
|
val str = fileName.substringBeforeLast(".") |
|
|
|
val authorIndex = str.indexOf("作者") |
|
|
|
val authorIndex = str.indexOf("作者") |
|
|
|
var name: String |
|
|
|
var name: String |
|
|
@ -30,14 +37,13 @@ object LocalBook { |
|
|
|
name = (name.substring(smhStart + 1, smhEnd)) |
|
|
|
name = (name.substring(smhStart + 1, smhEnd)) |
|
|
|
} |
|
|
|
} |
|
|
|
val book = Book( |
|
|
|
val book = Book( |
|
|
|
bookUrl = doc.uri.toString(), |
|
|
|
bookUrl = path, |
|
|
|
name = name, |
|
|
|
name = name, |
|
|
|
author = author, |
|
|
|
author = author, |
|
|
|
originName = fileName |
|
|
|
originName = fileName |
|
|
|
) |
|
|
|
) |
|
|
|
App.db.bookDao().insert(book) |
|
|
|
App.db.bookDao().insert(book) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun deleteBook(book: Book, deleteOriginal: Boolean) { |
|
|
|
fun deleteBook(book: Book, deleteOriginal: Boolean) { |
|
|
|
kotlin.runCatching { |
|
|
|
kotlin.runCatching { |
|
|
@ -47,8 +53,12 @@ object LocalBook { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (deleteOriginal) { |
|
|
|
if (deleteOriginal) { |
|
|
|
|
|
|
|
if (book.bookUrl.isContentPath()) { |
|
|
|
val uri = Uri.parse(book.bookUrl) |
|
|
|
val uri = Uri.parse(book.bookUrl) |
|
|
|
DocumentFile.fromSingleUri(App.INSTANCE, uri)?.delete() |
|
|
|
DocumentFile.fromSingleUri(App.INSTANCE, uri)?.delete() |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
FileUtils.deleteFile(book.bookUrl) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|