第一次打包失败
This commit is contained in:
@@ -1,206 +0,0 @@
|
||||
// @ts-nocheck
|
||||
import Intent from 'android.content.Intent';
|
||||
import ClipData from 'android.content.ClipData';
|
||||
import Uri from 'android.net.Uri';
|
||||
import InputStream from 'java.io.InputStream';
|
||||
import OpenableColumns from 'android.provider.OpenableColumns';
|
||||
import Cursor from 'android.database.Cursor';
|
||||
import File from 'java.io.File';
|
||||
import FileInputStream from 'java.io.FileInputStream';
|
||||
import FileOutputStream from 'java.io.FileOutputStream';
|
||||
import BufferedOutputStream from 'java.io.BufferedOutputStream';
|
||||
import ByteArrayInputStream from 'java.io.ByteArrayInputStream';
|
||||
|
||||
import { ChooseFileOption, ChooseFile, ChooseFileSuccessCallbackResult } from '../interface'
|
||||
import { GeneralCallbackResultImpl } from '../unierror'
|
||||
|
||||
const REQUEST_CODE_CHOOSE_FILE : Int = 42
|
||||
let resultFunction : ((requestCode : Int, resultCode : Int, data ?: Intent) => void) | null = null
|
||||
|
||||
|
||||
class ChooseFileImpl implements ChooseFile {
|
||||
name : string = ''
|
||||
path : string = ''
|
||||
// private _path : string = ''
|
||||
size : number = 0
|
||||
time : number = 0
|
||||
type : string = 'file'
|
||||
// private uri : Uri
|
||||
private options: ChooseFileOption
|
||||
constructor(uri : Uri, options : ChooseFileOption) {
|
||||
// this.uri = uri
|
||||
this.options = options
|
||||
this.time = Date.now()
|
||||
this.type = this.getFileTypeFromUri(uri);
|
||||
// this._path = uri.getPath() ?? ''
|
||||
this.getFileInfoFromUri(uri)
|
||||
if (this.isCache(options)) {
|
||||
this.copyFileToCache(uri)
|
||||
}
|
||||
|
||||
}
|
||||
private isCache(options : ChooseFileOption) : boolean {
|
||||
const extension = this.getFileExtension(this.name)
|
||||
const extensions = options.extension
|
||||
const type = options.type ?? 'all'
|
||||
const hasExtension = extensions != null && extension != '' && extensions.includes(extension)
|
||||
const isVideoOrImage = ['video', 'image'].includes(type)
|
||||
const isFileAndNotVideoOrImage = type == 'file' && !['video', 'image'].includes(this.type);
|
||||
if ((type == 'all' || isVideoOrImage || isFileAndNotVideoOrImage) && !hasExtension) {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
private getFileExtension(fileName : string) : string {
|
||||
const lastDotIndex = fileName.lastIndexOf(".");
|
||||
if (lastDotIndex == -1) {
|
||||
return "";
|
||||
}
|
||||
return fileName.substring(lastDotIndex + 1);
|
||||
}
|
||||
private copyFileToCache(uri : Uri) {
|
||||
const cacheDir = UTSAndroid.getAppCachePath();
|
||||
const context = UTSAndroid.getAppContext();
|
||||
if(cacheDir != null) {
|
||||
const path = new File(cacheDir);
|
||||
if (!path.exists()) {
|
||||
path.mkdir();
|
||||
}
|
||||
}
|
||||
let 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()}`;
|
||||
}
|
||||
const extension = this.getFileExtension(this.name)
|
||||
|
||||
fileName = `${fileName}.${extension}`
|
||||
}
|
||||
const destFile = new File(cacheDir, fileName);
|
||||
|
||||
try {
|
||||
const inputStream = context!.getContentResolver().openInputStream(uri)
|
||||
const outputStream = new FileOutputStream(destFile)
|
||||
if (inputStream != null) {
|
||||
let buffer = ByteArray(1024);
|
||||
let c = inputStream.read(buffer)
|
||||
while (c > 0) {
|
||||
outputStream.write(buffer, 0, c);
|
||||
c = inputStream.read(buffer)
|
||||
}
|
||||
}
|
||||
this.path = cacheDir + fileName//this.name
|
||||
} catch (e) {
|
||||
|
||||
}
|
||||
}
|
||||
private getFileTypeFromUri(uri : Uri) : string {
|
||||
const context = UTSAndroid.getAppContext();
|
||||
let fileType = 'file'
|
||||
let mimeType = context!.getContentResolver().getType(uri);
|
||||
if (mimeType != null) {
|
||||
if (mimeType.startsWith("video")) {
|
||||
fileType = "video";
|
||||
} else if (mimeType.startsWith("image")) {
|
||||
fileType = "image";
|
||||
}
|
||||
}
|
||||
return fileType;
|
||||
}
|
||||
private getFileInfoFromUri(uri : Uri) {
|
||||
const context = UTSAndroid.getAppContext();
|
||||
let cursor = context!.getContentResolver().query(uri, null, null, null, null);
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
this.name = cursor.getString(cursor.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME))
|
||||
const fileSize = cursor.getLong(cursor.getColumnIndexOrThrow(OpenableColumns.SIZE));
|
||||
this.size = Number.from(fileSize)
|
||||
cursor.close();
|
||||
} else if ("file".equals(uri.getScheme())) {
|
||||
this.name = uri.getLastPathSegment() ?? '';
|
||||
const file = new File(uri.getPath() ?? '');
|
||||
const fileSize = file.length();
|
||||
this.size = Number.from(fileSize)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export function chooseFile(options : ChooseFileOption) {
|
||||
if (resultFunction != null) {
|
||||
UTSAndroid.offAppActivityResult(resultFunction!)
|
||||
}
|
||||
const type = options.type ?? 'all'
|
||||
const intent = new 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, options.count == 1 ? false : true); // 允许多选
|
||||
|
||||
resultFunction = (requestCode : Int, resultCode : Int, data ?: Intent) => {
|
||||
if (requestCode == REQUEST_CODE_CHOOSE_FILE) {
|
||||
UTSAndroid.offAppActivityResult(resultFunction!);
|
||||
if (resultCode == -1 && data != null) {
|
||||
const clipData = data.getClipData();
|
||||
const tempFiles : ChooseFile[] = []
|
||||
|
||||
if (clipData != null) {
|
||||
// 多选
|
||||
// const itemCount = clipData.getItemCount();
|
||||
// if (options.count != null && options.count! > itemCount) {
|
||||
// const err = new GeneralCallbackResultImpl(9010002, `选中文件数量超过${options.count}`)
|
||||
// options.fail?.(err)
|
||||
// options.complete?.(err)
|
||||
// return
|
||||
// }
|
||||
for (let i = 0; i < clipData.getItemCount(); i++) {
|
||||
const uri = clipData.getItemAt(i.toInt()).getUri();
|
||||
const chooseFile = new ChooseFileImpl(uri, options);
|
||||
if(chooseFile.path !=''){
|
||||
tempFiles.push(chooseFile)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// 单选
|
||||
const uri = data.getData();
|
||||
if (uri != null) {
|
||||
const chooseFile = new ChooseFileImpl(uri, options)
|
||||
if(chooseFile.path !='' ){
|
||||
tempFiles.push(chooseFile)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
const count = options.count ?? Integer.MAX_VALUE // Number.MAX_VALUE
|
||||
if(tempFiles.length > 0 && count >= tempFiles.length){
|
||||
options.success?.({
|
||||
tempFiles,
|
||||
errMsg: 'chooseFile:ok'
|
||||
} as ChooseFileSuccessCallbackResult)
|
||||
} else {
|
||||
const err = new GeneralCallbackResultImpl(9010002, `没有可用的文件或文件超过设置数量`)
|
||||
options.fail?.(err)
|
||||
options.complete?.(err)
|
||||
}
|
||||
} else {
|
||||
const err = new GeneralCallbackResultImpl(9010002, `没有可用的文件`)
|
||||
options.fail?.(err)
|
||||
options.complete?.(err)
|
||||
}
|
||||
} else {
|
||||
const err = new GeneralCallbackResultImpl(9010002, `没有可用的文件`)
|
||||
options.fail?.(err)
|
||||
options.complete?.(err)
|
||||
}
|
||||
}
|
||||
|
||||
UTSAndroid.onAppActivityResult(resultFunction!)
|
||||
UTSAndroid.getUniActivity()!.startActivityForResult(Intent.createChooser(intent, "选择文件"), REQUEST_CODE_CHOOSE_FILE)
|
||||
// UTSAndroid.getUniActivity()!.overridePendingTransition((10).toInt(), (0).toInt());
|
||||
}
|
||||
262
unpackage/dist/build/app-plus/uni_modules/lime-choose-file/utssdk/app-android/src/index.kt
vendored
Normal file
262
unpackage/dist/build/app-plus/uni_modules/lime-choose-file/utssdk/app-android/src/index.kt
vendored
Normal file
@@ -0,0 +1,262 @@
|
||||
@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.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()
|
||||
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()
|
||||
val UniErrorSubject = "chooseFile\""
|
||||
val UniErrors: Map<ChooseFileErrorCode, String> = Map(_uA(
|
||||
_uA(
|
||||
9010001,
|
||||
"chooseFile:ok"
|
||||
),
|
||||
_uA(
|
||||
9010002,
|
||||
"ChooseFile:failed"
|
||||
)
|
||||
))
|
||||
open class GeneralCallbackResultImpl : UniError, GeneralCallbackResult {
|
||||
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 {
|
||||
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)
|
||||
}
|
||||
))
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
{
|
||||
"deploymentTarget": "9"
|
||||
}
|
||||
@@ -1,138 +0,0 @@
|
||||
// @ts-nocheck
|
||||
import { ChooseFileOption, ChooseFile, ChooseFileSuccessCallbackResult } from '../interface'
|
||||
import { UIDocumentPickerDelegate, UIDocumentPickerMode } from "UIKit"
|
||||
import { URL, FileManager } from 'Foundation';
|
||||
import { GeneralCallbackResultImpl } from '../unierror'
|
||||
import { DispatchQueue } from 'Dispatch';
|
||||
|
||||
const documentTypes : Map<string, string[]> = new Map([
|
||||
["all", ["public.item"]],
|
||||
["file", [
|
||||
"public.text",
|
||||
"public.zip-archive",
|
||||
"public.data",
|
||||
"com.adobe.pdf",
|
||||
"com.microsoft.word.doc",
|
||||
"com.microsoft.word.docx",
|
||||
"com.microsoft.excel.xls",
|
||||
"com.microsoft.excel.xlsx"
|
||||
]
|
||||
],
|
||||
["video", ["public.movie"]],
|
||||
["image", ["public.image"]],
|
||||
])
|
||||
|
||||
class ChooseFileImpl {
|
||||
name : string = ''
|
||||
path : string = ''
|
||||
size : number = 0
|
||||
time : number = 0
|
||||
type : string = 'file'
|
||||
constructor(uri : URL, options : ChooseFileOption) {
|
||||
try {
|
||||
const originalFileName = uri.lastPathComponent;
|
||||
const attributes = UTSiOS.try(FileManager.default.attributesOfItem(atPath = uri.path))
|
||||
const fileSize = attributes[FileAttributeKey.size] as number
|
||||
const pathExtension = `${uri.pathExtension}`
|
||||
|
||||
const imageFormats = ['jpeg', 'jpg', 'png', 'gif', 'bmp', 'tiff', 'svg']
|
||||
const videoFormats = ['mov', 'm4v', 'mp4', 'avi']
|
||||
|
||||
|
||||
let fileName = originalFileName
|
||||
if(options.filename != null) {
|
||||
fileName = options.filename!
|
||||
if(options.count != null && options.count! > 1) {
|
||||
fileName = `${fileName}_${Date.now()}`;
|
||||
}
|
||||
fileName = `${fileName}.${pathExtension}`
|
||||
}
|
||||
|
||||
// #ifdef UNI-APP-X
|
||||
const dataPath = UTSiOS.getDataPath()
|
||||
// #endif
|
||||
// #ifndef UNI-APP-X
|
||||
const dataPath = UTSiOS.getDataPath().replace(/data$/, "doc");
|
||||
// #endif
|
||||
const file = new URL(fileURLWithPath = dataPath).appendingPathComponent(fileName)//.absoluteString
|
||||
const fileData = FileManager.default.contents(atPath = uri.path);
|
||||
UTSiOS.try(fileData?.write(to = file))
|
||||
|
||||
if (imageFormats.includes(pathExtension)) {
|
||||
this.type = 'image'
|
||||
} else if (videoFormats.includes(pathExtension)) {
|
||||
this.type = 'video'
|
||||
} else {
|
||||
this.type = 'file'
|
||||
}
|
||||
|
||||
this.name = `${originalFileName}`
|
||||
this.size = fileSize
|
||||
this.path = `${file.absoluteString}`
|
||||
} catch (e) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class FilePickerManager implements UIDocumentPickerDelegate {
|
||||
options : ChooseFileOption = {}
|
||||
constructor() { }
|
||||
chooseFile(options : ChooseFileOption) {
|
||||
DispatchQueue.main.async(execute = () : void => {
|
||||
this.options = options
|
||||
const type = options.type ?? 'all'
|
||||
const count = options.count ?? 1
|
||||
const types = (documentTypes.get(type) ?? documentTypes.get('all')) as string[]
|
||||
let documentPicker = UIDocumentPickerViewController(
|
||||
documentTypes = types,
|
||||
in = UIDocumentPickerMode.import
|
||||
)
|
||||
documentPicker.delegate = this
|
||||
// 多选要大于 ios11
|
||||
if (UTSiOS.available("iOS 11.0, *")) {
|
||||
documentPicker.allowsMultipleSelection = count > 1
|
||||
}
|
||||
UTSiOS.getCurrentViewController().present(documentPicker, animated = true)
|
||||
})
|
||||
}
|
||||
documentPicker(controller : UIDocumentPickerViewController, @argumentLabel("didPickDocumentsAt") urls : URL[]) {
|
||||
DispatchQueue.main.async(execute = () : void => {
|
||||
const tempFiles : ChooseFile[] = []
|
||||
for (let i = 0; i < urls.length; i++) {
|
||||
const url = urls[i]
|
||||
const chooseFile = new ChooseFileImpl(url, this.options);
|
||||
// IOS -> js 无法传class?
|
||||
const file : ChooseFile = {
|
||||
name: chooseFile.name,
|
||||
path: chooseFile.path,
|
||||
size: chooseFile.size,
|
||||
time: chooseFile.time,
|
||||
type: chooseFile.type,
|
||||
}
|
||||
if (chooseFile.path != '') {
|
||||
tempFiles.push(file)
|
||||
}
|
||||
}
|
||||
|
||||
const count = this.options.count ?? Number.from(Double.greatestFiniteMagnitude) //Number.from(Double.MAX_VALUE)
|
||||
if (tempFiles.length > 0 && count >= tempFiles.length) {
|
||||
const res : ChooseFileSuccessCallbackResult = {
|
||||
tempFiles,
|
||||
errMsg: 'chooseFile:ok'
|
||||
}
|
||||
this.options.success?.(res)
|
||||
} else {
|
||||
const err = new GeneralCallbackResultImpl(9010002, `没有可用的文件或文件超过设置数量`)
|
||||
this.options.fail?.(err)
|
||||
this.options.complete?.(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const fileManager = new FilePickerManager()
|
||||
|
||||
export function chooseFile(options : ChooseFileOption) {
|
||||
fileManager.chooseFile(options)
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
// @ts-nocheck
|
||||
export * from './interface';
|
||||
import { type ChooseFileOption } from './interface';
|
||||
|
||||
export function chooseFile(options: ChooseFileOption){
|
||||
// #ifdef WEB || APP-HARMONY
|
||||
uni.chooseFile({
|
||||
count: options.count ?? 100,
|
||||
type: options.type,
|
||||
extension: options.extension,
|
||||
success(res) {
|
||||
options.success?.({
|
||||
// tempFilePaths
|
||||
errMsg : 'ok',
|
||||
tempFiles: res.tempFiles
|
||||
})
|
||||
},
|
||||
fail(err) {
|
||||
options.fail?.({
|
||||
errCode: err.errCode,
|
||||
errSubject: 'lime-choose-file'
|
||||
})
|
||||
},
|
||||
complete(res) {
|
||||
// options.complete?.(res)
|
||||
}
|
||||
})
|
||||
// #endif
|
||||
// #ifdef MP-WEIXIN
|
||||
uni.chooseMessageFile(options)
|
||||
// #endif
|
||||
// #ifndef WEB || MP-WEIXIN || APP-HARMONY
|
||||
console.error('chooseFile 不支持该平台')
|
||||
// #endif
|
||||
}
|
||||
@@ -1,108 +0,0 @@
|
||||
// @ts-nocheck
|
||||
/**
|
||||
* 错误码
|
||||
* 根据uni错误码规范要求,建议错误码以90开头,以下是错误码示例:
|
||||
* - 9010001 错误信息1
|
||||
* - 9010002 错误信息2
|
||||
*/
|
||||
export type ChooseFileErrorCode = 9010001 | 9010002 | 1101001 | 1101002 | 1101003 | 1101004 | 1101005 | 1101006 | 1101007 | 1101008 | 1101009 | 1101010;
|
||||
/**
|
||||
* myApi 的错误回调参数
|
||||
*/
|
||||
export interface GeneralCallbackResult extends IUniError {
|
||||
errCode : ChooseFileErrorCode
|
||||
};
|
||||
|
||||
|
||||
/** 返回选择的文件的本地临时文件对象数组 */
|
||||
// #ifdef APP-ANDROID
|
||||
export interface ChooseFile {
|
||||
/** 选择的文件名称 */
|
||||
name : string
|
||||
/** 本地临时文件路径 (本地路径) */
|
||||
path : string
|
||||
/** 本地临时文件大小,单位 B */
|
||||
size : number
|
||||
/** 选择的文件的会话发送时间,Unix时间戳,工具暂不支持此属性 */
|
||||
time : number
|
||||
/** 选择的文件类型
|
||||
*
|
||||
* 可选值:
|
||||
* - 'video': 选择了视频文件;
|
||||
* - 'image': 选择了图片文件;
|
||||
* - 'file': 选择了除图片和视频的文件; */
|
||||
type : 'video' | 'image' | 'file' | 'all'
|
||||
}
|
||||
// #endif
|
||||
// #ifndef APP-ANDROID
|
||||
export type ChooseFile = {
|
||||
/** 选择的文件名称 */
|
||||
name : string
|
||||
/** 本地临时文件路径 (本地路径) */
|
||||
path : string
|
||||
/** 本地临时文件大小,单位 B */
|
||||
size : number
|
||||
/** 选择的文件的会话发送时间,Unix时间戳,工具暂不支持此属性 */
|
||||
time : number
|
||||
/** 选择的文件类型
|
||||
*
|
||||
* 可选值:
|
||||
* - 'video': 选择了视频文件;
|
||||
* - 'image': 选择了图片文件;
|
||||
* - 'file': 选择了除图片和视频的文件; */
|
||||
type : 'video' | 'image' | 'file' | 'all'
|
||||
}
|
||||
// #endif
|
||||
|
||||
export type ChooseFileSuccessCallbackResult = {
|
||||
/** 返回选择的文件的本地临时文件对象数组 */
|
||||
tempFiles : ChooseFile[],
|
||||
errMsg : string
|
||||
}
|
||||
|
||||
|
||||
/** 接口调用成功的回调函数 */
|
||||
export type ChooseFileSuccessCallback = (
|
||||
result : ChooseFileSuccessCallbackResult
|
||||
) => void
|
||||
|
||||
/** 接口调用失败的回调函数 */
|
||||
export type ChooseFileFailCallback = (res : GeneralCallbackResult) => void
|
||||
|
||||
/** 接口调用结束的回调函数(调用成功、失败都会执行) */
|
||||
export type ChooseFileCompleteCallback = (
|
||||
res : GeneralCallbackResult
|
||||
) => void
|
||||
|
||||
export type ChooseFileOption = {
|
||||
/**
|
||||
* 指定文件名,如果是多选就在后面增加上时间
|
||||
*/
|
||||
filename?: string
|
||||
/**
|
||||
* 最多可以选择的文件数量。
|
||||
* @defaultValue 100
|
||||
*/
|
||||
count ?: number | null,
|
||||
/**
|
||||
* 所选文件类型
|
||||
* @defaultValue all
|
||||
*/
|
||||
type ?: string | null,
|
||||
/**
|
||||
* 根据文件拓展名过滤,每一项都不能是空字符串。默认不过滤。仅H5支持
|
||||
*/
|
||||
extension ?: (string[]) | null,
|
||||
/**
|
||||
* 成功则返回图片的本地文件路径列表 tempFilePaths
|
||||
*/
|
||||
success ?: ChooseFileSuccessCallback | null,
|
||||
/**
|
||||
* 接口调用失败的回调函数
|
||||
*/
|
||||
fail ?: ChooseFileFailCallback | null,
|
||||
/**
|
||||
* 接口调用结束的回调函数(调用成功、失败都会执行)
|
||||
*/
|
||||
complete ?: ChooseFileCompleteCallback | null
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
// @ts-nocheck
|
||||
/* 此规范为 uni 规范,可以按照自己的需要选择是否实现 */
|
||||
import { ChooseFileErrorCode, GeneralCallbackResult } from "./interface.uts"
|
||||
/**
|
||||
* 错误主题
|
||||
* 注意:错误主题一般为插件名称,每个组件不同,需要使用时请更改。
|
||||
* [可选实现]
|
||||
*/
|
||||
export const UniErrorSubject = 'chooseFile"';
|
||||
|
||||
|
||||
/**
|
||||
* 错误信息
|
||||
* @UniError
|
||||
* [可选实现]
|
||||
*/
|
||||
export const UniErrors : Map<ChooseFileErrorCode, string> = new Map([
|
||||
/**
|
||||
* 错误码及对应的错误信息
|
||||
*/
|
||||
[9010001, 'chooseFile:ok'],
|
||||
[9010002, 'ChooseFile:failed'],
|
||||
]);
|
||||
|
||||
|
||||
/**
|
||||
* 错误对象实现
|
||||
*/
|
||||
export class GeneralCallbackResultImpl extends UniError implements GeneralCallbackResult {
|
||||
|
||||
/**
|
||||
* 错误对象构造函数
|
||||
*/
|
||||
constructor(errCode : ChooseFileErrorCode, errMsg: string|null = null) {
|
||||
super();
|
||||
this.errSubject = UniErrorSubject;
|
||||
this.errCode = errCode;
|
||||
this.errMsg = errMsg ?? UniErrors[errCode] ?? "";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user