1
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"minSdkVersion": "19"
|
||||
}
|
||||
@@ -0,0 +1,432 @@
|
||||
import MediaStore from "android.provider.MediaStore"
|
||||
import Activity from 'android.app.Activity';
|
||||
import Bundle from 'android.os.Bundle';
|
||||
import Intent from 'android.content.Intent';
|
||||
import Uri from "android.net.Uri"
|
||||
import Context from 'android.content.Context'
|
||||
import ContentResolver from 'android.content.ContentResolver'
|
||||
import InputStream from 'java.io.InputStream'
|
||||
import FileOutputStream from 'java.io.FileOutputStream'
|
||||
import File from 'java.io.File'
|
||||
|
||||
import ByteArrayOutputStream from 'java.io.ByteArrayOutputStream'
|
||||
import MimeTypeMap from 'android.webkit.MimeTypeMap'
|
||||
import OpenableColumns from 'android.provider.OpenableColumns'
|
||||
import Build from 'android.os.Build'
|
||||
import ParcelFileDescriptor from 'android.os.ParcelFileDescriptor'
|
||||
|
||||
import { xFileSListType,fileListType } from "../interface.uts"
|
||||
|
||||
type xFileSNameTYpe = {
|
||||
name : string,
|
||||
type : string,
|
||||
uri : Uri,
|
||||
id : string,
|
||||
realFilePath : string,
|
||||
cacheFilePath : string,
|
||||
fileSize : number,
|
||||
status : number
|
||||
}
|
||||
type callReadBackFun = (byte : ByteArray) => void
|
||||
|
||||
type resumtype = (requestCode : Int, resultCode : Int, data : any) => void
|
||||
|
||||
const mimeTypes = new Map<string, string>([
|
||||
// 图片类型
|
||||
["jpg", "image/*"],
|
||||
["jpeg", "image/*"],
|
||||
["png", "image/*"],
|
||||
["gif", "image/*"],
|
||||
["bmp", "image/*"],
|
||||
["webp", "image/*"],
|
||||
["tiff", "image/*"],
|
||||
["svg", "image/*"],
|
||||
["ico", "image/*"],
|
||||
|
||||
// 视频类型
|
||||
["mp4", "video/*"],
|
||||
["webm", "video/*"],
|
||||
["ogv", "video/*"],
|
||||
["mov", "video/*"],
|
||||
["avi", "video/*"],
|
||||
["wmv", "video/*"],
|
||||
|
||||
// 音频类型
|
||||
["mp3", "audio/*"],
|
||||
["wav", "audio/*"],
|
||||
["ogg", "audio/*"],
|
||||
["flac", "audio/*"],
|
||||
["aac", "audio/*"],
|
||||
|
||||
// 文档类型
|
||||
["pdf", "application/*"],
|
||||
["doc", "application/*"],
|
||||
["docx", "application/*"],
|
||||
["xls", "application/*"],
|
||||
["xlsx", "application/*"],
|
||||
["ppt", "application/*"],
|
||||
["pptx", "application/*"],
|
||||
["odt", "application/*"],
|
||||
["ods", "application/*"],
|
||||
|
||||
// 文本类型
|
||||
["txt", "text/plain"],
|
||||
["html", "text/html"],
|
||||
["css", "text/css"],
|
||||
["js", "text/javascript"],
|
||||
["xml", "text/xml"],
|
||||
|
||||
// 其他类型
|
||||
["zip", "application/*"],
|
||||
["rar", "application/*"],
|
||||
["7z", "application/*"],
|
||||
["json", "application/*"]
|
||||
]);
|
||||
|
||||
|
||||
|
||||
function getMimeTypeFromUri(uri : Uri, max : number) : xFileSNameTYpe {
|
||||
let context = UTSAndroid.getAppContext() as Context
|
||||
let contentResolver = context.getContentResolver() as ContentResolver;
|
||||
let mime = MimeTypeMap.getSingleton() as MimeTypeMap;
|
||||
let gz = mime.getExtensionFromMimeType(contentResolver.getType(uri)) as string | null;
|
||||
let str = uri.getEncodedPath()
|
||||
let par = contentResolver.openFileDescriptor(uri, "r") as ParcelFileDescriptor
|
||||
let filesize = par.getStatSize()
|
||||
if (gz == null) {
|
||||
gz = str!.substring(str!.lastIndexOf(".") + 1)
|
||||
}
|
||||
let name = str!.substring(0, str!.lastIndexOf("/"))
|
||||
name = str!.substring(0, str!.lastIndexOf("."))
|
||||
///安卓10,api 29+需要遵循隐私策略获取名称。
|
||||
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.Q) {
|
||||
let cursor = contentResolver.query(uri, null, null, null, null)
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
let nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
|
||||
if (nameIndex != -1) {
|
||||
name = cursor.getString(nameIndex);
|
||||
}
|
||||
cursor.close();
|
||||
}
|
||||
// 确保关闭游标
|
||||
if (cursor != null && !cursor.isClosed()) {
|
||||
cursor.close();
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
name,
|
||||
type: gz!,
|
||||
uri: uri,
|
||||
id: Math.floor(Math.random() * 1 * Math.floor(Math.random() * Date.now())).toString().substring(0, 10),
|
||||
realFilePath: "",
|
||||
cacheFilePath: "",
|
||||
fileSize: filesize,
|
||||
status: filesize > max ? 4 : 0
|
||||
|
||||
} as xFileSNameTYpe;
|
||||
}
|
||||
|
||||
function readFileToInputStrem(uri : Uri) : ByteArray {
|
||||
//读写块的大小。
|
||||
let sizeBlock = 1024
|
||||
let context = UTSAndroid.getAppContext() as Context
|
||||
let cover = context.getContentResolver() as ContentResolver
|
||||
let inputStream = cover.openInputStream(uri)
|
||||
let byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
// 文件的字节数组
|
||||
let buffer = new ByteArray(sizeBlock.toInt());
|
||||
let n = inputStream!.read(buffer)
|
||||
while (-1 != n) {
|
||||
byteArrayOutputStream.write(buffer, 0, n);
|
||||
n = inputStream!.read(buffer)
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
let filelistAll = [] as xFileSNameTYpe[]
|
||||
function isInfilelist(file : xFileSNameTYpe) : boolean {
|
||||
return filelistAll.some((el) : boolean => {
|
||||
return el.name == file.name && el.type == file.type
|
||||
})
|
||||
}
|
||||
let callFun = (list : fileListType[]) => { }
|
||||
let nowXFileSystem = null as null|xFileSystem;
|
||||
const activityResult = (requestCode : Int, resultCode : Int, data ?: Intent)=>{
|
||||
|
||||
let jgCode = 1;
|
||||
console.log(1)
|
||||
if (data != null && requestCode == jgCode.toInt()) {
|
||||
let fdata = data! as Intent
|
||||
let flistdata = fdata.getClipData();
|
||||
let flistdataOnly = data?.data
|
||||
//多选
|
||||
if (flistdata != null) {
|
||||
let count = flistdata.getItemCount();
|
||||
|
||||
for (let i = 0; i < count; i++) {
|
||||
let item = flistdata.getItemAt(i.toInt())
|
||||
let uri = item.getUri()
|
||||
|
||||
|
||||
let itemfile = getMimeTypeFromUri(uri, nowXFileSystem!.maxFileSize)
|
||||
if (!isInfilelist(itemfile)) {
|
||||
|
||||
filelistAll.push(itemfile)
|
||||
}
|
||||
|
||||
}
|
||||
//单选
|
||||
} else if (flistdataOnly != null) {
|
||||
let itemfile = getMimeTypeFromUri(flistdataOnly!, nowXFileSystem!.maxFileSize)
|
||||
if (!isInfilelist(itemfile)) {
|
||||
filelistAll.push(itemfile)
|
||||
}
|
||||
}
|
||||
let listfile = filelistAll.map((el) : fileListType => {
|
||||
return {
|
||||
name: el.name,
|
||||
type: el.type,
|
||||
id: el.id,
|
||||
uri: '',
|
||||
realFilePath: el.realFilePath,
|
||||
cacheFilePath: el.cacheFilePath,
|
||||
status: el.fileSize > nowXFileSystem!.maxFileSize ? 4 : 0,
|
||||
size: el.fileSize
|
||||
} as fileListType
|
||||
});
|
||||
|
||||
callFun(listfile)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 由于安卓隐私的问题。实际得到的所有文件 其实 都不是真实地址。因此你想要得到真实的地址,你就得复制到你的应用沙盒中。
|
||||
*/
|
||||
export class xFileSystem {
|
||||
mimeTypeFilter : string[] = [] as string[]
|
||||
filelist = [] as xFileSNameTYpe[]
|
||||
multiple : boolean = true;
|
||||
|
||||
maxFileSize = 30 * 1024 * 1024;
|
||||
|
||||
constructor(typeFilter : string[] | null, tempmultiple : boolean | null, maxSize : number | null) {
|
||||
if (typeFilter != null) {
|
||||
|
||||
let filter : string[] = [] as string[];
|
||||
let tms = typeFilter! as string[]
|
||||
for (let i = 0; i < tms.length; i++) {
|
||||
if (mimeTypes.get(tms[i]) != null) {
|
||||
filter.push(mimeTypes.get(tms[i])!)
|
||||
}
|
||||
}
|
||||
if (filter.length == 0) {
|
||||
filter.push("*/*")
|
||||
}
|
||||
|
||||
this.mimeTypeFilter = filter
|
||||
|
||||
}
|
||||
if (tempmultiple != null) {
|
||||
this.multiple = tempmultiple!
|
||||
}
|
||||
if (maxSize != null) {
|
||||
this.maxFileSize = maxSize!
|
||||
}
|
||||
nowXFileSystem = this;
|
||||
UTSAndroid.onAppActivityResult(activityResult);
|
||||
}
|
||||
|
||||
|
||||
|
||||
destory() {
|
||||
UTSAndroid.offAppActivityResult(activityResult)
|
||||
// filelistAll = [] as xFileSNameTYpe[]
|
||||
}
|
||||
openDocument(call : (list : fileListType[]) => void) {
|
||||
|
||||
let intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
||||
intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
|
||||
} else {
|
||||
intent.setAction(Intent.ACTION_GET_CONTENT);
|
||||
}
|
||||
|
||||
// intent.addCategory(Intent.CATEGORY_OPENABLE);
|
||||
if (this.mimeTypeFilter.length == 0) {
|
||||
intent.type = "*/*"
|
||||
} else {
|
||||
let key = this.mimeTypeFilter[this.mimeTypeFilter.length - 1]
|
||||
let ftype = key;
|
||||
if (ftype != null) {
|
||||
intent.setType(ftype!);
|
||||
} else {
|
||||
intent.setType("*/*");
|
||||
}
|
||||
// console.log(this.mimeTypeFilter)
|
||||
// intent.putExtra(Intent.EXTRA_MIME_TYPES, []);
|
||||
|
||||
}
|
||||
|
||||
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, this.multiple)
|
||||
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
||||
let _this = this;
|
||||
callFun = call;
|
||||
|
||||
UTSAndroid.getUniActivity()!.startActivityForResult(intent, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除一个文件条目
|
||||
* @param {string} id - 文件id
|
||||
* @returns {fileListType[]} 最新的文件列表
|
||||
*/
|
||||
remove(id : string) : fileListType[] {
|
||||
|
||||
let index = -1;
|
||||
for (let i = 0; i < filelistAll.length; i++) {
|
||||
let item = filelistAll[i]
|
||||
if (item.id == id) {
|
||||
filelistAll.splice(i, 1)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return filelistAll.map((el) : fileListType => {
|
||||
|
||||
return {
|
||||
name: el.name,
|
||||
type: el.type,
|
||||
id: el.id,
|
||||
uri: '',
|
||||
realFilePath: el.realFilePath,
|
||||
cacheFilePath: el.cacheFilePath,
|
||||
size: el.fileSize,
|
||||
status: el.status
|
||||
} as fileListType
|
||||
});
|
||||
}
|
||||
/**
|
||||
* 返回一个空文件列表数组
|
||||
*/
|
||||
clear() : fileListType[] {
|
||||
filelistAll = [] as xFileSNameTYpe[];
|
||||
this.filelist = [] as xFileSNameTYpe[];
|
||||
return [] as fileListType[]
|
||||
}
|
||||
/**
|
||||
* 读取文件
|
||||
* @description 提供这个函数是因为如果是大文件上传,你可能需要分块上传。由于安卓隐私问题,大文件超过16mb复制回用户区可能会卡或者其它意外事项。
|
||||
* @param {string} id - 文件id
|
||||
* @param {number} size - 读写块的大小,每次读取的大小
|
||||
* @param {Function<byte:ByteArray>} callBack - 读取文件时的回调。
|
||||
*/
|
||||
readFile(item : xFileSListType, size : number | null = null, callBack : null | callReadBackFun = null) : ByteArray {
|
||||
let index = -1;
|
||||
for (let i = 0; i < filelistAll.length; i++) {
|
||||
let item2 = filelistAll[i]
|
||||
if (item.id == item2.id) {
|
||||
index = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (index == -1) {
|
||||
console.error("文件id不存在")
|
||||
return new ByteArray((0).toInt())
|
||||
}
|
||||
let file = filelistAll[index]
|
||||
// 1024
|
||||
|
||||
let sizeBlock = size == null ? 1024 : size!;
|
||||
let context = UTSAndroid.getAppContext() as Context
|
||||
let cover = context.getContentResolver() as ContentResolver
|
||||
let inputStream = cover.openInputStream(file.uri)
|
||||
let byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
// 文件的字节数组
|
||||
let buffer = new ByteArray(sizeBlock.toInt());
|
||||
let n = inputStream!.read(buffer)
|
||||
|
||||
while (-1 != n) {
|
||||
byteArrayOutputStream.write(buffer, 0, n);
|
||||
if (callBack != null) {
|
||||
let tempbuffer = new ByteArray(sizeBlock.toInt());
|
||||
byteArrayOutputStream.write(tempbuffer, 0, n);
|
||||
callBack(tempbuffer)!
|
||||
}
|
||||
n = inputStream!.read(buffer)
|
||||
}
|
||||
let ar = byteArrayOutputStream.toByteArray()
|
||||
try {
|
||||
byteArrayOutputStream.flush()
|
||||
} catch (e) {
|
||||
byteArrayOutputStream.close()
|
||||
}
|
||||
return ar
|
||||
}
|
||||
/**
|
||||
* 获取文件地址
|
||||
* @description 主要是用来你外部上传用,但记住这个内部会放到临时目录中,因此你的地址也是临时在用户中。
|
||||
* @returns {Promise<string>} 返回文件地址
|
||||
*/
|
||||
copyFileToPath(item : xFileSListType, path : string) : Promise<xFileSListType | null> {
|
||||
// let index = -1;
|
||||
// for(let i=0;i<this.filelist.length;i++){
|
||||
// let item = this.filelist[i]
|
||||
// if(item.id==id){
|
||||
// index = i;
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// if(index==-1){
|
||||
// console.error("文件id不存在")
|
||||
// uni.showToast({title:"文件id不存在",icon:'none'})
|
||||
// return Promise.resolve(null)
|
||||
// }
|
||||
|
||||
let file = item
|
||||
if (file.status == 4) {
|
||||
console.error("文件大小超了")
|
||||
// uni.showToast({title:"文件大小超了",icon:'none'})
|
||||
return Promise.resolve(item)
|
||||
}
|
||||
let fileData = this.readFile(item, 1024, null);
|
||||
let filepath = path + "/" + file.name
|
||||
|
||||
|
||||
let realfilepath = UTSAndroid.convert2AbsFullPath(filepath)
|
||||
// let outputFile = new File(filepath)
|
||||
uni.showLoading({ title: '...', mask: true })
|
||||
return new Promise((res, rej) => {
|
||||
try {
|
||||
let outputStream = new FileOutputStream(realfilepath)
|
||||
outputStream.write(fileData);
|
||||
outputStream.flush();
|
||||
uni.hideLoading()
|
||||
file.realFilePath = realfilepath
|
||||
file.cacheFilePath = filepath
|
||||
res({
|
||||
name: file.name,
|
||||
type: file.type,
|
||||
id: file.id,
|
||||
realFilePath: file.realFilePath,
|
||||
cacheFilePath: file.cacheFilePath,
|
||||
status: file.status,
|
||||
size: file.size,
|
||||
uri: file.uri
|
||||
} as xFileSListType)
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
uni.hideLoading()
|
||||
uni.showToast({ title: "异常", icon: 'none' })
|
||||
rej("异常")
|
||||
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"dependencies": {
|
||||
"x_file_s": "./lib/x_file_s.har"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
import { x_xFileSNameTYpe, x_xFileSystem } from "x_file_s"
|
||||
import { fileListType, xFileSListType } from "../interface"
|
||||
|
||||
let HxFilesSytem : x_xFileSystem | null = null;
|
||||
export class xFileSystem {
|
||||
mimeTypeFilter : string[] = []
|
||||
filelist = [] as fileListType[]
|
||||
multiple : boolean = true;
|
||||
maxFileSize = 30 * 1024 * 1024;
|
||||
constructor(typeFilter : string[] | null, tempmultiple : boolean | null, maxSize : number | null) {
|
||||
if (typeFilter != null) {
|
||||
let tms = typeFilter! as string[];
|
||||
this.mimeTypeFilter = tms;
|
||||
}
|
||||
if (tempmultiple != null) {
|
||||
let tm = tempmultiple as boolean;
|
||||
this.multiple = tm
|
||||
}
|
||||
if (maxSize != null) {
|
||||
this.maxFileSize = maxSize!
|
||||
}
|
||||
|
||||
if (HxFilesSytem == null) {
|
||||
HxFilesSytem = new x_xFileSystem(UTSHarmony.getCurrentWindow()!.getUIContext(), this.mimeTypeFilter, this.multiple, this.maxFileSize)
|
||||
} else {
|
||||
HxFilesSytem!.clear();
|
||||
}
|
||||
}
|
||||
destory() {
|
||||
HxFilesSytem = null;
|
||||
this.filelist = [] as fileListType[]
|
||||
}
|
||||
openDocument(callfun : (list : fileListType[]) => void) {
|
||||
if (HxFilesSytem != null) {
|
||||
HxFilesSytem!.openDocument((templist : x_xFileSNameTYpe[]) => {
|
||||
let listfile = templist.map((el) : fileListType => {
|
||||
return {
|
||||
name: el.name,
|
||||
type: el.type,
|
||||
id: el.id,
|
||||
uri: el.uri,
|
||||
realFilePath: el.realFilePath,
|
||||
cacheFilePath: el.cacheFilePath,
|
||||
status: el.status,
|
||||
size: el.fileSize
|
||||
} as fileListType
|
||||
});
|
||||
this.filelist = listfile
|
||||
callfun(listfile)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除一个文件条目
|
||||
* @param {string} id - 文件id
|
||||
* @returns {fileListType[]} 最新的文件列表
|
||||
*/
|
||||
remove(id : string) : fileListType[] {
|
||||
|
||||
const templist = HxFilesSytem!.remove(id)
|
||||
|
||||
let listfile = templist.map((el) : fileListType => {
|
||||
return {
|
||||
name: el.name,
|
||||
type: el.type,
|
||||
id: el.id,
|
||||
uri: el.uri,
|
||||
realFilePath: el.realFilePath,
|
||||
cacheFilePath: el.cacheFilePath,
|
||||
status: el.status,
|
||||
size: el.fileSize
|
||||
} as fileListType
|
||||
});
|
||||
this.filelist = listfile
|
||||
|
||||
return listfile
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回一个空文件列表数组
|
||||
*/
|
||||
clear() : fileListType[] {
|
||||
this.filelist = [] as fileListType[];
|
||||
HxFilesSytem!.clear();
|
||||
return [] as fileListType[]
|
||||
}
|
||||
|
||||
copyFileToPath(item : xFileSListType, path : string) : Promise<xFileSListType | null> {
|
||||
return Promise.resolve(item);
|
||||
}
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>NSPrivacyAccessedAPITypes</key>
|
||||
<array/>
|
||||
<key>NSPrivacyCollectedDataTypes</key>
|
||||
<array/>
|
||||
<key>NSPrivacyTracking</key>
|
||||
<false/>
|
||||
<key>NSPrivacyTrackingDomains</key>
|
||||
<array/>
|
||||
</dict>
|
||||
</plist>
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"deploymentTarget": "11"
|
||||
}
|
||||
@@ -0,0 +1,197 @@
|
||||
import { xFileSListType,fileListType } from "../interface"
|
||||
import { UIDocumentPickerViewController, UIDocumentPickerMode, UIDocumentPickerDelegate, UIViewController } from "UIKit"
|
||||
import { URL, URLResourceKey, URLResourceValues, Data, FileManager } from "Foundation"
|
||||
import { UTType } from "UniformTypeIdentifiers"
|
||||
import Swift from "Swift"
|
||||
|
||||
function getUid(rdix = 1, length = 12) : string {
|
||||
let ix = "";
|
||||
// #ifdef APP
|
||||
ix = Math.floor(Math.random() * rdix * Math.floor(Math.random() * Date.now())).toString().substring(0, length as Int);
|
||||
// #endif
|
||||
return ix;
|
||||
}
|
||||
let filelistAll = [] as fileListType[]
|
||||
export class xFileSystem {
|
||||
|
||||
mimeTypeFilter : string[] = []
|
||||
|
||||
filelist = [] as fileListType[]
|
||||
multiple : boolean = true;
|
||||
actionSeleCtedFiles : ActionsClass | null = null
|
||||
maxFileSize = 30 * 1024 * 1024;
|
||||
constructor(typeFilter : string[] | null, tempmultiple : boolean | null, maxSize : number | null) {
|
||||
if (typeFilter != null) {
|
||||
let tms = typeFilter! as string[];
|
||||
this.mimeTypeFilter = tms;
|
||||
|
||||
}
|
||||
if (tempmultiple != null) {
|
||||
let tm = tempmultiple as boolean;
|
||||
this.multiple = tm
|
||||
}
|
||||
if (maxSize != null) {
|
||||
this.maxFileSize = maxSize!
|
||||
}
|
||||
}
|
||||
destory(){
|
||||
|
||||
}
|
||||
openDocument(callfun : (list : any) => void) {
|
||||
let _this = this;;
|
||||
DispatchQueue.main.async(execute = () : void => {
|
||||
_this.actionSeleCtedFiles = new ActionsClass((files : URL[]) => {
|
||||
let keysToFetch = new Swift.Set<URLResourceKey>();
|
||||
keysToFetch.insert(URLResourceKey.totalFileAllocatedSizeKey)
|
||||
// 获得当前用户的缓存目录路径主目录。
|
||||
let userDir = FileManager.default.urls(for = FileManager.SearchPathDirectory.cachesDirectory, in = FileManager.SearchPathDomainMask.userDomainMask).first!
|
||||
|
||||
for (let item in files) {
|
||||
let ditem = item;
|
||||
|
||||
try {
|
||||
let ds = UTSiOS.try(ditem.resourceValues(forKeys = keysToFetch), "?")
|
||||
|
||||
if (ds != null) {
|
||||
let fSize = ds?.totalFileAllocatedSize ?? 0
|
||||
let fName = ditem.lastPathComponent;
|
||||
let fPath = ditem.absoluteString;
|
||||
let fType = fName.substring((fName.lastIndexOf('.') + 1).toInt())
|
||||
let realFilePath = ''
|
||||
let cacheFilePath = fPath
|
||||
let status = 0
|
||||
let ifleid = getUid(1, 8);
|
||||
let tempath = UTSiOS.getDataPath() as string
|
||||
|
||||
let destinationFileName = ifleid + "." + fType
|
||||
let destinationURL = userDir.appendingPathComponent(destinationFileName)
|
||||
|
||||
let real = UTSiOS.try(FileManager.default.copyItem(at = ditem, to = destinationURL), "?");
|
||||
let itemobj = {
|
||||
name: fName,
|
||||
type: fType,
|
||||
id: ifleid,
|
||||
uri:"",
|
||||
realFilePath: destinationFileName,
|
||||
cacheFilePath: destinationFileName,
|
||||
size: fSize,
|
||||
status: fSize > _this.maxFileSize ? 4 : 0
|
||||
|
||||
} as fileListType;
|
||||
|
||||
if(!_this.isInfilelist(itemobj)){
|
||||
|
||||
_this.filelist.push(itemobj)
|
||||
filelistAll.push(itemobj)
|
||||
}
|
||||
|
||||
}
|
||||
} catch (e) {
|
||||
//TODO handle the exception
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
let list = [] as string[]
|
||||
for (file in _this.filelist) {
|
||||
list.push(JSON.stringify(file)!)
|
||||
}
|
||||
callfun(JSON.stringify(list))
|
||||
_this.filelist = [] as fileListType[]
|
||||
|
||||
})
|
||||
try {
|
||||
if (UTSiOS.available("iOS 14.0, *")) {
|
||||
let filter : UTType[] = [] as UTType[];
|
||||
for (let i = 0; i < _this.mimeTypeFilter.length; i++) {
|
||||
let contentType = new UTType(filenameExtension = _this.mimeTypeFilter[i])
|
||||
if (contentType != null) {
|
||||
filter.push(new UTType(filenameExtension = _this.mimeTypeFilter[i])!)
|
||||
}
|
||||
}
|
||||
if(filter.length==0){
|
||||
filter.push(UTType.item)
|
||||
}
|
||||
|
||||
let documentPickerController = new UIDocumentPickerViewController(forOpeningContentTypes = filter,asCopy = true)
|
||||
documentPickerController.delegate = _this.actionSeleCtedFiles
|
||||
documentPickerController.allowsMultipleSelection = _this.multiple // 是否允许选择多个文件
|
||||
UTSiOS.getCurrentViewController().present(documentPickerController, animated = true, completion = null)
|
||||
} else {
|
||||
let documentPickerController = new UIDocumentPickerViewController(documentTypes = ['public.data'], in = UIDocumentPickerMode.import)
|
||||
documentPickerController.delegate = _this.actionSeleCtedFiles
|
||||
documentPickerController.allowsMultipleSelection = _this.multiple // 是否允许选择多个文件
|
||||
UTSiOS.getCurrentViewController().present(documentPickerController, animated = true, completion = null)
|
||||
}
|
||||
|
||||
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
//读取文件
|
||||
copyFileToPath(item : xFileSListType, basedir : string) : string {
|
||||
// let filepath = ''
|
||||
// for (file in this.filelist) {
|
||||
// if (file.id == id) {
|
||||
// filepath = file.cacheFilePath
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
return item.cacheFilePath;
|
||||
}
|
||||
isInfilelist(file:fileListType):boolean{
|
||||
return filelistAll.some((el):boolean=>{
|
||||
return el.name==file.name&&el.type==file.type
|
||||
})
|
||||
}
|
||||
/**
|
||||
* 返回一个空文件列表数组
|
||||
*/
|
||||
clear():fileListType[]{
|
||||
filelistAll = [] as fileListType[];
|
||||
this.filelist = [] as fileListType[];
|
||||
return [] as fileListType[]
|
||||
}
|
||||
// ios不作删除自行管理缓存文件。
|
||||
remove(id : string) : fileListType[] {
|
||||
let index = -1;
|
||||
for(let i=0;i<filelistAll.length;i++){
|
||||
let item = filelistAll[i]
|
||||
if(item.id==id){
|
||||
filelistAll.splice(i,1)
|
||||
break;
|
||||
}
|
||||
}
|
||||
return filelistAll.map((el):fileListType=>{
|
||||
return {
|
||||
name:el.name,
|
||||
type:el.type,
|
||||
id:el.id,
|
||||
uri:'',
|
||||
realFilePath:el.realFilePath,
|
||||
cacheFilePath:el.cacheFilePath,
|
||||
size:el.size,
|
||||
status:el.status
|
||||
} as fileListType
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
class ActionsClass implements UIDocumentPickerDelegate {
|
||||
callSuccessFiles = (files : URL[]) => { }
|
||||
constructor(call : (files : URL[]) => void) {
|
||||
super()
|
||||
this.callSuccessFiles = call;
|
||||
}
|
||||
documentPicker(controller : UIDocumentPickerViewController, @argumentLabel("didPickDocumentsAt") urls : URL[]) {
|
||||
|
||||
this.callSuccessFiles(urls)
|
||||
}
|
||||
documentPickerWasCancelled(controller : UIDocumentPickerViewController) {
|
||||
console.log("User cancelled the document picker")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
export type xFileSListType = {
|
||||
name:string,
|
||||
type:string,
|
||||
id:string,
|
||||
uri:string,
|
||||
/** web平台文件对象 */
|
||||
file?:any,
|
||||
/** 个人用户自定数据 */
|
||||
request?:any,
|
||||
/** 安卓机/ios机下面的真实sd路径 */
|
||||
realFilePath:string,
|
||||
/** uniapp应用协议文件路径,可用uni.getFileSystemManager进行删除或者查询到 */
|
||||
cacheFilePath:string,
|
||||
/** 文件大小 */
|
||||
size?:number
|
||||
/**
|
||||
* 0等待,1失败,2成功,3上传中,4超过大小。
|
||||
*/
|
||||
status?:0|1|2|3|4
|
||||
}
|
||||
|
||||
// #ifndef MP
|
||||
export type fileListType = xFileSListType
|
||||
// #endif
|
||||
|
||||
// #ifdef MP
|
||||
export type fileListType = any
|
||||
// #endif
|
||||
@@ -0,0 +1,123 @@
|
||||
|
||||
export { xFileSListType } from "../interface"
|
||||
export type fileListType = any;
|
||||
|
||||
type xFileSNameTYpe = {
|
||||
name:string,
|
||||
type:string,
|
||||
uri:string,
|
||||
id:string,
|
||||
file:any,
|
||||
size:number,
|
||||
status:number
|
||||
}
|
||||
// type callReadBackFun = (byte:ByteArray)=>void
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 由于安卓隐私的问题。实际得到的所有文件 其实 都不是真实地址。因此你想要得到真实的地址,你就得复制到你的应用沙盒中。
|
||||
*/
|
||||
export class xFileSystem {
|
||||
mimeTypeFilter = [] as string[];
|
||||
filelist = [] as xFileSNameTYpe[]
|
||||
inputDom:null|HTMLInputElement = null;
|
||||
multiple:boolean = true;
|
||||
maxFileSize:number = 30*1024*1024
|
||||
constructor(typeFilter:string|null,tempmultiple:boolean|null,maxSize:number|null){
|
||||
if(typeFilter!=null){
|
||||
|
||||
let filter : string[] = [] as string[];
|
||||
let tms = typeFilter! as string[]
|
||||
for (let i = 0; i < tms.length; i++) {
|
||||
filter.push("."+tms[i])
|
||||
}
|
||||
if(filter.length==0){
|
||||
filter.push("*/*")
|
||||
}
|
||||
|
||||
this.mimeTypeFilter = filter
|
||||
}
|
||||
if(tempmultiple!=null){
|
||||
this.multiple = tempmultiple!
|
||||
}
|
||||
if(maxSize!=null){
|
||||
this.maxFileSize = maxSize!
|
||||
}
|
||||
}
|
||||
isInfilelist(file:xFileSNameTYpe):boolean{
|
||||
return this.filelist.some((el):boolean=>{
|
||||
return el.name==file.name&&el.type==file.type
|
||||
})
|
||||
}
|
||||
destory(){
|
||||
|
||||
}
|
||||
openDocument(call:(list:xFileSListType[])=>void){
|
||||
|
||||
let _this = this;
|
||||
let filetype = this.mimeTypeFilter[0].split('/')[0]
|
||||
if(filetype=='*'){
|
||||
filetype = 'all'
|
||||
}
|
||||
|
||||
wx.chooseMessageFile({
|
||||
count:100,
|
||||
type:filetype,
|
||||
success(result) {
|
||||
let filelist = result.tempFiles
|
||||
for(let i=0;i<filelist.length;i++){
|
||||
let item = filelist[i];
|
||||
let fileitem = {
|
||||
name:item.name,
|
||||
type:item.type.substring(item.type.lastIndexOf("/")+1),
|
||||
uri:"",
|
||||
id:Math.floor(Math.random() * 1 * Math.floor(Math.random() * Date.now())).toString().substring(0, 10),
|
||||
file:item,
|
||||
size:item.size,
|
||||
status:item.size>_this.maxFileSize?4:0,
|
||||
} as xFileSNameTYpe
|
||||
if(!_this.isInfilelist(fileitem)){
|
||||
_this.filelist.push(fileitem)
|
||||
}
|
||||
}
|
||||
call(_this.filelist.map((el)=>{
|
||||
return {
|
||||
name:el.name,
|
||||
type:el.type,
|
||||
id:el.id,
|
||||
request:null,
|
||||
realFilePath:el.file.path,
|
||||
cacheFilePath:el.file.path,
|
||||
size:el.size,
|
||||
status:el.status
|
||||
}
|
||||
}))
|
||||
},
|
||||
fail(er) {
|
||||
console.error(er)
|
||||
}
|
||||
})
|
||||
}
|
||||
/**
|
||||
* 删除一个文件条目
|
||||
* @param {string} id - 文件id
|
||||
* @returns {fileListType[]} 最新的文件列表
|
||||
*/
|
||||
remove(id:string):xFileSListType[]{
|
||||
|
||||
return this.filelist.filter(el=>el.id!=id)
|
||||
|
||||
}
|
||||
/**
|
||||
* 返回一个空文件列表数组
|
||||
*/
|
||||
clear():xFileSListType[]{
|
||||
this.filelist = [] as xFileSNameTYpe[];
|
||||
return [] as xFileSListType[]
|
||||
}
|
||||
copyFileToPath(item:xFileSListType,path:string):Promise<xFileSListType|null>{
|
||||
return Promise.resolve(item)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
|
||||
|
||||
export { xFileSListType } from "../interface"
|
||||
|
||||
export type fileListType = xFileSListType;
|
||||
|
||||
|
||||
type xFileSNameTYpe = {
|
||||
name:string,
|
||||
type:string,
|
||||
uri:string,
|
||||
id:string,
|
||||
file:any,
|
||||
size:number,
|
||||
status:number
|
||||
}
|
||||
// type callReadBackFun = (byte:ByteArray)=>void
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 由于安卓隐私的问题。实际得到的所有文件 其实 都不是真实地址。因此你想要得到真实的地址,你就得复制到你的应用沙盒中。
|
||||
*/
|
||||
export class xFileSystem {
|
||||
mimeTypeFilter = [] as string[];
|
||||
filelist = [] as xFileSNameTYpe[]
|
||||
inputDom:null|HTMLInputElement = null;
|
||||
multiple:boolean = true;
|
||||
maxFileSize:number = 30*1024*1024
|
||||
constructor(typeFilter:string|null,tempmultiple:boolean|null,maxSize:number|null){
|
||||
if(typeFilter!=null){
|
||||
|
||||
let filter : string[] = [] as string[];
|
||||
let tms = typeFilter! as string[]
|
||||
for (let i = 0; i < tms.length; i++) {
|
||||
filter.push("."+tms[i])
|
||||
}
|
||||
if(filter.length==0){
|
||||
filter.push("*/*")
|
||||
}
|
||||
|
||||
this.mimeTypeFilter = filter
|
||||
}
|
||||
if(tempmultiple!=null){
|
||||
this.multiple = tempmultiple!
|
||||
}
|
||||
if(maxSize!=null){
|
||||
this.maxFileSize = maxSize!
|
||||
}
|
||||
}
|
||||
isInfilelist(file:xFileSNameTYpe):boolean{
|
||||
return this.filelist.some((el):boolean=>{
|
||||
return el.name==file.name&&el.type==file.type
|
||||
})
|
||||
}
|
||||
destory(){
|
||||
|
||||
}
|
||||
openDocument(call:(list:fileListType[])=>void){
|
||||
if(this.inputDom!=null){
|
||||
this.inputDom!.remove()
|
||||
}
|
||||
let _this = this;
|
||||
let inputdom = document.createElement("input")
|
||||
inputdom.type = 'file';
|
||||
inputdom.accept = this.mimeTypeFilter.join(", ");
|
||||
inputdom.style.setProperty('position','absolute')
|
||||
inputdom.style.setProperty('left','-200%')
|
||||
inputdom.style.setProperty('top','-200%')
|
||||
inputdom.style.setProperty('opacity','0')
|
||||
if(this.multiple){
|
||||
inputdom.multiple = "multiple"
|
||||
}
|
||||
inputdom.onchange = function(evt){
|
||||
let filelist = inputdom.files||[] as File[];
|
||||
for(let i=0;i<filelist.length;i++){
|
||||
let item = filelist[i];
|
||||
|
||||
let fileitem = {
|
||||
name:item.name,
|
||||
type:item.type.substring(item.type.lastIndexOf("/")+1),
|
||||
uri:"",
|
||||
id:Math.floor(Math.random() * 1 * Math.floor(Math.random() * Date.now())).toString().substring(0, 10),
|
||||
file:item
|
||||
size:item.size,
|
||||
status:item.size>_this.maxFileSize?4:0
|
||||
} as xFileSNameTYpe
|
||||
if(!_this.isInfilelist(fileitem)){
|
||||
_this.filelist.push(fileitem)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
call(_this.filelist.map((el)=>{
|
||||
return {
|
||||
name:el.name,
|
||||
type:el.type,
|
||||
file:el.file,
|
||||
id:el.id,
|
||||
request:null,
|
||||
realFilePath:"",
|
||||
cacheFilePath:"",
|
||||
size:el.size,
|
||||
status:el.size>_this.maxFileSize?4:0
|
||||
}
|
||||
}))
|
||||
}
|
||||
inputdom.click()
|
||||
}
|
||||
/**
|
||||
* 删除一个文件条目
|
||||
* @param {string} id - 文件id
|
||||
* @returns {fileListType[]} 最新的文件列表
|
||||
*/
|
||||
remove(id:string):fileListType[]{
|
||||
return [] as fileListType[]
|
||||
}
|
||||
/**
|
||||
* 返回一个空文件列表数组
|
||||
*/
|
||||
clear():fileListType[]{
|
||||
this.filelist = [] as xFileSNameTYpe[];
|
||||
return [] as fileListType[]
|
||||
}
|
||||
copyFileToPath(item:fileListType,path:string):Promise<fileListType|null>{
|
||||
return Promise.resolve(item)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user