-
Notifications
You must be signed in to change notification settings - Fork 19
/
UriHelperAndroid.kt
31 lines (25 loc) · 1.06 KB
/
UriHelperAndroid.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
package com.ustadmobile.core.uri
import android.annotation.SuppressLint
import android.content.Context
import com.ustadmobile.core.util.ext.getFileNameAndSize
import com.ustadmobile.door.DoorUri
import kotlinx.io.Source
import kotlinx.io.asSource
import kotlinx.io.buffered
import java.io.IOException
class UriHelperAndroid(private val appContext: Context): UriHelper{
override suspend fun getMimeType(uri: DoorUri): String? {
return appContext.contentResolver.getType(uri.uri)
}
override suspend fun getFileName(uri: DoorUri): String {
return appContext.contentResolver.getFileNameAndSize(uri.uri).first
}
override suspend fun getSize(uri: DoorUri): Long {
return appContext.contentResolver.getFileNameAndSize(uri.uri).second
}
@SuppressLint("Recycle") //The input stream is closed when the source is closed.
override suspend fun openSource(uri: DoorUri): Source {
return appContext.contentResolver.openInputStream(uri.uri)?.asSource()?.buffered()
?: throw IOException("Could not open uri: $uri")
}
}