部分优化
This commit is contained in:
Binary file not shown.
@@ -1,277 +0,0 @@
|
||||
@file:Suppress("UNCHECKED_CAST", "USELESS_CAST", "INAPPLICABLE_JVM_NAME", "UNUSED_ANONYMOUS_PARAMETER", "SENSELESS_COMPARISON", "NAME_SHADOWING", "UNNECESSARY_NOT_NULL_ASSERTION")
|
||||
package uts.sdk.modules.limeChooseFile
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.provider.OpenableColumns
|
||||
import io.dcloud.uniapp.*
|
||||
import io.dcloud.uniapp.extapi.*
|
||||
import io.dcloud.unicloud.*
|
||||
import io.dcloud.uts.*
|
||||
import io.dcloud.uts.Map
|
||||
import io.dcloud.uts.Set
|
||||
import io.dcloud.uts.UTSAndroid
|
||||
import java.io.File
|
||||
import java.io.FileOutputStream
|
||||
import kotlin.properties.Delegates
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Deferred
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.async
|
||||
typealias ChooseFileErrorCode = Number
|
||||
interface GeneralCallbackResult : IUniError {
|
||||
override var errCode: ChooseFileErrorCode
|
||||
}
|
||||
interface ChooseFile {
|
||||
var name: String
|
||||
var path: String
|
||||
var size: Number
|
||||
var time: Number
|
||||
var type: String
|
||||
}
|
||||
open class ChooseFileSuccessCallbackResult (
|
||||
@JsonNotNull
|
||||
open var tempFiles: UTSArray<ChooseFile>,
|
||||
@JsonNotNull
|
||||
open var errMsg: String,
|
||||
) : UTSObject(), IUTSSourceMap {
|
||||
override fun `__$getOriginalPosition`(): UTSSourceMapPosition? {
|
||||
return UTSSourceMapPosition("ChooseFileSuccessCallbackResult", "uni_modules/lime-choose-file/utssdk/interface.uts", 34, 13)
|
||||
}
|
||||
}
|
||||
typealias ChooseFileSuccessCallback = (result: ChooseFileSuccessCallbackResult) -> Unit
|
||||
typealias ChooseFileFailCallback = (res: GeneralCallbackResult) -> Unit
|
||||
typealias ChooseFileCompleteCallback = (res: GeneralCallbackResult) -> Unit
|
||||
open class ChooseFileOption (
|
||||
open var filename: String? = null,
|
||||
open var count: Number? = null,
|
||||
open var type: String? = null,
|
||||
open var extension: UTSArray<String>? = null,
|
||||
open var success: ChooseFileSuccessCallback? = null,
|
||||
open var fail: ChooseFileFailCallback? = null,
|
||||
open var complete: ChooseFileCompleteCallback? = null,
|
||||
) : UTSObject(), IUTSSourceMap {
|
||||
override fun `__$getOriginalPosition`(): UTSSourceMapPosition? {
|
||||
return UTSSourceMapPosition("ChooseFileOption", "uni_modules/lime-choose-file/utssdk/interface.uts", 45, 13)
|
||||
}
|
||||
}
|
||||
val UniErrorSubject = "chooseFile\""
|
||||
val UniErrors: Map<ChooseFileErrorCode, String> = Map(_uA(
|
||||
_uA(
|
||||
9010001,
|
||||
"chooseFile:ok"
|
||||
),
|
||||
_uA(
|
||||
9010002,
|
||||
"ChooseFile:failed"
|
||||
)
|
||||
))
|
||||
open class GeneralCallbackResultImpl : UniError, GeneralCallbackResult, IUTSSourceMap {
|
||||
override fun `__$getOriginalPosition`(): UTSSourceMapPosition? {
|
||||
return UTSSourceMapPosition("GeneralCallbackResultImpl", "uni_modules/lime-choose-file/utssdk/unierror.uts", 25, 14)
|
||||
}
|
||||
constructor(errCode: ChooseFileErrorCode, errMsg: String? = null) : super() {
|
||||
this.errSubject = UniErrorSubject
|
||||
this.errCode = errCode
|
||||
this.errMsg = errMsg ?: UniErrors[errCode] ?: ""
|
||||
}
|
||||
}
|
||||
val REQUEST_CODE_CHOOSE_FILE: Int = 42
|
||||
var resultFunction: ((requestCode: Int, resultCode: Int, data: Intent?) -> Unit)? = null
|
||||
open class ChooseFileImpl : ChooseFile, IUTSSourceMap {
|
||||
override fun `__$getOriginalPosition`(): UTSSourceMapPosition? {
|
||||
return UTSSourceMapPosition("ChooseFileImpl", "uni_modules/lime-choose-file/utssdk/app-android/index.uts", 17, 7)
|
||||
}
|
||||
override var name: String = ""
|
||||
override var path: String = ""
|
||||
override var size: Number = 0
|
||||
override var time: Number = 0
|
||||
override var type: String = "file"
|
||||
private var options: ChooseFileOption
|
||||
constructor(uri: Uri, options: ChooseFileOption){
|
||||
this.options = options
|
||||
this.time = Date.now()
|
||||
this.type = this.getFileTypeFromUri(uri)
|
||||
this.getFileInfoFromUri(uri)
|
||||
if (this.isCache(options)) {
|
||||
this.copyFileToCache(uri)
|
||||
}
|
||||
}
|
||||
private fun isCache(options: ChooseFileOption): Boolean {
|
||||
val extension = this.getFileExtension(this.name)
|
||||
val extensions = options.extension
|
||||
val type = options.type ?: "all"
|
||||
val hasExtension = extensions != null && extension != "" && extensions.includes(extension)
|
||||
val isVideoOrImage = _uA(
|
||||
"video",
|
||||
"image"
|
||||
).includes(type)
|
||||
val isFileAndNotVideoOrImage = type == "file" && !_uA(
|
||||
"video",
|
||||
"image"
|
||||
).includes(this.type)
|
||||
if ((type == "all" || isVideoOrImage || isFileAndNotVideoOrImage) && !hasExtension) {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
private fun getFileExtension(fileName: String): String {
|
||||
val lastDotIndex = fileName.lastIndexOf(".")
|
||||
if (lastDotIndex == -1) {
|
||||
return ""
|
||||
}
|
||||
return fileName.substring(lastDotIndex + 1)
|
||||
}
|
||||
private fun copyFileToCache(uri: Uri) {
|
||||
val cacheDir = UTSAndroid.getAppCachePath()
|
||||
val context = UTSAndroid.getAppContext()
|
||||
if (cacheDir != null) {
|
||||
val path = File(cacheDir)
|
||||
if (!path.exists()) {
|
||||
path.mkdir()
|
||||
}
|
||||
}
|
||||
var fileName = this.name
|
||||
if (this.options.filename != null) {
|
||||
fileName = this.options.filename!!
|
||||
if (this.options.count != null && this.options.count!! > 1) {
|
||||
fileName = "" + fileName + "_" + Date.now()
|
||||
}
|
||||
val extension = this.getFileExtension(this.name)
|
||||
fileName = "" + fileName + "." + extension
|
||||
}
|
||||
val destFile = File(cacheDir, fileName)
|
||||
try {
|
||||
val inputStream = context!!.getContentResolver().openInputStream(uri)
|
||||
val outputStream = FileOutputStream(destFile)
|
||||
if (inputStream != null) {
|
||||
var buffer = ByteArray(1024)
|
||||
var c = inputStream.read(buffer)
|
||||
while(c > 0){
|
||||
outputStream.write(buffer, 0, c)
|
||||
c = inputStream.read(buffer)
|
||||
}
|
||||
}
|
||||
this.path = cacheDir + fileName
|
||||
}
|
||||
catch (e: Throwable) {}
|
||||
}
|
||||
private fun getFileTypeFromUri(uri: Uri): String {
|
||||
val context = UTSAndroid.getAppContext()
|
||||
var fileType = "file"
|
||||
var mimeType = context!!.getContentResolver().getType(uri)
|
||||
if (mimeType != null) {
|
||||
if (mimeType.startsWith("video")) {
|
||||
fileType = "video"
|
||||
} else if (mimeType.startsWith("image")) {
|
||||
fileType = "image"
|
||||
}
|
||||
}
|
||||
return fileType
|
||||
}
|
||||
private fun getFileInfoFromUri(uri: Uri) {
|
||||
val context = UTSAndroid.getAppContext()
|
||||
var cursor = context!!.getContentResolver().query(uri, null, null, null, null)
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
this.name = cursor.getString(cursor.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME))
|
||||
val fileSize = cursor.getLong(cursor.getColumnIndexOrThrow(OpenableColumns.SIZE))
|
||||
this.size = UTSNumber.from(fileSize)
|
||||
cursor.close()
|
||||
} else if ("file".equals(uri.getScheme())) {
|
||||
this.name = uri.getLastPathSegment() ?: ""
|
||||
val file = File(uri.getPath() ?: "")
|
||||
val fileSize = file.length()
|
||||
this.size = UTSNumber.from(fileSize)
|
||||
}
|
||||
}
|
||||
}
|
||||
fun chooseFile(options: ChooseFileOption) {
|
||||
if (resultFunction != null) {
|
||||
UTSAndroid.offAppActivityResult(resultFunction!!)
|
||||
}
|
||||
val type = options.type ?: "all"
|
||||
val intent = Intent(Intent.ACTION_GET_CONTENT)
|
||||
if (type.equals("all") || type.equals("file")) {
|
||||
intent.setType("*/*")
|
||||
} else if (type.equals("video")) {
|
||||
intent.setType("video/*")
|
||||
} else if (type.equals("image")) {
|
||||
intent.setType("image/*")
|
||||
}
|
||||
intent.addCategory(Intent.CATEGORY_OPENABLE)
|
||||
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, if (options.count == 1) {
|
||||
false
|
||||
} else {
|
||||
true
|
||||
}
|
||||
)
|
||||
resultFunction = fun(requestCode: Int, resultCode: Int, data: Intent?){
|
||||
if (requestCode == REQUEST_CODE_CHOOSE_FILE) {
|
||||
UTSAndroid.offAppActivityResult(resultFunction!!)
|
||||
if (resultCode == -1 && data != null) {
|
||||
val clipData = data.getClipData()
|
||||
val tempFiles: UTSArray<ChooseFile> = _uA()
|
||||
if (clipData != null) {
|
||||
run {
|
||||
var i: Number = 0
|
||||
while(i < clipData.getItemCount()){
|
||||
val uri = clipData.getItemAt(i.toInt()).getUri()
|
||||
val chooseFile = ChooseFileImpl(uri, options)
|
||||
if (chooseFile.path != "") {
|
||||
tempFiles.push(chooseFile)
|
||||
}
|
||||
i++
|
||||
}
|
||||
}
|
||||
} else {
|
||||
val uri = data.getData()
|
||||
if (uri != null) {
|
||||
val chooseFile = ChooseFileImpl(uri, options)
|
||||
if (chooseFile.path != "") {
|
||||
tempFiles.push(chooseFile)
|
||||
}
|
||||
}
|
||||
}
|
||||
val count = options.count ?: Integer.MAX_VALUE
|
||||
if (tempFiles.length > 0 && count >= tempFiles.length) {
|
||||
options.success?.invoke(ChooseFileSuccessCallbackResult(tempFiles = tempFiles, errMsg = "chooseFile:ok"))
|
||||
} else {
|
||||
val err = GeneralCallbackResultImpl(9010002, "没有可用的文件或文件超过设置数量")
|
||||
options.fail?.invoke(err)
|
||||
options.complete?.invoke(err)
|
||||
}
|
||||
} else {
|
||||
val err = GeneralCallbackResultImpl(9010002, "没有可用的文件")
|
||||
options.fail?.invoke(err)
|
||||
options.complete?.invoke(err)
|
||||
}
|
||||
} else {
|
||||
val err = GeneralCallbackResultImpl(9010002, "没有可用的文件")
|
||||
options.fail?.invoke(err)
|
||||
options.complete?.invoke(err)
|
||||
}
|
||||
}
|
||||
UTSAndroid.onAppActivityResult(resultFunction!!)
|
||||
UTSAndroid.getUniActivity()!!.startActivityForResult(Intent.createChooser(intent, "选择文件"), REQUEST_CODE_CHOOSE_FILE)
|
||||
}
|
||||
open class ChooseFileOptionJSONObject : UTSJSONObject() {
|
||||
open var filename: String? = null
|
||||
open var count: Number? = null
|
||||
open var type: String? = null
|
||||
open var extension: UTSArray<String>? = null
|
||||
open var success: UTSCallback? = null
|
||||
open var fail: UTSCallback? = null
|
||||
open var complete: UTSCallback? = null
|
||||
}
|
||||
fun chooseFileByJs(options: ChooseFileOptionJSONObject) {
|
||||
return chooseFile(ChooseFileOption(filename = options.filename, count = options.count, type = options.type, extension = options.extension, success = fun(result: ChooseFileSuccessCallbackResult): Unit {
|
||||
options.success?.invoke(result)
|
||||
}
|
||||
, fail = fun(res: GeneralCallbackResult): Unit {
|
||||
options.fail?.invoke(res)
|
||||
}
|
||||
, complete = fun(res: GeneralCallbackResult): Unit {
|
||||
options.complete?.invoke(res)
|
||||
}
|
||||
))
|
||||
}
|
||||
Reference in New Issue
Block a user