/
StorageResolverHelper.kt
267 lines (236 loc) · 8.89 KB
/
StorageResolverHelper.kt
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
@file:JvmName("StorageResolverHelper")
package com.tonyodev.fetch2core
import android.content.ContentResolver
import android.content.ContentValues
import android.content.Context
import android.net.Uri
import android.os.Build
import android.os.ParcelFileDescriptor
import android.provider.DocumentsContract
import java.io.*
fun getOutputResourceWrapper(parcelFileDescriptor: ParcelFileDescriptor): OutputResourceWrapper {
return getOutputResourceWrapper(parcelFileDescriptor.fileDescriptor, parcelFileDescriptor)
}
@JvmOverloads
fun getOutputResourceWrapper(fileDescriptor: FileDescriptor,
parcelFileDescriptor: ParcelFileDescriptor? = null): OutputResourceWrapper {
return getOutputResourceWrapper(FileOutputStream(fileDescriptor), parcelFileDescriptor)
}
@JvmOverloads
fun getOutputResourceWrapper(fileOutputStream: FileOutputStream,
parcelFileDescriptor: ParcelFileDescriptor? = null): OutputResourceWrapper {
return object : OutputResourceWrapper() {
private val fileOutputStream = fileOutputStream
private val parcelFileDescriptor = parcelFileDescriptor
init {
this.fileOutputStream.channel.position(0)
}
override fun write(byteArray: ByteArray, offSet: Int, length: Int) {
this.fileOutputStream.write(byteArray, offSet, length)
}
override fun setWriteOffset(offset: Long) {
this.fileOutputStream.channel.position(offset)
}
override fun flush() {
this.fileOutputStream.flush()
}
override fun close() {
this.fileOutputStream.close()
}
}
}
fun getOutputResourceWrapper(filePath: String, contentResolver: ContentResolver): OutputResourceWrapper {
return if (isUriPath(filePath)) {
getOutputResourceWrapper(Uri.parse(filePath), contentResolver)
} else {
getOutputResourceWrapper(File(filePath))
}
}
fun getOutputResourceWrapper(fileUri: Uri, contentResolver: ContentResolver): OutputResourceWrapper {
return when {
fileUri.scheme == "content" -> {
val parcelFileDescriptor = contentResolver.openFileDescriptor(fileUri, "w")
if (parcelFileDescriptor == null) {
throw FileNotFoundException("$fileUri $FILE_NOT_FOUND")
} else {
getOutputResourceWrapper(parcelFileDescriptor)
}
}
fileUri.scheme == "file" -> {
val file = File(fileUri.path)
if (file.exists() && file.canWrite()) {
getOutputResourceWrapper(file)
} else {
val parcelFileDescriptor = contentResolver.openFileDescriptor(fileUri, "w")
if (parcelFileDescriptor == null) {
throw FileNotFoundException("$fileUri $FILE_NOT_FOUND")
} else {
getOutputResourceWrapper(parcelFileDescriptor)
}
}
}
else -> {
throw FileNotFoundException("$fileUri $FILE_NOT_FOUND")
}
}
}
fun getOutputResourceWrapper(filePath: String): OutputResourceWrapper {
val file = File(filePath)
return if (file.exists()) getOutputResourceWrapper(file) else throw FileNotFoundException("$file $FILE_NOT_FOUND")
}
fun getOutputResourceWrapper(file: File): OutputResourceWrapper {
if (!file.exists()) {
throw FileNotFoundException("${file.canonicalPath} $FILE_NOT_FOUND")
}
return getOutputResourceWrapper(RandomAccessFile(file, "rw"))
}
fun getOutputResourceWrapper(randomAccessFile: RandomAccessFile): OutputResourceWrapper {
return object : OutputResourceWrapper() {
private val randomAccessFile = randomAccessFile
init {
this.randomAccessFile.seek(0)
}
override fun write(byteArray: ByteArray, offSet: Int, length: Int) {
this.randomAccessFile.write(byteArray, offSet, length)
}
override fun setWriteOffset(offset: Long) {
this.randomAccessFile.seek(offset)
}
override fun flush() {
}
override fun close() {
this.randomAccessFile.close()
}
}
}
fun deleteFile(filePath: String, context: Context): Boolean {
return if (isUriPath(filePath)) {
val uri = Uri.parse(filePath)
when {
uri.scheme == "file" -> {
val file = File(uri.path)
if (file.canWrite() && file.exists()) deleteFile(file) else false
}
uri.scheme == "content" -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
&& DocumentsContract.isDocumentUri(context, uri)) {
DocumentsContract.deleteDocument(context.contentResolver, uri)
} else {
context.contentResolver.delete(uri, null, null) > 0
}
}
else -> false
}
} else {
deleteFile(File(filePath))
}
}
fun renameFile(oldFile: String, newFile: String, context: Context): Boolean {
return if (isUriPath(oldFile)) {
val uri = Uri.parse(oldFile)
when {
uri.scheme == "file" -> {
val file = File(uri.path)
if (file.canWrite() && file.exists()) renameFile(file, File(newFile)) else {
val contentValue = ContentValues()
contentValue.put("uri", newFile)
context.contentResolver.update(uri, contentValue, null, null) > 0
}
}
uri.scheme == "content" -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
&& DocumentsContract.isDocumentUri(context, uri)) {
DocumentsContract.renameDocument(context.contentResolver, uri, newFile) != null
} else {
val contentValue = ContentValues()
contentValue.put("uri", newFile)
context.contentResolver.update(uri, contentValue, null, null) > 0
}
}
else -> false
}
} else {
renameFile(File(oldFile), File(newFile))
}
}
fun createFileAtPath(filePath: String, increment: Boolean, context: Context): String {
return if (isUriPath(filePath)) {
val uri = Uri.parse(filePath)
when {
uri.scheme == "file" -> {
createLocalFile(uri.path ?: filePath, increment)
}
uri.scheme == "content" -> {
val parcelFileDescriptor = context.contentResolver.openFileDescriptor(uri, "w")
if (parcelFileDescriptor == null) {
throw IOException(FNC)
} else {
filePath
}
}
else -> throw IOException(FNC)
}
} else {
createLocalFile(filePath, increment)
}
}
fun createLocalFile(filePath: String, increment: Boolean): String {
return if (!increment) {
createFile(File(filePath))
filePath
} else {
getIncrementedFileIfOriginalExists(filePath).absolutePath
}
}
fun allocateFile(filePath: String, contentLength: Long, context: Context) {
return if (isUriPath(filePath)) {
val uri = Uri.parse(filePath)
when {
uri.scheme == "file" -> {
allocateFile(File(uri.path ?: filePath), contentLength)
}
uri.scheme == "content" -> {
val parcelFileDescriptor = context.contentResolver.openFileDescriptor(uri, "w")
if (parcelFileDescriptor == null) {
throw IOException(FILE_ALLOCATION_ERROR)
} else {
allocateParcelFileDescriptor(parcelFileDescriptor, contentLength)
}
}
else -> throw IOException(FILE_ALLOCATION_ERROR)
}
} else {
allocateFile(File(filePath), contentLength)
}
}
fun allocateParcelFileDescriptor(parcelFileDescriptor: ParcelFileDescriptor, contentLength: Long) {
if (contentLength > 0) {
try {
val fileOutputStream = FileOutputStream(parcelFileDescriptor.fileDescriptor)
if (fileOutputStream.channel.size() == contentLength) {
return
}
fileOutputStream.channel.position(contentLength - 1.toLong())
fileOutputStream.write(1)
} catch (e: Exception) {
throw IOException(FILE_ALLOCATION_ERROR)
}
}
}
fun allocateFile(file: File, contentLength: Long) {
if (!file.exists()) {
createFile(file)
}
if (file.length() == contentLength) {
return
}
if (contentLength > 0) {
try {
val randomAccessFile = RandomAccessFile(file, "rw")
randomAccessFile.setLength(contentLength)
randomAccessFile.close()
} catch (e: Exception) {
throw IOException(FILE_ALLOCATION_ERROR)
}
}
}