-
-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathSaveAsActivity.kt
More file actions
125 lines (109 loc) · 4.91 KB
/
SaveAsActivity.kt
File metadata and controls
125 lines (109 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package org.fossify.filemanager.activities
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import org.fossify.commons.dialogs.FilePickerDialog
import org.fossify.commons.extensions.getDocumentFile
import org.fossify.commons.extensions.getDoesFilePathExist
import org.fossify.commons.extensions.getFileOutputStreamSync
import org.fossify.commons.extensions.getFilenameFromContentUri
import org.fossify.commons.extensions.getFilenameFromPath
import org.fossify.commons.extensions.getMimeType
import org.fossify.commons.extensions.needsStupidWritePermissions
import org.fossify.commons.extensions.rescanPaths
import org.fossify.commons.extensions.showErrorToast
import org.fossify.commons.extensions.toast
import org.fossify.commons.extensions.viewBinding
import org.fossify.commons.helpers.NavigationIcon
import org.fossify.commons.helpers.ensureBackgroundThread
import org.fossify.filemanager.R
import org.fossify.filemanager.databinding.ActivitySaveAsBinding
import org.fossify.filemanager.extensions.config
import java.io.File
class SaveAsActivity : SimpleActivity() {
private val binding by viewBinding(ActivitySaveAsBinding::inflate)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
tryInitFileManager()
}
private fun tryInitFileManager() {
handleStoragePermission { granted ->
if (granted) {
saveAsDialog()
} else {
toast(R.string.no_storage_permissions)
finish()
}
}
}
private fun saveAsDialog() {
if (intent.action == Intent.ACTION_SEND && intent.extras?.containsKey(Intent.EXTRA_STREAM) == true) {
FilePickerDialog(this, pickFile = false, showHidden = config.shouldShowHidden(), showFAB = true, showFavoritesButton = true) {
val destination = it
handleSAFDialog(destination) {
toast(R.string.saving)
ensureBackgroundThread {
try {
if (!getDoesFilePathExist(destination)) {
if (needsStupidWritePermissions(destination)) {
val document = getDocumentFile(destination)
document!!.createDirectory(destination.getFilenameFromPath())
} else {
File(destination).mkdirs()
}
}
val source = intent.getParcelableExtra<Uri>(Intent.EXTRA_STREAM)!!
val originalFilename = getFilenameFromContentUri(source)
?: source.toString().getFilenameFromPath()
val filename = sanitizeFilename(originalFilename)
val mimeType = contentResolver.getType(source)
?: intent.type?.takeIf { it != "*/*" }
?: filename.getMimeType()
val inputStream = contentResolver.openInputStream(source)
val destinationPath = getAvailablePath("$destination/$filename")
val outputStream = getFileOutputStreamSync(destinationPath, mimeType, null)!!
inputStream!!.copyTo(outputStream)
rescanPaths(arrayListOf(destinationPath))
toast(R.string.file_saved)
finish()
} catch (e: Exception) {
showErrorToast(e)
finish()
}
}
}
}
} else {
toast(R.string.unknown_error_occurred)
finish()
}
}
override fun onResume() {
super.onResume()
setupTopAppBar(binding.activitySaveAsAppbar, NavigationIcon.Arrow)
}
private fun sanitizeFilename(filename: String): String {
return filename.replace("[/\\\\<>:\"|?*\u0000-\u001F]".toRegex(), "_")
.takeIf { it.isNotBlank() } ?: "unnamed_file"
}
private fun getAvailablePath(destinationPath: String): String {
if (!getDoesFilePathExist(destinationPath)) {
return destinationPath
}
val file = File(destinationPath)
return findAvailableName(file)
}
private fun findAvailableName(file: File): String {
val parent = file.parent ?: return file.absolutePath
val name = file.nameWithoutExtension
val ext = if (file.extension.isNotEmpty()) ".${file.extension}" else ""
var index = 1
var newPath: String
do {
newPath = "$parent/${name}_$index$ext"
index++
} while (getDoesFilePathExist(newPath))
return newPath
}
}