1
0
Fork 0
mirror of https://github.com/anyproto/anytype-kotlin.git synced 2025-06-08 05:47:05 +09:00

DROID-2156 Sharing Extensions | Enhancement | Basic media sharing (from outside into Anytype) (#823)

Co-authored-by: Konstantin Ivanov <54908981+konstantiniiv@users.noreply.github.com>
Co-authored-by: konstantiniiv <ki@anytype.io>
This commit is contained in:
Evgenii Kozlov 2024-02-05 18:31:04 +01:00 committed by GitHub
parent ad58c376d3
commit 2cc1561b73
Signed by: github
GPG key ID: B5690EEEBB952194
27 changed files with 549 additions and 45 deletions

View file

@ -7,6 +7,7 @@ dependencies {
implementation project(':data')
implementation project(':domain')
implementation project(':localization')
implementation libs.kotlin
implementation libs.coroutinesAndroid

View file

@ -0,0 +1,85 @@
package com.anytypeio.anytype.device
import android.content.Context
import android.net.Uri
import android.provider.OpenableColumns
import com.anytypeio.anytype.domain.base.AppCoroutineDispatchers
import com.anytypeio.anytype.domain.device.FileSharer
import com.anytypeio.anytype.localization.R
import java.io.File
import java.io.FileOutputStream
import javax.inject.Inject
import kotlinx.coroutines.withContext
import timber.log.Timber
class SharedFileUploader @Inject constructor(
private val context: Context,
private val dispatchers: AppCoroutineDispatchers
) : FileSharer {
override suspend fun getPath(uri: String): String = withContext(dispatchers.io) {
if (BuildConfig.DEBUG) Timber.d("Getting path for: $uri")
val parsed = Uri.parse(uri)
checkNotNull(parsed)
parsePathFromUri(parsed)
}
override suspend fun clear() {
TODO("Not yet implemented")
}
private fun parsePathFromUri(extra: Uri) : String {
val name = if (extra.scheme == CONTENT_URI_SCHEME) {
context.contentResolver.query(
extra,
null,
null,
null,
null
).use { cursor ->
if (cursor != null && cursor.moveToFirst()) {
val idx = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)
if (idx != -1) {
cursor.getString(idx)
} else {
context.resources.getString(R.string.untitled)
}
} else {
context.resources.getString(R.string.untitled)
}
}
} else {
val rawPath = extra.path
if (rawPath != null) {
rawPath.substring(rawPath.lastIndexOf("/"))
} else {
""
}
}
val inputStream = context.contentResolver.openInputStream(extra)
val cacheDir = context.getExternalFilesDir(null)
if (cacheDir != null && !cacheDir.exists()) {
cacheDir.mkdirs()
}
var path = ""
inputStream?.use { input ->
val newFile = File(cacheDir?.path + "/" + name);
FileOutputStream(newFile).use { output ->
val buffer = ByteArray(1024)
var read: Int = input.read(buffer)
while (read != -1) {
output.write(buffer, 0, read)
read = input.read(buffer)
}
}
path = newFile.path
}
return path
}
companion object {
const val CONTENT_URI_SCHEME = "content"
}
}