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