2020package eu.opencloud.android.data.providers
2121
2222import android.content.Context
23+ import android.net.Uri
2324import android.os.Environment
25+ import timber.log.Timber
2426import java.io.File
2527
2628@Suppress(" UnusedPrivateProperty" )
@@ -30,4 +32,38 @@ class ScopedStorageProvider(
3032) : LocalStorageProvider(rootFolderName) {
3133
3234 override fun getPrimaryStorageDirectory (): File = Environment .getExternalStorageDirectory()
35+
36+ override fun getAccountDirectoryPath (accountName : String ): String {
37+ val sanitizedName = accountName.replace(" /" , " _" ).replace(" \\ " , " _" ).replace(" :" , " _" )
38+ val newPath = getRootFolderPath() + File .separator + sanitizedName
39+
40+ val oldEncodedName = Uri .encode(accountName, " @" )
41+ val oldPath = getRootFolderPath() + File .separator + oldEncodedName
42+
43+ if (oldPath != newPath) {
44+ val oldDir = File (oldPath)
45+ val newDir = File (newPath)
46+
47+ // If old encoded directory exists, but the new readable one doesn't, migrate it!
48+ if (oldDir.exists() && oldDir.isDirectory && ! newDir.exists()) {
49+ try {
50+ if (oldDir.renameTo(newDir)) {
51+ Timber .i(" Successfully migrated account directory from $oldEncodedName to readable name $sanitizedName " )
52+ return newPath
53+ } else {
54+ return oldPath // Fallback if rename fails
55+ }
56+ } catch (e: Exception ) {
57+ Timber .e(e, " Failed to migrate account directory to readable name" )
58+ return oldPath
59+ }
60+ } else if (oldDir.exists() && oldDir.isDirectory && newDir.exists()) {
61+ // If both exist, we should probably stick to the new one or the old one. Let's use old one to not lose files
62+ // that haven't been migrated yet.
63+ return oldPath
64+ }
65+ }
66+
67+ return newPath
68+ }
3369}
0 commit comments