Skip to content

Commit 10c5a5d

Browse files
authored
Merge pull request #12569 from Isira-Seneviratne/Fix-import
Fix database import
2 parents deb5425 + cf4b5e1 commit 10c5a5d

4 files changed

Lines changed: 19 additions & 20 deletions

File tree

app/src/main/java/org/schabi/newpipe/settings/BackupRestoreSettingsFragment.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ public class BackupRestoreSettingsFragment extends BasePreferenceFragment {
5757
@Override
5858
public void onCreatePreferences(@Nullable final Bundle savedInstanceState,
5959
@Nullable final String rootKey) {
60-
final var dbDir = requireContext().getDatabasePath(BackupFileLocator.FILE_NAME_DB).toPath()
61-
.getParent();
62-
manager = new ImportExportManager(new BackupFileLocator(dbDir));
60+
manager = new ImportExportManager(new BackupFileLocator(requireContext()));
6361

6462
importExportDataPathKey = getString(R.string.import_export_data_path);
6563

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package org.schabi.newpipe.settings.export
22

3+
import android.content.Context
34
import java.nio.file.Path
45
import kotlin.io.path.div
56

67
/**
78
* Locates specific files of NewPipe based on the home directory of the app.
89
*/
9-
class BackupFileLocator(homeDir: Path) {
10+
class BackupFileLocator(context: Context) {
1011
companion object {
1112
const val FILE_NAME_DB = "newpipe.db"
1213
@Deprecated(
@@ -17,9 +18,8 @@ class BackupFileLocator(homeDir: Path) {
1718
const val FILE_NAME_JSON_PREFS = "preferences.json"
1819
}
1920

20-
val dbDir = homeDir / "databases"
21-
val db = homeDir / FILE_NAME_DB
22-
val dbJournal = homeDir / "$FILE_NAME_DB-journal"
23-
val dbShm = dbDir / "$FILE_NAME_DB-shm"
24-
val dbWal = dbDir / "$FILE_NAME_DB-wal"
21+
val db: Path = context.getDatabasePath(FILE_NAME_DB).toPath()
22+
val dbJournal: Path = db.resolveSibling("$FILE_NAME_DB-journal")
23+
val dbShm: Path = db.resolveSibling("$FILE_NAME_DB-shm")
24+
val dbWal: Path = db.resolveSibling("$FILE_NAME_DB-wal")
2525
}

app/src/main/java/org/schabi/newpipe/settings/export/ImportExportManager.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import java.io.FileNotFoundException
1212
import java.io.IOException
1313
import java.io.ObjectOutputStream
1414
import java.util.zip.ZipOutputStream
15-
import kotlin.io.path.createDirectories
15+
import kotlin.io.path.createParentDirectories
1616
import kotlin.io.path.deleteIfExists
1717

1818
class ImportExportManager(private val fileLocator: BackupFileLocator) {
@@ -63,7 +63,7 @@ class ImportExportManager(private val fileLocator: BackupFileLocator) {
6363
*/
6464
@Throws(IOException::class)
6565
fun ensureDbDirectoryExists() {
66-
fileLocator.dbDir.createDirectories()
66+
fileLocator.db.createParentDirectories()
6767
}
6868

6969
/**

app/src/test/java/org/schabi/newpipe/settings/ImportExportManagerTest.kt

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ import org.schabi.newpipe.streams.io.StoredFileHelper
2525
import us.shandian.giga.io.FileStream
2626
import java.io.File
2727
import java.io.ObjectInputStream
28+
import java.nio.file.Paths
2829
import java.util.zip.ZipFile
29-
import kotlin.io.path.Path
3030
import kotlin.io.path.createTempDirectory
3131
import kotlin.io.path.createTempFile
3232
import kotlin.io.path.deleteIfExists
33+
import kotlin.io.path.div
3334
import kotlin.io.path.exists
3435
import kotlin.io.path.fileSize
3536
import kotlin.io.path.inputStream
@@ -52,7 +53,7 @@ class ImportExportManagerTest {
5253

5354
@Test
5455
fun `The settings must be exported successfully in the correct format`() {
55-
val db = Path(classloader.getResource("settings/newpipe.db")!!.file)
56+
val db = Paths.get(classloader.getResource("settings/newpipe.db")!!.toURI())
5657
`when`(fileLocator.db).thenReturn(db)
5758

5859
val expectedPreferences = mapOf("such pref" to "much wow")
@@ -87,21 +88,21 @@ class ImportExportManagerTest {
8788

8889
@Test
8990
fun `Ensuring db directory existence must work`() {
90-
val dir = createTempDirectory("newpipe_")
91-
Assume.assumeTrue(dir.deleteIfExists())
92-
`when`(fileLocator.dbDir).thenReturn(dir)
91+
val path = createTempDirectory("newpipe_") / BackupFileLocator.FILE_NAME_DB
92+
Assume.assumeTrue(path.parent.deleteIfExists())
93+
`when`(fileLocator.db).thenReturn(path)
9394

9495
ImportExportManager(fileLocator).ensureDbDirectoryExists()
95-
assertTrue(dir.exists())
96+
assertTrue(path.parent.exists())
9697
}
9798

9899
@Test
99100
fun `Ensuring db directory existence must work when the directory already exists`() {
100-
val dir = createTempDirectory("newpipe_")
101-
`when`(fileLocator.dbDir).thenReturn(dir)
101+
val path = createTempDirectory("newpipe_") / BackupFileLocator.FILE_NAME_DB
102+
`when`(fileLocator.db).thenReturn(path)
102103

103104
ImportExportManager(fileLocator).ensureDbDirectoryExists()
104-
assertTrue(dir.exists())
105+
assertTrue(path.parent.exists())
105106
}
106107

107108
@Test

0 commit comments

Comments
 (0)