first commit

This commit is contained in:
2026-06-02 10:42:33 +08:00
commit dd4975fd2c
1084 changed files with 442416 additions and 0 deletions

View File

@@ -0,0 +1,203 @@
// @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 {
override name: string = '';
override path: string = '';
// private _path : string = ''
override size: number = 0;
override time: number = 0;
override 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: any) {
}
}
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());
}
//# sourceMappingURL=index.uts.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,77 @@
// @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;
}
;
/** 返回选择的文件的本地临时文件对象数组 */
export interface ChooseFile {
/** 选择的文件名称 */
name: string;
/** 本地临时文件路径 (本地路径) */
path: string;
/** 本地临时文件大小,单位 B */
size: number;
/** 选择的文件的会话发送时间Unix时间戳工具暂不支持此属性 */
time: number;
/** 选择的文件类型
*
* 可选值:
* - 'video': 选择了视频文件;
* - 'image': 选择了图片文件;
* - 'file': 选择了除图片和视频的文件; */
type: 'video' | 'image' | 'file' | 'all';
}
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;
};
//# sourceMappingURL=interface.uts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"interface.uts","sourceRoot":"","sources":["uni_modules/lime-choose-file/utssdk/interface.uts"],"names":[],"mappings":"AAAA,cAAc;AACd;;;;;GAKG;AACH,MAAM,MAAM,mBAAmB,GAAG,OAAO,GAAG,OAAO,GAAG,OAAO,GAAG,OAAO,GAAG,OAAO,GAAG,OAAO,GAAG,OAAO,GAAG,OAAO,GAAG,OAAO,GAAG,OAAO,GAAG,OAAO,GAAG,OAAO,CAAC;AACxJ;;GAEG;AACH,MAAM,WAAW,qBAAsB,SAAQ,SAAS;IACvD,OAAO,EAAG,mBAAmB,CAAA;CAC7B;AAAA,CAAC;AAGF,yBAAyB;AAEzB,MAAM,WAAW,UAAU;IAC1B,cAAc;IACd,IAAI,EAAG,MAAM,CAAA;IACb,sBAAsB;IACtB,IAAI,EAAG,MAAM,CAAA;IACb,oBAAoB;IACpB,IAAI,EAAG,MAAM,CAAA;IACb,qCAAqC;IACrC,IAAI,EAAG,MAAM,CAAA;IACb;;;;;iCAK6B;IAC7B,IAAI,EAAG,OAAO,GAAG,OAAO,GAAG,MAAM,GAAG,KAAK,CAAA;CACzC;AAsBD,MAAM,MAAM,+BAA+B,GAAG;IAC7C,yBAAyB;IACzB,SAAS,EAAG,UAAU,EAAE,CAAC;IACzB,MAAM,EAAG,MAAM,CAAA;CACf,CAAA;AAGD,kBAAkB;AAClB,MAAM,MAAM,yBAAyB,GAAG,CACvC,MAAM,EAAG,+BAA+B,KACpC,IAAI,CAAA;AAET,kBAAkB;AAClB,MAAM,MAAM,sBAAsB,GAAG,CAAC,GAAG,EAAG,qBAAqB,KAAK,IAAI,CAAA;AAE1E,+BAA+B;AAC/B,MAAM,MAAM,0BAA0B,GAAG,CACxC,GAAG,EAAG,qBAAqB,KACvB,IAAI,CAAA;AAET,MAAM,MAAM,gBAAgB,GAAG;IAC9B;;OAEG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB;;;OAGG;IACH,KAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB;;;OAGG;IACH,IAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB;;OAEG;IACH,SAAU,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,GAAG,IAAI,CAAC;IAC/B;;OAEG;IACH,OAAQ,CAAC,EAAE,yBAAyB,GAAG,IAAI,CAAC;IAC5C;;OAEG;IACH,IAAK,CAAC,EAAE,sBAAsB,GAAG,IAAI,CAAC;IACtC;;OAEG;IACH,QAAS,CAAC,EAAE,0BAA0B,GAAG,IAAI,CAAA;CAC7C,CAAA","sourcesContent":["// @ts-nocheck\r\n/**\r\n * 错误码\r\n * 根据uni错误码规范要求建议错误码以90开头以下是错误码示例\r\n * - 9010001 错误信息1\r\n * - 9010002 错误信息2\r\n */\r\nexport type ChooseFileErrorCode = 9010001 | 9010002 | 1101001 | 1101002 | 1101003 | 1101004 | 1101005 | 1101006 | 1101007 | 1101008 | 1101009 | 1101010;\r\n/**\r\n * myApi 的错误回调参数\r\n */\r\nexport interface GeneralCallbackResult extends IUniError {\r\n\terrCode : ChooseFileErrorCode\r\n};\r\n\r\n\r\n/** 返回选择的文件的本地临时文件对象数组 */\r\n\r\nexport interface ChooseFile {\r\n\t/** 选择的文件名称 */\r\n\tname : string\r\n\t/** 本地临时文件路径 (本地路径) */\r\n\tpath : string\r\n\t/** 本地临时文件大小,单位 B */\r\n\tsize : number\r\n\t/** 选择的文件的会话发送时间Unix时间戳工具暂不支持此属性 */\r\n\ttime : number\r\n\t/** 选择的文件类型\r\n\t *\r\n\t * 可选值:\r\n\t * - 'video': 选择了视频文件;\r\n\t * - 'image': 选择了图片文件;\r\n\t * - 'file': 选择了除图片和视频的文件; */\r\n\ttype : 'video' | 'image' | 'file' | 'all'\r\n}\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\nexport type ChooseFileSuccessCallbackResult = {\r\n\t/** 返回选择的文件的本地临时文件对象数组 */\r\n\ttempFiles : ChooseFile[],\r\n\terrMsg : string\r\n}\r\n\r\n\r\n/** 接口调用成功的回调函数 */\r\nexport type ChooseFileSuccessCallback = (\r\n\tresult : ChooseFileSuccessCallbackResult\r\n) => void\r\n\r\n/** 接口调用失败的回调函数 */\r\nexport type ChooseFileFailCallback = (res : GeneralCallbackResult) => void\r\n\r\n/** 接口调用结束的回调函数(调用成功、失败都会执行) */\r\nexport type ChooseFileCompleteCallback = (\r\n\tres : GeneralCallbackResult\r\n) => void\r\n\r\nexport type ChooseFileOption = {\r\n\t/**\r\n\t * 指定文件名,如果是多选就在后面增加上时间\r\n\t */\r\n\tfilename?: string\r\n\t/**\r\n\t * 最多可以选择的文件数量。\r\n\t * @defaultValue 100\r\n\t */\r\n\tcount ?: number | null,\r\n\t/**\r\n\t * 所选文件类型\r\n\t * @defaultValue all\r\n\t */\r\n\ttype ?: string | null,\r\n\t/**\r\n\t * 根据文件拓展名过滤每一项都不能是空字符串。默认不过滤。仅H5支持\r\n\t */\r\n\textension ?: (string[]) | null,\r\n\t/**\r\n\t * 成功则返回图片的本地文件路径列表 tempFilePaths\r\n\t */\r\n\tsuccess ?: ChooseFileSuccessCallback | null,\r\n\t/**\r\n\t * 接口调用失败的回调函数\r\n\t */\r\n\tfail ?: ChooseFileFailCallback | null,\r\n\t/**\r\n\t * 接口调用结束的回调函数(调用成功、失败都会执行)\r\n\t */\r\n\tcomplete ?: ChooseFileCompleteCallback | null\r\n}"]}

View File

@@ -0,0 +1,36 @@
// @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] ?? "";
}
}
//# sourceMappingURL=unierror.uts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"unierror.uts","sourceRoot":"","sources":["uni_modules/lime-choose-file/utssdk/unierror.uts"],"names":[],"mappings":"AAAA,cAAc;AACd,iCAAiC;AACjC,OAAO,EAAE,mBAAmB,EAAE,qBAAqB,EAAE,MAAM,iBAAiB,CAAA;AAC5E;;;;GAIG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,aAAa,CAAC;AAG7C;;;;GAIG;AACH,MAAM,CAAC,MAAM,SAAS,EAAG,GAAG,CAAC,mBAAmB,EAAE,MAAM,CAAC,GAAG,IAAI,GAAG,CAAC;IAClE;;OAEG;IACH,CAAC,OAAO,EAAE,eAAe,CAAC;IAC1B,CAAC,OAAO,EAAE,mBAAmB,CAAC;CAC/B,CAAC,CAAC;AAGH;;GAEG;AACH,MAAM,OAAO,yBAA0B,SAAQ,QAAS,YAAW,qBAAqB;IAEtF;;OAEG;IACH,YAAY,OAAO,EAAG,mBAAmB,EAAE,MAAM,EAAE,MAAM,GAAC,IAAI,GAAG,IAAI;QACnE,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,UAAU,GAAG,eAAe,CAAC;QAClC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,MAAM,GAAG,MAAM,IAAI,SAAS,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC;IACnD,CAAC;CACF","sourcesContent":["// @ts-nocheck\r\n/* 此规范为 uni 规范,可以按照自己的需要选择是否实现 */\r\nimport { ChooseFileErrorCode, GeneralCallbackResult } from \"./interface.uts\"\r\n/**\r\n * 错误主题\r\n * 注意:错误主题一般为插件名称,每个组件不同,需要使用时请更改。\r\n * [可选实现]\r\n */\r\nexport const UniErrorSubject = 'chooseFile\"';\r\n\r\n\r\n/**\r\n * 错误信息\r\n * @UniError\r\n * [可选实现]\r\n */\r\nexport const UniErrors : Map<ChooseFileErrorCode, string> = new Map([\r\n /**\r\n * 错误码及对应的错误信息\r\n */\r\n [9010001, 'chooseFile:ok'],\r\n [9010002, 'ChooseFile:failed'],\r\n]);\r\n\r\n\r\n/**\r\n * 错误对象实现\r\n */\r\nexport class GeneralCallbackResultImpl extends UniError implements GeneralCallbackResult {\r\n\r\n /**\r\n * 错误对象构造函数\r\n */\r\n constructor(errCode : ChooseFileErrorCode, errMsg: string|null = null) {\r\n super();\r\n this.errSubject = UniErrorSubject;\r\n this.errCode = errCode;\r\n this.errMsg = errMsg ?? UniErrors[errCode] ?? \"\";\r\n }\r\n}"]}