mirror of
https://github.com/TeamNewPipe/NewPipe
synced 2025-09-01 18:37:56 +00:00
Merge pull request #12569 from Isira-Seneviratne/Fix-import
Fix database import
This commit is contained in:
@@ -57,9 +57,7 @@ public class BackupRestoreSettingsFragment extends BasePreferenceFragment {
|
||||
@Override
|
||||
public void onCreatePreferences(@Nullable final Bundle savedInstanceState,
|
||||
@Nullable final String rootKey) {
|
||||
final var dbDir = requireContext().getDatabasePath(BackupFileLocator.FILE_NAME_DB).toPath()
|
||||
.getParent();
|
||||
manager = new ImportExportManager(new BackupFileLocator(dbDir));
|
||||
manager = new ImportExportManager(new BackupFileLocator(requireContext()));
|
||||
|
||||
importExportDataPathKey = getString(R.string.import_export_data_path);
|
||||
|
||||
|
@@ -1,12 +1,13 @@
|
||||
package org.schabi.newpipe.settings.export
|
||||
|
||||
import android.content.Context
|
||||
import java.nio.file.Path
|
||||
import kotlin.io.path.div
|
||||
|
||||
/**
|
||||
* Locates specific files of NewPipe based on the home directory of the app.
|
||||
*/
|
||||
class BackupFileLocator(homeDir: Path) {
|
||||
class BackupFileLocator(context: Context) {
|
||||
companion object {
|
||||
const val FILE_NAME_DB = "newpipe.db"
|
||||
@Deprecated(
|
||||
@@ -17,9 +18,8 @@ class BackupFileLocator(homeDir: Path) {
|
||||
const val FILE_NAME_JSON_PREFS = "preferences.json"
|
||||
}
|
||||
|
||||
val dbDir = homeDir / "databases"
|
||||
val db = homeDir / FILE_NAME_DB
|
||||
val dbJournal = homeDir / "$FILE_NAME_DB-journal"
|
||||
val dbShm = dbDir / "$FILE_NAME_DB-shm"
|
||||
val dbWal = dbDir / "$FILE_NAME_DB-wal"
|
||||
val db: Path = context.getDatabasePath(FILE_NAME_DB).toPath()
|
||||
val dbJournal: Path = db.resolveSibling("$FILE_NAME_DB-journal")
|
||||
val dbShm: Path = db.resolveSibling("$FILE_NAME_DB-shm")
|
||||
val dbWal: Path = db.resolveSibling("$FILE_NAME_DB-wal")
|
||||
}
|
||||
|
@@ -12,7 +12,7 @@ import java.io.FileNotFoundException
|
||||
import java.io.IOException
|
||||
import java.io.ObjectOutputStream
|
||||
import java.util.zip.ZipOutputStream
|
||||
import kotlin.io.path.createDirectories
|
||||
import kotlin.io.path.createParentDirectories
|
||||
import kotlin.io.path.deleteIfExists
|
||||
|
||||
class ImportExportManager(private val fileLocator: BackupFileLocator) {
|
||||
@@ -63,7 +63,7 @@ class ImportExportManager(private val fileLocator: BackupFileLocator) {
|
||||
*/
|
||||
@Throws(IOException::class)
|
||||
fun ensureDbDirectoryExists() {
|
||||
fileLocator.dbDir.createDirectories()
|
||||
fileLocator.db.createParentDirectories()
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -25,11 +25,12 @@ import org.schabi.newpipe.streams.io.StoredFileHelper
|
||||
import us.shandian.giga.io.FileStream
|
||||
import java.io.File
|
||||
import java.io.ObjectInputStream
|
||||
import java.nio.file.Paths
|
||||
import java.util.zip.ZipFile
|
||||
import kotlin.io.path.Path
|
||||
import kotlin.io.path.createTempDirectory
|
||||
import kotlin.io.path.createTempFile
|
||||
import kotlin.io.path.deleteIfExists
|
||||
import kotlin.io.path.div
|
||||
import kotlin.io.path.exists
|
||||
import kotlin.io.path.fileSize
|
||||
import kotlin.io.path.inputStream
|
||||
@@ -52,7 +53,7 @@ class ImportExportManagerTest {
|
||||
|
||||
@Test
|
||||
fun `The settings must be exported successfully in the correct format`() {
|
||||
val db = Path(classloader.getResource("settings/newpipe.db")!!.file)
|
||||
val db = Paths.get(classloader.getResource("settings/newpipe.db")!!.toURI())
|
||||
`when`(fileLocator.db).thenReturn(db)
|
||||
|
||||
val expectedPreferences = mapOf("such pref" to "much wow")
|
||||
@@ -87,21 +88,21 @@ class ImportExportManagerTest {
|
||||
|
||||
@Test
|
||||
fun `Ensuring db directory existence must work`() {
|
||||
val dir = createTempDirectory("newpipe_")
|
||||
Assume.assumeTrue(dir.deleteIfExists())
|
||||
`when`(fileLocator.dbDir).thenReturn(dir)
|
||||
val path = createTempDirectory("newpipe_") / BackupFileLocator.FILE_NAME_DB
|
||||
Assume.assumeTrue(path.parent.deleteIfExists())
|
||||
`when`(fileLocator.db).thenReturn(path)
|
||||
|
||||
ImportExportManager(fileLocator).ensureDbDirectoryExists()
|
||||
assertTrue(dir.exists())
|
||||
assertTrue(path.parent.exists())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Ensuring db directory existence must work when the directory already exists`() {
|
||||
val dir = createTempDirectory("newpipe_")
|
||||
`when`(fileLocator.dbDir).thenReturn(dir)
|
||||
val path = createTempDirectory("newpipe_") / BackupFileLocator.FILE_NAME_DB
|
||||
`when`(fileLocator.db).thenReturn(path)
|
||||
|
||||
ImportExportManager(fileLocator).ensureDbDirectoryExists()
|
||||
assertTrue(dir.exists())
|
||||
assertTrue(path.parent.exists())
|
||||
}
|
||||
|
||||
@Test
|
||||
|
Reference in New Issue
Block a user