index.uts 72.9 KB
Newer Older
DCloud-yyl's avatar
DCloud-yyl 已提交
1 2 3 4 5
import { WriteFileOptions, ReadFileOptions, MkDirOptions, RmDirOptions, TruncateFileOptions, UnLinkOptions, ReadDirOptions, AccessOptions, RenameOptions, GetFileInfoOptions, CopyFileOptions, StatOptions, AppendFileOptions, OpenFileOptions, OpenFileSuccessCallback, OpenFileSuccessResult, SaveFileOptions, UnzipFileOptions, GetSavedFileListOptions, ReadCompressedFileOptions, ReadCompressedFileResult, RemoveSavedFileOptions, WriteOptions, WriteResult, OpenFileSyncOptions, WriteSyncOptions, CloseOptions, CloseSyncOptions, FStatOptions, FStatSuccessResult, FTruncateFileOptions, FTruncateFileSyncOptions, FStatSyncOptions, ReadZipEntryOptions, ZipFileItem, ReadZipEntryCallback, EntriesResult } from "../interface.uts"
import { ReadFileSuccessResult, FileManagerSuccessResult, ReadDirSuccessResult, AccessSuccessResult, SaveFileSuccessResult, GetFileInfoSuccessResult, StatSuccessResult, FileStats, Stats, GetSavedFileListResult } from "../interface.uts"
import { GetFileSystemManager, FileSystemManager } from "../interface.uts"
import { FileSystemManagerFailImpl, FileSystemManagerUniErrorSubject, FileSystemManagerUniErrors, UniErrors } from "../unierror.uts"
import { FileDescriptorUtil } from "./FileDescriptorUtil"
DCloud-yyl's avatar
DCloud-yyl 已提交
6 7 8 9 10
import File from "java.io.File"
import Base64 from "android.util.Base64"
import MessageDigest from 'java.security.MessageDigest';
import FileInputStream from 'java.io.FileInputStream';
import Charsets from "kotlin.text.Charsets"
DCloud-yyl's avatar
DCloud-yyl 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
import FileOutputStream from 'java.io.FileOutputStream';
import ZipInputStream from 'java.util.zip.ZipInputStream';
import BufferedInputStream from 'java.io.BufferedInputStream';
import BrotliInputStream from 'org.brotli.dec.BrotliInputStream'
import Build from 'android.os.Build'
import Environment from 'android.os.Environment'
import Option from 'android.app.VoiceInteractor.PickOptionRequest.Option';
import FileDescriptor from 'java.io.FileDescriptor';
import ParcelFileDescriptor from 'android.os.ParcelFileDescriptor';
import ByteBuffer from 'java.nio.ByteBuffer';
import InputStream from 'java.io.InputStream';

class AndroidStats implements Stats, io.dcloud.uts.log.LogSelf, io.dcloud.uts.json.IJsonStringify {
	/**
		 * 文件的类型和存取的权限,对应 POSIX stat.st_mode
		 * 注意android中,文件类型只包含是否是目录与文件,
		 * 另外在android中这里的权限指的是当前进程对文件或者文件夹是否有读,写,执行的权限,
		 * 这里没有与 POSIX stat.st_mode对应的组,其他人等相关权限的数据返回,只有所有者的相关权限
		 */
	mode : number = 0;
	/**
	 * 文件大小,单位:B,对应 POSIX stat.st_size
	 */
	size : number = 0;
	/**
	 * 文件最近一次被存取或被执行的时间,UNIX 时间戳,对应 POSIX stat.st_atime
	 * 注意:android中由于系统限制无法获取该数据
	 */
	lastAccessedTime : number = 0;
	/**
	 * 文件最后一次被修改的时间,UNIX 时间戳,对应 POSIX stat.st_mtime
	 */
	lastModifiedTime : number = 0;
	/**
	 * @internal
	 */
	mIsFile : boolean = false;

	constructor() {
	}
	/**
	 * 判断当前文件是否一个目录
	 */
	isDirectory() : boolean {
		return !this.mIsFile
	}
	/**
	 * 判断当前文件是否一个普通文件
	 */
	isFile() : boolean {
		return this.mIsFile
	}
	/**
	 * @internal
	 */
	override toLog() : any | null {
		return this.toJSON()
	}
	/**
	 * @internal
	 */
	override toJSON() : any | null {
		let jsonRet = new UTSJSONObject()
		jsonRet.set("mode", this.mode)
		jsonRet.set("size", this.size)
		jsonRet.set("lastAccessedTime", this.lastAccessedTime)
		jsonRet.set("lastModifiedTime", this.lastModifiedTime)
		return jsonRet
	}
DCloud-yyl's avatar
DCloud-yyl 已提交
80

DCloud-yyl's avatar
DCloud-yyl 已提交
81 82 83 84 85 86 87 88 89 90 91
}
class AndroidFileSystemManager implements FileSystemManager {
	fileDesUtil : FileDescriptorUtil = new FileDescriptorUtil()
	private static manager : AndroidFileSystemManager = new AndroidFileSystemManager()
	private constructor() { }
	public static getManager() : AndroidFileSystemManager {
		return this.manager;
	}
	public stat(options : StatOptions) {
		let currentDispatcher = UTSAndroid.getDispatcher("main")
		UTSAndroid.getDispatcher('io').async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
92

DCloud-yyl's avatar
DCloud-yyl 已提交
93
			currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
94 95

				let filePath = UTSAndroid.convert2AbsFullPath(options.path)
DCloud-yyl's avatar
DCloud-yyl 已提交
96 97

				let isSandyBox = isSandyBoxPath(filePath, true)
DCloud-yyl's avatar
DCloud-yyl 已提交
98
				if (!isSandyBox) {
DCloud-yyl's avatar
DCloud-yyl 已提交
99 100
					currentDispatcher.async(function (_) {
						let err = new FileSystemManagerFailImpl(1300013);
DCloud-yyl's avatar
DCloud-yyl 已提交
101 102
						options.fail?.(err)
						options.complete?.(err)
DCloud-yyl's avatar
DCloud-yyl 已提交
103
					}, null)
DCloud-yyl's avatar
DCloud-yyl 已提交
104 105
					return
				}
DCloud-yyl's avatar
DCloud-yyl 已提交
106

DCloud-yyl's avatar
DCloud-yyl 已提交
107
				let targetFile = new File(filePath)
DCloud-yyl's avatar
DCloud-yyl 已提交
108

DCloud-yyl's avatar
DCloud-yyl 已提交
109 110 111 112
				if (!targetFile.exists()) {
					/**
					 * 文件不存在
					 */
DCloud-yyl's avatar
DCloud-yyl 已提交
113 114
					currentDispatcher.async(function (_) {
						let err = new FileSystemManagerFailImpl(1300002);
DCloud-yyl's avatar
DCloud-yyl 已提交
115 116
						options.fail?.(err)
						options.complete?.(err)
DCloud-yyl's avatar
DCloud-yyl 已提交
117
					}, null)
DCloud-yyl's avatar
DCloud-yyl 已提交
118 119
					return
				}
DCloud-yyl's avatar
DCloud-yyl 已提交
120

DCloud-yyl's avatar
DCloud-yyl 已提交
121 122
				if (options.recursive == true && targetFile.isDirectory()) {
					// 如果当前是目录,并且设置 则需要遍历所有子目录
DCloud-yyl's avatar
DCloud-yyl 已提交
123
					let res : Array<FileStats> = []
DCloud-yyl's avatar
DCloud-yyl 已提交
124 125 126 127
					/**
					 * 与文件不同的是,需要迭代下属的文件
					 */
					targetFile.walk()
DCloud-yyl's avatar
DCloud-yyl 已提交
128 129
						.onEnter(function (file : File) : boolean {
							if (file.isDirectory()) {
DCloud-yyl's avatar
DCloud-yyl 已提交
130 131 132 133 134
								return true
							}
							return false
						})
						.iterator()
DCloud-yyl's avatar
DCloud-yyl 已提交
135 136 137 138 139
						.forEach(function (file : File) {
							let perFileStats : FileStats = {
								path: file.getPath(),
								stats: wrapStats(file)
							}
DCloud-yyl's avatar
DCloud-yyl 已提交
140 141
							res.add(perFileStats)
						})
DCloud-yyl's avatar
DCloud-yyl 已提交
142 143 144 145 146 147


					let success : StatSuccessResult = {
						errMsg: "stat:ok",
						stats: res
					}
DCloud-yyl's avatar
DCloud-yyl 已提交
148 149
					options.success?.(success)
					options.complete?.(success)
DCloud-yyl's avatar
DCloud-yyl 已提交
150 151 152


				} else {
DCloud-yyl's avatar
DCloud-yyl 已提交
153 154 155
					// 默认是false,只读取当前一个文件
					let mode = 0
					let filesize = 0;
DCloud-yyl's avatar
DCloud-yyl 已提交
156 157 158 159 160 161 162 163 164 165 166

					let rootFileStats : FileStats = {
						path: targetFile.getPath(),
						stats: wrapStats(targetFile)
					}
					let res : Array<FileStats> = [rootFileStats]
					let success : StatSuccessResult = {
						errMsg: "stat:ok",
						stats: res
					}
					//new StatSuccessResult("stat:ok", res)
DCloud-yyl's avatar
DCloud-yyl 已提交
167 168 169
					options.success?.(success)
					options.complete?.(success)
				}
DCloud-yyl's avatar
DCloud-yyl 已提交
170 171 172 173


			}, null)
		}, null)
DCloud-yyl's avatar
DCloud-yyl 已提交
174 175
	}

DCloud-yyl's avatar
DCloud-yyl 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
	public statSync(path : string, recursive : boolean) : FileStats[] {
		let msgPrefix = "statSync:fail "
		let filePath = UTSAndroid.convert2AbsFullPath(path)
		let tempRecursive = recursive
		let isSandyBox = isSandyBoxPath(filePath, true)
		if (!isSandyBox) {
			throw new UniError(FileSystemManagerUniErrorSubject, 1300013, msgPrefix + FileSystemManagerUniErrors.get(1300013)!)
		}
		let targetFile = new File(filePath)
		if (!targetFile.exists()) {
			/**
			 * 文件不存在
			 */
			throw new UniError(FileSystemManagerUniErrorSubject, 1300002, msgPrefix + FileSystemManagerUniErrors.get(1300002)!);
		}
		if (tempRecursive == true && targetFile.isDirectory()) {
			// 如果当前是目录,并且设置 则需要遍历所有子目录
			let res : Array<FileStats> = []
			/**
			 * 与文件不同的是,需要迭代下属的文件
			 */
			targetFile.walk()
				.onEnter(function (file : File) : boolean {
					if (file.isDirectory()) {
						return true
					}
					return false
				})
				.iterator()
				.forEach(function (file : File) {
					let perFileStats : FileStats = {
						path: file.getPath(),
						stats: wrapStats(file)
					}
					res.add(perFileStats)
				})

			return res
		} else {
			// 默认是false,只读取当前一个文件
			let mode = 0
			let filesize = 0;

			let rootFileStats : FileStats = {
				path: targetFile.getPath(),
				stats: wrapStats(targetFile)
			}
			return [rootFileStats]
		}
	}

	public getFileInfo(options : GetFileInfoOptions) {

		let currentDispatcher = UTSAndroid.getDispatcher("main")
		UTSAndroid.getDispatcher('io').async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
231 232 233

			let filePath = UTSAndroid.convert2AbsFullPath(options.filePath)

DCloud-yyl's avatar
DCloud-yyl 已提交
234
			let isSandyBox = isSandyBoxPath(filePath, true)
DCloud-yyl's avatar
DCloud-yyl 已提交
235
			if (!isSandyBox) {
DCloud-yyl's avatar
DCloud-yyl 已提交
236 237
				currentDispatcher.async(function (_) {
					let err = new FileSystemManagerFailImpl(1300013);
DCloud-yyl's avatar
DCloud-yyl 已提交
238 239
					options.fail?.(err)
					options.complete?.(err)
DCloud-yyl's avatar
DCloud-yyl 已提交
240
				}, null)
DCloud-yyl's avatar
DCloud-yyl 已提交
241 242 243 244 245 246 247 248 249
				return
			}

			let targetFile = new File(filePath)

			if (!targetFile.exists()) {
				/**
				 * 文件不存在
				 */
DCloud-yyl's avatar
DCloud-yyl 已提交
250 251
				currentDispatcher.async(function (_) {
					let err = new FileSystemManagerFailImpl(1300002);
DCloud-yyl's avatar
DCloud-yyl 已提交
252 253
					options.fail?.(err)
					options.complete?.(err)
DCloud-yyl's avatar
DCloud-yyl 已提交
254
				}, null)
DCloud-yyl's avatar
DCloud-yyl 已提交
255 256
				return
			}
DCloud-yyl's avatar
DCloud-yyl 已提交
257

DCloud-yyl's avatar
DCloud-yyl 已提交
258 259 260 261
			if (targetFile.isDirectory()) {
				/**
				 * 文件是个目录
				 */
DCloud-yyl's avatar
DCloud-yyl 已提交
262 263
				currentDispatcher.async(function (_) {
					let err = new FileSystemManagerFailImpl( 1300021);
DCloud-yyl's avatar
DCloud-yyl 已提交
264 265
					options.fail?.(err)
					options.complete?.(err)
DCloud-yyl's avatar
DCloud-yyl 已提交
266
				}, null)
DCloud-yyl's avatar
DCloud-yyl 已提交
267 268
				return
			}
DCloud-yyl's avatar
DCloud-yyl 已提交
269 270

			if (options.digestAlgorithm == null) {
DCloud-yyl's avatar
DCloud-yyl 已提交
271 272 273 274 275 276
				options.digestAlgorithm = "md5"
			}
			if (options.digestAlgorithm!.toLowerCase() != 'md5' && options.digestAlgorithm!.toLowerCase() != 'sha1') {
				/**
				 * invalid digestAlgorithm
				 */
DCloud-yyl's avatar
DCloud-yyl 已提交
277 278
				currentDispatcher.async(function (_) {
					let err = new FileSystemManagerFailImpl(1300022);
DCloud-yyl's avatar
DCloud-yyl 已提交
279 280
					options.fail?.(err)
					options.complete?.(err)
DCloud-yyl's avatar
DCloud-yyl 已提交
281
				}, null)
DCloud-yyl's avatar
DCloud-yyl 已提交
282 283 284 285 286 287 288 289 290 291 292 293 294 295
				return
			}


			let md5Digest = MessageDigest.getInstance(options.digestAlgorithm!)

			let fileLen = targetFile.length()
			// 使用缓冲区读取文件内容
			let buffer = new ByteArray(8192)
			let fis = new FileInputStream(targetFile)

			let len = 0
			while (len != -1) {
				len = fis.read(buffer)
DCloud-yyl's avatar
DCloud-yyl 已提交
296
				if (len != -1) {
DCloud-yyl's avatar
DCloud-yyl 已提交
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
					md5Digest.update(buffer, 0, len.toInt())
				}
			}
			fis.close();
			let digestByte = md5Digest.digest();
			// byte to string
			let strHexString = new StringBuffer();
			//  byte buffer
			for (let i = 0; i < digestByte.size; i++) {
				// 取0xff部分
				let hex = Integer.toHexString(0xff & digestByte[i.toInt()].toInt());
				if (hex.length == 1) {
					strHexString.append('0');
				}
				strHexString.append(hex);
			}
			// 得到返回結果
			let sign = strHexString.toString();

DCloud-yyl's avatar
DCloud-yyl 已提交
316 317 318 319 320 321
			currentDispatcher.async(function (_) {
				let success : GetFileInfoSuccessResult = {
					digest: sign,
					size: fileLen,
					errMsg: "getFileInfo:ok"
				}
DCloud-yyl's avatar
DCloud-yyl 已提交
322 323
				options.success?.(success)
				options.complete?.(success)
DCloud-yyl's avatar
DCloud-yyl 已提交
324 325
			}, null)
		}, null)
DCloud-yyl's avatar
DCloud-yyl 已提交
326 327 328 329

	}


DCloud-yyl's avatar
DCloud-yyl 已提交
330 331 332
	public copyFileSync(srcPath : string, destPath : string) : void {
		// 检查来源文件
		let msgPrefix = "copyFileSync:fail "
DCloud-yyl's avatar
DCloud-yyl 已提交
333
		let filePath = UTSAndroid.convert2AbsFullPath(srcPath)
DCloud-yyl's avatar
DCloud-yyl 已提交
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
		if (filePath.startsWith("/android_asset/")) {
			// 用户访问的是asset 路径,此时不是释放模式
			let byteArray : ByteArray
			try {
				let assetName = filePath.substring("/android_asset/".length)
				let assetStream = UTSAndroid.getAppContext()!.getResources().getAssets().open(assetName);
				let byteLen = assetStream.available();
				byteArray = new ByteArray(byteLen)
				assetStream.read(byteArray);
			} catch (e) {
				throw new UniError(FileSystemManagerUniErrorSubject, 1300201, msgPrefix + FileSystemManagerUniErrors.get(1300201)!);
			}
			// 检查目标文件
			let newFilePath = UTSAndroid.convert2AbsFullPath(destPath)
			let isSandyBox = isSandyBoxPath(newFilePath, false)
			if (!isSandyBox) {
				throw new UniError(FileSystemManagerUniErrorSubject, 1300013, msgPrefix + FileSystemManagerUniErrors.get(1300013)!);
			}
			let newFile = new File(newFilePath)
			if (newFile.getParentFile() != null && !newFile.getParentFile()!.exists()) {
				/**
				 * 父文件不存在
				 */
				throw new UniError(FileSystemManagerUniErrorSubject, 1300002, msgPrefix + FileSystemManagerUniErrors.get(1300002)!)
			}
			newFile.writeBytes(byteArray)
			if (!newFile.exists()) {
				// 调用系统api 失败
				throw new UniError(FileSystemManagerUniErrorSubject, 1300201, msgPrefix + FileSystemManagerUniErrors.get(1300201)!)
			}

		} else {
			let isSandyBox = isSandyBoxPath(filePath, true)
			if (!isSandyBox) {
				throw new UniError(FileSystemManagerUniErrorSubject, 1300013, msgPrefix + FileSystemManagerUniErrors.get(1300013)!)
			}
			let targetFile = new File(filePath)
			if (!targetFile.exists()) {
				/**
				 * 文件不存在
				 */
				throw new UniError(FileSystemManagerUniErrorSubject, 1300002, msgPrefix + FileSystemManagerUniErrors.get(1300002)!)
			}
			// 检查目标文件
			let newFilePath = UTSAndroid.convert2AbsFullPath(destPath)
			isSandyBox = isSandyBoxPath(newFilePath, false)
			if (!isSandyBox) {
				throw new UniError(FileSystemManagerUniErrorSubject, 1300013, msgPrefix + FileSystemManagerUniErrors.get(1300013)!)
			}
			let newFile = new File(newFilePath)
			if (newFile.getParentFile() != null && !newFile.getParentFile()!.exists()) {
				/**
				 * 父文件不存在
				 */
				throw new UniError(FileSystemManagerUniErrorSubject, 1300002, msgPrefix + FileSystemManagerUniErrors.get(1300002)!)
			}

			let copyRetFile = targetFile.copyTo(newFile, true)
			if (!copyRetFile.exists()) {
				// 调用系统api 失败
				throw new UniError(FileSystemManagerUniErrorSubject, 1300201, msgPrefix + FileSystemManagerUniErrors.get(1300201)!)
			}
DCloud-yyl's avatar
DCloud-yyl 已提交
396 397 398 399
		}
	}


DCloud-yyl's avatar
DCloud-yyl 已提交
400
	public copyFile(options : CopyFileOptions) {
DCloud-yyl's avatar
DCloud-yyl 已提交
401

DCloud-yyl's avatar
DCloud-yyl 已提交
402 403
		let currentDispatcher = UTSAndroid.getDispatcher("main")
		UTSAndroid.getDispatcher('io').async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
404 405 406

			// 检查来源文件
			let filePath = UTSAndroid.convert2AbsFullPath(options.srcPath)
DCloud-yyl's avatar
DCloud-yyl 已提交
407 408

			if (filePath.startsWith("/android_asset/")) {
DCloud-yyl's avatar
DCloud-yyl 已提交
409
				// 用户访问的是asset 路径,此时不是释放模式
DCloud-yyl's avatar
DCloud-yyl 已提交
410 411

				let exceptionInfo : Exception | null = null
DCloud-yyl's avatar
DCloud-yyl 已提交
412 413 414 415 416 417 418
				let byteArray = new ByteArray(0)
				try {
					let assetName = filePath.substring("/android_asset/".length)
					let assetStream = UTSAndroid.getAppContext()!.getResources().getAssets().open(assetName);
					let byteLen = assetStream.available();
					byteArray = new ByteArray(byteLen)
					assetStream.read(byteArray);
DCloud-yyl's avatar
DCloud-yyl 已提交
419
				} catch (e : Exception) {
DCloud-yyl's avatar
DCloud-yyl 已提交
420 421
					exceptionInfo = e
				}
DCloud-yyl's avatar
DCloud-yyl 已提交
422 423 424 425

				if (exceptionInfo != null) {
					currentDispatcher.async(function (_) {
						let err = new FileSystemManagerFailImpl(1300201);
DCloud-yyl's avatar
DCloud-yyl 已提交
426 427
						options.fail?.(err)
						options.complete?.(err)
DCloud-yyl's avatar
DCloud-yyl 已提交
428 429
					}, null)
					return
DCloud-yyl's avatar
DCloud-yyl 已提交
430
				}
DCloud-yyl's avatar
DCloud-yyl 已提交
431 432


DCloud-yyl's avatar
DCloud-yyl 已提交
433 434
				// 检查目标文件
				let newFilePath = UTSAndroid.convert2AbsFullPath(options.destPath)
DCloud-yyl's avatar
DCloud-yyl 已提交
435
				let isSandyBox = isSandyBoxPath(newFilePath, false)
DCloud-yyl's avatar
DCloud-yyl 已提交
436
				if (!isSandyBox) {
DCloud-yyl's avatar
DCloud-yyl 已提交
437 438
					currentDispatcher.async(function (_) {
						let err = new FileSystemManagerFailImpl(1300013);
DCloud-yyl's avatar
DCloud-yyl 已提交
439 440
						options.fail?.(err)
						options.complete?.(err)
DCloud-yyl's avatar
DCloud-yyl 已提交
441
					}, null)
DCloud-yyl's avatar
DCloud-yyl 已提交
442 443 444
					return
				}
				let newFile = new File(newFilePath)
DCloud-yyl's avatar
DCloud-yyl 已提交
445
				if (newFile.getParentFile() != null && !newFile.getParentFile()!.exists()) {
DCloud-yyl's avatar
DCloud-yyl 已提交
446 447 448
					/**
					 * 父文件不存在
					 */
DCloud-yyl's avatar
DCloud-yyl 已提交
449 450
					currentDispatcher.async(function (_) {
						let err = new FileSystemManagerFailImpl(1300002);
DCloud-yyl's avatar
DCloud-yyl 已提交
451 452
						options.fail?.(err)
						options.complete?.(err)
DCloud-yyl's avatar
DCloud-yyl 已提交
453
					}, null)
DCloud-yyl's avatar
DCloud-yyl 已提交
454 455 456 457 458
					return
				}
				newFile.writeBytes(byteArray)
				if (!newFile.exists()) {
					// 调用系统api 失败
DCloud-yyl's avatar
DCloud-yyl 已提交
459 460
					currentDispatcher.async(function (_) {
						let err = new FileSystemManagerFailImpl(1300201);
DCloud-yyl's avatar
DCloud-yyl 已提交
461 462
						options.fail?.(err)
						options.complete?.(err)
DCloud-yyl's avatar
DCloud-yyl 已提交
463
					}, null)
DCloud-yyl's avatar
DCloud-yyl 已提交
464 465
					return
				}
DCloud-yyl's avatar
DCloud-yyl 已提交
466 467 468 469 470

				currentDispatcher.async(function (_) {
					let success : FileManagerSuccessResult = {
						errMsg: "copyFile:ok"
					}
DCloud-yyl's avatar
DCloud-yyl 已提交
471 472
					options.success?.(success)
					options.complete?.(success)
DCloud-yyl's avatar
DCloud-yyl 已提交
473 474 475 476
				}, null)

			} else {
				let isSandyBox = isSandyBoxPath(filePath, true)
DCloud-yyl's avatar
DCloud-yyl 已提交
477
				if (!isSandyBox) {
DCloud-yyl's avatar
DCloud-yyl 已提交
478 479
					currentDispatcher.async(function (_) {
						let err = new FileSystemManagerFailImpl(1300013);
DCloud-yyl's avatar
DCloud-yyl 已提交
480 481
						options.fail?.(err)
						options.complete?.(err)
DCloud-yyl's avatar
DCloud-yyl 已提交
482
					}, null)
DCloud-yyl's avatar
DCloud-yyl 已提交
483 484 485 486 487 488 489
					return
				}
				let targetFile = new File(filePath)
				if (!targetFile.exists()) {
					/**
					 * 文件不存在
					 */
DCloud-yyl's avatar
DCloud-yyl 已提交
490 491
					currentDispatcher.async(function (_) {
						let err = new FileSystemManagerFailImpl(1300002);
DCloud-yyl's avatar
DCloud-yyl 已提交
492 493
						options.fail?.(err)
						options.complete?.(err)
DCloud-yyl's avatar
DCloud-yyl 已提交
494
					}, null)
DCloud-yyl's avatar
DCloud-yyl 已提交
495 496 497 498
					return
				}
				// 检查目标文件
				let newFilePath = UTSAndroid.convert2AbsFullPath(options.destPath)
DCloud-yyl's avatar
DCloud-yyl 已提交
499
				isSandyBox = isSandyBoxPath(newFilePath, false)
DCloud-yyl's avatar
DCloud-yyl 已提交
500
				if (!isSandyBox) {
DCloud-yyl's avatar
DCloud-yyl 已提交
501 502
					currentDispatcher.async(function (_) {
						let err = new FileSystemManagerFailImpl(1300013);
DCloud-yyl's avatar
DCloud-yyl 已提交
503 504
						options.fail?.(err)
						options.complete?.(err)
DCloud-yyl's avatar
DCloud-yyl 已提交
505
					}, null)
DCloud-yyl's avatar
DCloud-yyl 已提交
506 507 508
					return
				}
				let newFile = new File(newFilePath)
DCloud-yyl's avatar
DCloud-yyl 已提交
509
				if (newFile.getParentFile() != null && !newFile.getParentFile()!.exists()) {
DCloud-yyl's avatar
DCloud-yyl 已提交
510 511 512
					/**
					 * 父文件不存在
					 */
DCloud-yyl's avatar
DCloud-yyl 已提交
513 514
					currentDispatcher.async(function (_) {
						let err = new FileSystemManagerFailImpl(1300002);
DCloud-yyl's avatar
DCloud-yyl 已提交
515 516 517 518 519
						options.fail?.(err)
						options.complete?.(err)
					})
					return
				}
DCloud-yyl's avatar
DCloud-yyl 已提交
520 521

				let copyRetFile = targetFile.copyTo(newFile, true)
DCloud-yyl's avatar
DCloud-yyl 已提交
522 523
				if (!copyRetFile.exists()) {
					// 调用系统api 失败
DCloud-yyl's avatar
DCloud-yyl 已提交
524 525
					currentDispatcher.async(function (_) {
						let err = new FileSystemManagerFailImpl(1300201);
DCloud-yyl's avatar
DCloud-yyl 已提交
526 527 528 529 530
						options.fail?.(err)
						options.complete?.(err)
					})
					return
				}
DCloud-yyl's avatar
DCloud-yyl 已提交
531 532 533 534 535

				currentDispatcher.async(function (_) {
					let success : FileManagerSuccessResult = {
						errMsg: "copyFile:ok"
					}
DCloud-yyl's avatar
DCloud-yyl 已提交
536 537 538 539
					options.success?.(success)
					options.complete?.(success)
				})
			}
DCloud-yyl's avatar
DCloud-yyl 已提交
540 541

		}, null)
DCloud-yyl's avatar
DCloud-yyl 已提交
542 543 544 545

	}


DCloud-yyl's avatar
DCloud-yyl 已提交
546 547
	public renameSync(oldPath : string, newPath : string) : void {
		let msgPrefix = "renameSync:fail "
DCloud-yyl's avatar
DCloud-yyl 已提交
548
		let filePath = UTSAndroid.convert2AbsFullPath(oldPath)
DCloud-yyl's avatar
DCloud-yyl 已提交
549
		let isSandyBox = isSandyBoxPath(filePath, false)
DCloud-yyl's avatar
DCloud-yyl 已提交
550
		if (!isSandyBox) {
DCloud-yyl's avatar
DCloud-yyl 已提交
551
			throw new UniError(FileSystemManagerUniErrorSubject, 1300013, msgPrefix + FileSystemManagerUniErrors.get(1301003)!);
DCloud-yyl's avatar
DCloud-yyl 已提交
552 553 554 555 556 557 558
		}

		let targetFile = new File(filePath)
		if (!targetFile.exists()) {
			/**
			 * 文件不存在
			 */
DCloud-yyl's avatar
DCloud-yyl 已提交
559
			throw new UniError(FileSystemManagerUniErrorSubject, 1300002, msgPrefix + FileSystemManagerUniErrors.get(1300002)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
560 561
		}
		let newFilePath = UTSAndroid.convert2AbsFullPath(newPath)
DCloud-yyl's avatar
DCloud-yyl 已提交
562
		isSandyBox = isSandyBoxPath(newFilePath, false)
DCloud-yyl's avatar
DCloud-yyl 已提交
563
		if (!isSandyBox) {
DCloud-yyl's avatar
DCloud-yyl 已提交
564
			throw new UniError(FileSystemManagerUniErrorSubject, 1300013, msgPrefix + FileSystemManagerUniErrors.get(1300013)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
565 566
		}
		let newFile = new File(newFilePath)
DCloud-yyl's avatar
DCloud-yyl 已提交
567

DCloud-yyl's avatar
DCloud-yyl 已提交
568 569 570 571
		if (newFile.getParentFile() == null || !newFile.getParentFile()!.exists()) {
			/**
			 * 父文件不存在
			 */
DCloud-yyl's avatar
DCloud-yyl 已提交
572
			throw new UniError(FileSystemManagerUniErrorSubject, 1300002, msgPrefix + FileSystemManagerUniErrors.get(1300002)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
573
		}
DCloud-yyl's avatar
DCloud-yyl 已提交
574 575


DCloud-yyl's avatar
DCloud-yyl 已提交
576 577 578
		let renameRet = targetFile.renameTo(newFile)
		if (!renameRet) {
			// 调用系统api 失败
DCloud-yyl's avatar
DCloud-yyl 已提交
579
			throw new UniError(FileSystemManagerUniErrorSubject, 1300201, msgPrefix + FileSystemManagerUniErrors.get(1300201)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
580 581 582
		}
	}

DCloud-yyl's avatar
DCloud-yyl 已提交
583
	public rename(options : RenameOptions) {
DCloud-yyl's avatar
DCloud-yyl 已提交
584

DCloud-yyl's avatar
DCloud-yyl 已提交
585 586
		let currentDispatcher = UTSAndroid.getDispatcher("main")
		UTSAndroid.getDispatcher('io').async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
587 588

			let filePath = UTSAndroid.convert2AbsFullPath(options.oldPath)
DCloud-yyl's avatar
DCloud-yyl 已提交
589
			let isSandyBox = isSandyBoxPath(filePath, false)
DCloud-yyl's avatar
DCloud-yyl 已提交
590
			if (!isSandyBox) {
DCloud-yyl's avatar
DCloud-yyl 已提交
591 592
				currentDispatcher.async(function (_) {
					let err = new FileSystemManagerFailImpl(1300013);
DCloud-yyl's avatar
DCloud-yyl 已提交
593 594 595 596 597 598 599 600 601 602 603
					options.fail?.(err)
					options.complete?.(err)
				})
				return
			}

			let targetFile = new File(filePath)
			if (!targetFile.exists()) {
				/**
				 * 文件不存在
				 */
DCloud-yyl's avatar
DCloud-yyl 已提交
604 605
				currentDispatcher.async(function (_) {
					let err = new FileSystemManagerFailImpl(1300002);
DCloud-yyl's avatar
DCloud-yyl 已提交
606 607 608 609 610 611
					options.fail?.(err)
					options.complete?.(err)
				})
				return
			}
			let newFilePath = UTSAndroid.convert2AbsFullPath(options.newPath)
DCloud-yyl's avatar
DCloud-yyl 已提交
612
			isSandyBox = isSandyBoxPath(newFilePath, false)
DCloud-yyl's avatar
DCloud-yyl 已提交
613
			if (!isSandyBox) {
DCloud-yyl's avatar
DCloud-yyl 已提交
614 615
				currentDispatcher.async(function (_) {
					let err = new FileSystemManagerFailImpl(1300013);
DCloud-yyl's avatar
DCloud-yyl 已提交
616 617 618 619 620 621 622 623 624 625 626
					options.fail?.(err)
					options.complete?.(err)
				})
				return
			}
			let newFile = new File(newFilePath)

			if (newFile.getParentFile() == null || !newFile.getParentFile()!.exists()) {
				/**
				 * 父文件不存在
				 */
DCloud-yyl's avatar
DCloud-yyl 已提交
627 628
				currentDispatcher.async(function (_) {
					let err = new FileSystemManagerFailImpl(1300002);
DCloud-yyl's avatar
DCloud-yyl 已提交
629 630 631 632 633 634 635 636 637 638
					options.fail?.(err)
					options.complete?.(err)
				})
				return
			}


			let renameRet = targetFile.renameTo(newFile)
			if (!renameRet) {
				// 调用系统api 失败
DCloud-yyl's avatar
DCloud-yyl 已提交
639 640
				currentDispatcher.async(function (_) {
					let err = new FileSystemManagerFailImpl(1300201);
DCloud-yyl's avatar
DCloud-yyl 已提交
641 642 643 644 645 646
					options.fail?.(err)
					options.complete?.(err)
				})
				return
			}

DCloud-yyl's avatar
DCloud-yyl 已提交
647 648 649 650
			currentDispatcher.async(function (_) {
				let success : FileManagerSuccessResult = {
					errMsg: "rename:ok"
				}
DCloud-yyl's avatar
DCloud-yyl 已提交
651 652 653
				options.success?.(success)
				options.complete?.(success)
			})
DCloud-yyl's avatar
DCloud-yyl 已提交
654
		}, null)
DCloud-yyl's avatar
DCloud-yyl 已提交
655 656 657

	}

DCloud-yyl's avatar
DCloud-yyl 已提交
658 659
	public accessSync(path : string) : void {
		let msgPrefix = "accessSync:fail "
DCloud-yyl's avatar
DCloud-yyl 已提交
660 661
		let filePath = UTSAndroid.convert2AbsFullPath(path)
		let targetFile = new File(filePath)
DCloud-yyl's avatar
DCloud-yyl 已提交
662
		let isSandyBox = isSandyBoxPath(filePath, true)
DCloud-yyl's avatar
DCloud-yyl 已提交
663
		if (!isSandyBox) {
DCloud-yyl's avatar
DCloud-yyl 已提交
664
			throw new UniError(FileSystemManagerUniErrorSubject, 1300013, msgPrefix + FileSystemManagerUniErrors.get(1300013)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
665 666 667 668 669 670
		}

		if (!targetFile.exists()) {
			/**
			 * 文件不存在,或者不是文件夹,异常
			 */
DCloud-yyl's avatar
DCloud-yyl 已提交
671
			throw new UniError(FileSystemManagerUniErrorSubject, 1300002, msgPrefix + FileSystemManagerUniErrors.get(1300002)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
672 673 674
		}
	}

DCloud-yyl's avatar
DCloud-yyl 已提交
675
	public access(options : AccessOptions) {
DCloud-yyl's avatar
DCloud-yyl 已提交
676

DCloud-yyl's avatar
DCloud-yyl 已提交
677 678
		let currentDispatcher = UTSAndroid.getDispatcher("main")
		UTSAndroid.getDispatcher('io').async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
679 680 681 682

			let filePath = UTSAndroid.convert2AbsFullPath(options.path)
			let targetFile = new File(filePath)

DCloud-yyl's avatar
DCloud-yyl 已提交
683
			let isSandyBox = isSandyBoxPath(filePath, true)
DCloud-yyl's avatar
DCloud-yyl 已提交
684
			if (!isSandyBox) {
DCloud-yyl's avatar
DCloud-yyl 已提交
685 686
				currentDispatcher.async(function (_) {
					let err = new FileSystemManagerFailImpl(1300013);
DCloud-yyl's avatar
DCloud-yyl 已提交
687 688 689 690 691 692 693 694 695 696
					options.fail?.(err)
					options.complete?.(err)
				})
				return
			}

			if (!targetFile.exists()) {
				/**
				 * 文件不存在,或者不是文件夹,异常
				 */
DCloud-yyl's avatar
DCloud-yyl 已提交
697 698
				currentDispatcher.async(function (_) {
					let err = new FileSystemManagerFailImpl(1300002);
DCloud-yyl's avatar
DCloud-yyl 已提交
699 700 701 702 703 704
					options.fail?.(err)
					options.complete?.(err)
				})
				return
			}

DCloud-yyl's avatar
DCloud-yyl 已提交
705
			currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
706
				// 成功
DCloud-yyl's avatar
DCloud-yyl 已提交
707 708 709
				let success : FileManagerSuccessResult = {
					errMsg: "access:ok"
				}
DCloud-yyl's avatar
DCloud-yyl 已提交
710 711 712
				options.success?.(success)
				options.complete?.(success)
			})
DCloud-yyl's avatar
DCloud-yyl 已提交
713
		}, null)
DCloud-yyl's avatar
DCloud-yyl 已提交
714 715 716 717 718



	}

DCloud-yyl's avatar
DCloud-yyl 已提交
719 720
	readdirSync(dirPath : string) : string[] | null {
		let msgPrefix = "readdirSync:fail "
DCloud-yyl's avatar
DCloud-yyl 已提交
721
		let filePath = UTSAndroid.convert2AbsFullPath(dirPath)
DCloud-yyl's avatar
DCloud-yyl 已提交
722
		let isSandyBox = isSandyBoxPath(filePath, true)
DCloud-yyl's avatar
DCloud-yyl 已提交
723
		if (!isSandyBox) {
DCloud-yyl's avatar
DCloud-yyl 已提交
724
			throw new UniError(FileSystemManagerUniErrorSubject, 1300013, msgPrefix + FileSystemManagerUniErrors.get(1300013)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
725 726 727 728 729 730 731
		}

		let targetFile = new File(filePath)
		if (!targetFile.exists() || !targetFile.isDirectory()) {
			/**
			 * 文件不存在,或者不是文件夹,异常
			 */
DCloud-yyl's avatar
DCloud-yyl 已提交
732
			throw new UniError(FileSystemManagerUniErrorSubject, 1300002, msgPrefix + FileSystemManagerUniErrors.get(1300002)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
733 734 735 736
		}
		return UTSArray.fromNative(targetFile.list()!)
	}

DCloud-yyl's avatar
DCloud-yyl 已提交
737
	public readdir(options : ReadDirOptions) {
DCloud-yyl's avatar
DCloud-yyl 已提交
738

DCloud-yyl's avatar
DCloud-yyl 已提交
739 740
		let currentDispatcher = UTSAndroid.getDispatcher("main")
		UTSAndroid.getDispatcher('io').async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
741
			let filePath = UTSAndroid.convert2AbsFullPath(options.dirPath)
DCloud-yyl's avatar
DCloud-yyl 已提交
742
			let isSandyBox = isSandyBoxPath(filePath, true)
DCloud-yyl's avatar
DCloud-yyl 已提交
743
			if (!isSandyBox) {
DCloud-yyl's avatar
DCloud-yyl 已提交
744 745
				currentDispatcher.async(function (_) {
					let err = new FileSystemManagerFailImpl(1300013);
DCloud-yyl's avatar
DCloud-yyl 已提交
746 747 748 749 750 751 752 753 754 755 756
					options.fail?.(err)
					options.complete?.(err)
				})
				return
			}

			let targetFile = new File(filePath)
			if (!targetFile.exists() || !targetFile.isDirectory()) {
				/**
				 * 文件不存在,或者不是文件夹,异常
				 */
DCloud-yyl's avatar
DCloud-yyl 已提交
757 758
				currentDispatcher.async(function (_) {
					let err = new FileSystemManagerFailImpl(1300002);
DCloud-yyl's avatar
DCloud-yyl 已提交
759 760 761 762 763 764
					options.fail?.(err)
					options.complete?.(err)
				})
				return
			}

DCloud-yyl's avatar
DCloud-yyl 已提交
765 766 767 768
			let success : ReadDirSuccessResult = {
				files: UTSArray.fromNative(targetFile.list()!)
			}
			currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
769 770 771
				options.success?.(success)
				options.complete?.(success)
			})
DCloud-yyl's avatar
DCloud-yyl 已提交
772
		}, null)
DCloud-yyl's avatar
DCloud-yyl 已提交
773 774
	}

DCloud-yyl's avatar
DCloud-yyl 已提交
775 776
	public rmdirSync(dirPath : string, recursive : boolean) : void {
		let msgPrefix = "rmdirSync:fail "
DCloud-yyl's avatar
DCloud-yyl 已提交
777 778
		let filePath = UTSAndroid.convert2AbsFullPath(dirPath)
		let targetFile = new File(filePath)
DCloud-yyl's avatar
DCloud-yyl 已提交
779
		let isSandyBox = isSandyBoxPath(filePath, false)
DCloud-yyl's avatar
DCloud-yyl 已提交
780
		if (!isSandyBox) {
DCloud-yyl's avatar
DCloud-yyl 已提交
781
			throw new UniError(FileSystemManagerUniErrorSubject, 1300013, msgPrefix + FileSystemManagerUniErrors.get(1300013)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
782 783
		}

DCloud-yyl's avatar
DCloud-yyl 已提交
784
		if (!targetFile.exists() || !targetFile.isDirectory()) {
DCloud-yyl's avatar
DCloud-yyl 已提交
785
			/**
DCloud-yyl's avatar
DCloud-yyl 已提交
786
			 * 文件不存在 或者 文件是不是文件夹
DCloud-yyl's avatar
DCloud-yyl 已提交
787
			 */
DCloud-yyl's avatar
DCloud-yyl 已提交
788
			throw new UniError(FileSystemManagerUniErrorSubject, 1300002, msgPrefix + FileSystemManagerUniErrors.get(1300002)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
789
		}
DCloud-yyl's avatar
DCloud-yyl 已提交
790 791 792


		if (!recursive) {
DCloud-yyl's avatar
DCloud-yyl 已提交
793
			// 没有设置递归,则需要检查是否保存子目录
DCloud-yyl's avatar
DCloud-yyl 已提交
794 795
			let childList = targetFile.list()
			if (childList != null && childList.size > 0) {
DCloud-yyl's avatar
DCloud-yyl 已提交
796
				// 存在子目录
DCloud-yyl's avatar
DCloud-yyl 已提交
797
				throw new UniError(FileSystemManagerUniErrorSubject, 1300066, msgPrefix + FileSystemManagerUniErrors.get(1300066)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
798
			}
DCloud-yyl's avatar
DCloud-yyl 已提交
799
		} else {
DCloud-yyl's avatar
DCloud-yyl 已提交
800 801 802
			let delRet = targetFile.deleteRecursively()
			if (!delRet) {
				// 调用系统api 失败
DCloud-yyl's avatar
DCloud-yyl 已提交
803
				throw new UniError(FileSystemManagerUniErrorSubject, 1300201, msgPrefix + FileSystemManagerUniErrors.get(1300201)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
804 805 806 807
			}
		}
	}

DCloud-yyl's avatar
DCloud-yyl 已提交
808 809 810 811 812
	public mkdirSync(dirPath : string, recursive : boolean) : void {
		let msgPrefix = "mkdirSync:fail "
		if (dirPath.isEmpty()) {
			throw new UniError(FileSystemManagerUniErrorSubject, 1300066, msgPrefix + FileSystemManagerUniErrors.get(1300066)!)
		}
DCloud-yyl's avatar
DCloud-yyl 已提交
813
		let filePath = UTSAndroid.convert2AbsFullPath(dirPath)
DCloud-yyl's avatar
DCloud-yyl 已提交
814
		let isSandyBox = isSandyBoxPath(filePath, false)
DCloud-yyl's avatar
DCloud-yyl 已提交
815
		if (!isSandyBox) {
DCloud-yyl's avatar
DCloud-yyl 已提交
816
			throw new UniError(FileSystemManagerUniErrorSubject, 1300013, msgPrefix + FileSystemManagerUniErrors.get(1300013)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
817
		}
DCloud-yyl's avatar
DCloud-yyl 已提交
818 819
		let targetFile = new File(filePath)
		if (targetFile.exists()) {
DCloud-yyl's avatar
DCloud-yyl 已提交
820
			/**
DCloud-yyl's avatar
DCloud-yyl 已提交
821
			 * 文件已经存在,则无法继续创建
DCloud-yyl's avatar
DCloud-yyl 已提交
822
			 */
DCloud-yyl's avatar
DCloud-yyl 已提交
823
			throw new UniError(FileSystemManagerUniErrorSubject, 1301005, msgPrefix + FileSystemManagerUniErrors.get(1301005)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
824
		}
DCloud-yyl's avatar
DCloud-yyl 已提交
825 826 827 828 829 830 831 832 833 834 835 836

		if (!recursive) {
			//  没有设置递归创建
			if (targetFile.getParentFile() == null || !targetFile.getParentFile()!.exists()) {
				throw new UniError(FileSystemManagerUniErrorSubject, 1300002, msgPrefix + FileSystemManagerUniErrors.get(1300002)!)
			} else {
				// 父文件夹存在,则继续创建
				let mkRet = targetFile.mkdir()
				if (!mkRet) {
					// 调用系统api 失败
					throw new UniError(FileSystemManagerUniErrorSubject, 1300201, msgPrefix + FileSystemManagerUniErrors.get(1300201)!)
				}
DCloud-yyl's avatar
DCloud-yyl 已提交
837
			}
DCloud-yyl's avatar
DCloud-yyl 已提交
838 839 840 841
		} else {
			// 设置了递归
			let mkRet = targetFile.mkdirs()
			if (!mkRet) {
DCloud-yyl's avatar
DCloud-yyl 已提交
842
				// 调用系统api 失败
DCloud-yyl's avatar
DCloud-yyl 已提交
843
				throw new UniError(FileSystemManagerUniErrorSubject, 1300201, msgPrefix + FileSystemManagerUniErrors.get(1300201)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
844 845 846 847 848 849 850 851 852
			}
		}
	}

	public rmdir(options : RmDirOptions) {

		let filePath = UTSAndroid.convert2AbsFullPath(options.dirPath)
		let targetFile = new File(filePath)

DCloud-yyl's avatar
DCloud-yyl 已提交
853 854
		let currentDispatcher = UTSAndroid.getDispatcher("main")
		UTSAndroid.getDispatcher('io').async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
855

DCloud-yyl's avatar
DCloud-yyl 已提交
856
			let isSandyBox = isSandyBoxPath(filePath, false)
DCloud-yyl's avatar
DCloud-yyl 已提交
857
			if (!isSandyBox) {
DCloud-yyl's avatar
DCloud-yyl 已提交
858 859
				currentDispatcher.async(function (_) {
					let err = new FileSystemManagerFailImpl(1300013);
DCloud-yyl's avatar
DCloud-yyl 已提交
860 861 862 863 864 865 866 867 868 869
					options.fail?.(err)
					options.complete?.(err)
				})
				return
			}

			if (!targetFile.exists() || !targetFile.isDirectory()) {
				/**
				 * 文件不存在 或者 文件是不是文件夹
				 */
DCloud-yyl's avatar
DCloud-yyl 已提交
870 871
				currentDispatcher.async(function (_) {
					let err = new FileSystemManagerFailImpl(1300002);
DCloud-yyl's avatar
DCloud-yyl 已提交
872 873 874 875 876 877 878
					options.fail?.(err)
					options.complete?.(err)
				})
				return
			}


DCloud-yyl's avatar
DCloud-yyl 已提交
879
			if (!options.recursive) {
DCloud-yyl's avatar
DCloud-yyl 已提交
880 881
				// 没有设置递归,则需要检查是否保存子目录
				let childList = targetFile.list()
DCloud-yyl's avatar
DCloud-yyl 已提交
882
				if (childList != null && childList.size > 0) {
DCloud-yyl's avatar
DCloud-yyl 已提交
883
					// 存在子目录
DCloud-yyl's avatar
DCloud-yyl 已提交
884 885
					currentDispatcher.async(function (_) {
						let err = new FileSystemManagerFailImpl(1300066);
DCloud-yyl's avatar
DCloud-yyl 已提交
886 887 888 889 890 891
						options.fail?.(err)
						options.complete?.(err)
					})

					return
				}
DCloud-yyl's avatar
DCloud-yyl 已提交
892
			} else {
DCloud-yyl's avatar
DCloud-yyl 已提交
893 894 895
				let delRet = targetFile.deleteRecursively()
				if (!delRet) {
					// 调用系统api 失败
DCloud-yyl's avatar
DCloud-yyl 已提交
896 897
					currentDispatcher.async(function (_) {
						let err = new FileSystemManagerFailImpl(1300201);
DCloud-yyl's avatar
DCloud-yyl 已提交
898 899 900 901 902 903 904
						options.fail?.(err)
						options.complete?.(err)
					})
					return
				}
			}

DCloud-yyl's avatar
DCloud-yyl 已提交
905 906 907
			let success : FileManagerSuccessResult = {
				errMsg: "rmdir:ok"
			}
DCloud-yyl's avatar
DCloud-yyl 已提交
908
			// 删除成功
DCloud-yyl's avatar
DCloud-yyl 已提交
909
			currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
910 911 912
				options.success?.(success)
				options.complete?.(success)
			})
DCloud-yyl's avatar
DCloud-yyl 已提交
913
		}, null)
DCloud-yyl's avatar
DCloud-yyl 已提交
914 915 916 917 918 919 920



	}

	public mkdir(options : MkDirOptions) {

DCloud-yyl's avatar
DCloud-yyl 已提交
921 922
		let currentDispatcher = UTSAndroid.getDispatcher("main")
		UTSAndroid.getDispatcher('io').async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
923

DCloud-yyl's avatar
DCloud-yyl 已提交
924 925 926
			if (options.dirPath.isEmpty()) {
				currentDispatcher.async(function (_) {
					let err = new FileSystemManagerFailImpl(1300066);
DCloud-yyl's avatar
DCloud-yyl 已提交
927 928 929 930 931 932 933
					options.fail?.(err)
					options.complete?.(err)
				})
				return
			}

			let filePath = UTSAndroid.convert2AbsFullPath(options.dirPath)
DCloud-yyl's avatar
DCloud-yyl 已提交
934
			let isSandyBox = isSandyBoxPath(filePath, false)
DCloud-yyl's avatar
DCloud-yyl 已提交
935
			if (!isSandyBox) {
DCloud-yyl's avatar
DCloud-yyl 已提交
936 937
				currentDispatcher.async(function (_) {
					let err = new FileSystemManagerFailImpl(1300013);
DCloud-yyl's avatar
DCloud-yyl 已提交
938 939 940 941 942 943 944 945 946 947 948
					options.fail?.(err)
					options.complete?.(err)
				})
				return
			}

			let targetFile = new File(filePath)
			if (targetFile.exists()) {
				/**
				 * 文件已经存在,则无法继续创建
				 */
DCloud-yyl's avatar
DCloud-yyl 已提交
949 950
				currentDispatcher.async(function (_) {
					let err = new FileSystemManagerFailImpl(1301005);
DCloud-yyl's avatar
DCloud-yyl 已提交
951 952 953 954 955 956
					options.fail?.(err)
					options.complete?.(err)
				})
				return
			}

DCloud-yyl's avatar
DCloud-yyl 已提交
957
			if (!options.recursive) {
DCloud-yyl's avatar
DCloud-yyl 已提交
958
				//  没有设置递归创建
DCloud-yyl's avatar
DCloud-yyl 已提交
959 960 961
				if (targetFile.getParentFile() == null || !targetFile.getParentFile()!.exists()) {
					currentDispatcher.async(function (_) {
						let err = new FileSystemManagerFailImpl(1300002);
DCloud-yyl's avatar
DCloud-yyl 已提交
962 963 964 965
						options.fail?.(err)
						options.complete?.(err)
					})
					return
DCloud-yyl's avatar
DCloud-yyl 已提交
966
				} else {
DCloud-yyl's avatar
DCloud-yyl 已提交
967 968 969 970
					// 父文件夹存在,则继续创建
					let mkRet = targetFile.mkdir()
					if (!mkRet) {
						// 调用系统api 失败
DCloud-yyl's avatar
DCloud-yyl 已提交
971 972
						currentDispatcher.async(function (_) {
							let err = new FileSystemManagerFailImpl(1300201);
DCloud-yyl's avatar
DCloud-yyl 已提交
973 974 975 976 977 978 979
							options.fail?.(err)
							options.complete?.(err)
						})

						return
					}
				}
DCloud-yyl's avatar
DCloud-yyl 已提交
980
			} else {
DCloud-yyl's avatar
DCloud-yyl 已提交
981 982 983 984
				// 设置了递归
				let mkRet = targetFile.mkdirs()
				if (!mkRet) {
					// 调用系统api 失败
DCloud-yyl's avatar
DCloud-yyl 已提交
985 986
					currentDispatcher.async(function (_) {
						let err = new FileSystemManagerFailImpl(1300201);
DCloud-yyl's avatar
DCloud-yyl 已提交
987 988 989 990 991 992 993 994
						options.fail?.(err)
						options.complete?.(err)
					})
					return
				}
			}


DCloud-yyl's avatar
DCloud-yyl 已提交
995 996 997 998
			currentDispatcher.async(function (_) {
				let success : FileManagerSuccessResult = {
					errMsg: "mkdir:ok"
				}
DCloud-yyl's avatar
DCloud-yyl 已提交
999 1000 1001 1002
				// 删除成功
				options.success?.(success)
				options.complete?.(success)
			})
DCloud-yyl's avatar
DCloud-yyl 已提交
1003
		}, null)
DCloud-yyl's avatar
DCloud-yyl 已提交
1004 1005 1006 1007 1008



	}

DCloud-yyl's avatar
DCloud-yyl 已提交
1009 1010 1011 1012
	unlinkSync(filePath : string) : void {
		let msgPrefix = "unlinkSync:fail "
		let temFilePath = UTSAndroid.convert2AbsFullPath(filePath)
		let isSandyBox = isSandyBoxPath(temFilePath, false)
DCloud-yyl's avatar
DCloud-yyl 已提交
1013
		if (!isSandyBox) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1014
			throw new UniError(FileSystemManagerUniErrorSubject, 1300013, msgPrefix + FileSystemManagerUniErrors.get(1300013)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1015 1016
		}

DCloud-yyl's avatar
DCloud-yyl 已提交
1017
		let targetFile = new File(temFilePath)
DCloud-yyl's avatar
DCloud-yyl 已提交
1018
		if (!targetFile.exists()) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1019
			throw new UniError(FileSystemManagerUniErrorSubject, 1300002, msgPrefix + FileSystemManagerUniErrors.get(1300002)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1020
		}
DCloud-yyl's avatar
DCloud-yyl 已提交
1021

DCloud-yyl's avatar
DCloud-yyl 已提交
1022 1023 1024 1025
		// 文件存在,则进行删除操作
		let delRet = targetFile.delete()
		if (!delRet) {
			// 调用系统api 删除失败
DCloud-yyl's avatar
DCloud-yyl 已提交
1026
			throw new UniError(FileSystemManagerUniErrorSubject, 1300201, msgPrefix + FileSystemManagerUniErrors.get(1300201)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
		}
	}

	public unlink(options : UnLinkOptions) {

		/**
		 * 遗留问题,无法知道是否属于沙盒路径
		 */
		let filePath = UTSAndroid.convert2AbsFullPath(options.filePath)


DCloud-yyl's avatar
DCloud-yyl 已提交
1038 1039
		let currentDispatcher = UTSAndroid.getDispatcher("main")
		UTSAndroid.getDispatcher('io').async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1040

DCloud-yyl's avatar
DCloud-yyl 已提交
1041
			let isSandyBox = isSandyBoxPath(filePath, false)
DCloud-yyl's avatar
DCloud-yyl 已提交
1042
			if (!isSandyBox) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1043 1044
				currentDispatcher.async(function (_) {
					let err = new FileSystemManagerFailImpl(1300013);
DCloud-yyl's avatar
DCloud-yyl 已提交
1045 1046 1047 1048 1049 1050 1051 1052
					options.fail?.(err)
					options.complete?.(err)
				})
				return
			}

			let targetFile = new File(filePath)
			if (!targetFile.exists()) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1053 1054
				currentDispatcher.async(function (_) {
					let err = new FileSystemManagerFailImpl(1300002);
DCloud-yyl's avatar
DCloud-yyl 已提交
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
					options.fail?.(err)
					options.complete?.(err)
				})
				return
			}

			// 文件存在,则进行删除操作
			let delRet = targetFile.delete()
			if (!delRet) {
				// 调用系统api 删除失败
DCloud-yyl's avatar
DCloud-yyl 已提交
1065 1066
				currentDispatcher.async(function (_) {
					let err = new FileSystemManagerFailImpl(1300201);
DCloud-yyl's avatar
DCloud-yyl 已提交
1067 1068 1069 1070 1071 1072
					options.fail?.(err)
					options.complete?.(err)
				})
				return
			}

DCloud-yyl's avatar
DCloud-yyl 已提交
1073 1074 1075 1076
			currentDispatcher.async(function (_) {
				let success : FileManagerSuccessResult = {
					errMsg: "unlink:ok"
				}
DCloud-yyl's avatar
DCloud-yyl 已提交
1077 1078 1079 1080
				// 删除成功
				options.success?.(success)
				options.complete?.(success)
			})
DCloud-yyl's avatar
DCloud-yyl 已提交
1081
		}, null)
DCloud-yyl's avatar
DCloud-yyl 已提交
1082 1083 1084 1085 1086



	}

DCloud-yyl's avatar
DCloud-yyl 已提交
1087 1088
	public readFileSync(filePath : string, encoding : string | null) : string {
		let msgPrefix = "readFileSync:fail "
DCloud-yyl's avatar
DCloud-yyl 已提交
1089
		// 判断type 是否合法
DCloud-yyl's avatar
DCloud-yyl 已提交
1090 1091 1092
		let tempEncoding = encoding
		if (tempEncoding == null) {
			tempEncoding = "utf-8"
DCloud-yyl's avatar
DCloud-yyl 已提交
1093
		}
DCloud-yyl's avatar
DCloud-yyl 已提交
1094 1095
		if (tempEncoding.toLowerCase() != 'base64' && tempEncoding.toLowerCase() != 'utf-8' && tempEncoding.toLowerCase() != 'ascii') {
			throw new UniError(FileSystemManagerUniErrorSubject, 1200002, msgPrefix + FileSystemManagerUniErrors.get(1200002)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1096
		}
DCloud-yyl's avatar
DCloud-yyl 已提交
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
		let tempFilePath = UTSAndroid.convert2AbsFullPath(filePath)
		if (tempFilePath.startsWith("/android_asset/")) {
			let byteArray : ByteArray
			try {
				let assetName = tempFilePath.substring("/android_asset/".length)
				let assetStream = UTSAndroid.getAppContext()!.getResources().getAssets().open(assetName);
				let byteLen = assetStream.available();
				byteArray = new ByteArray(byteLen)
				assetStream.read(byteArray);
			} catch (e : Exception) {
				throw new UniError(FileSystemManagerUniErrorSubject, 1300201, msgPrefix + FileSystemManagerUniErrors.get(1300201)! + ":" + filePath)
			}
DCloud-yyl's avatar
DCloud-yyl 已提交
1109

DCloud-yyl's avatar
DCloud-yyl 已提交
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
			if (tempEncoding.toLowerCase() == 'base64') {
				// base64
				let base64Content = Base64.encodeToString(byteArray, Base64.NO_WRAP)
				return base64Content
			} else if (tempEncoding.toLowerCase() == "ascii") {
				// ascii
				let text = new String(byteArray, Charsets.US_ASCII)
				return text
			} else {
				// utf-8 兜底
				let text = new String(byteArray, Charsets.UTF_8)
				return text
			}
DCloud-yyl's avatar
DCloud-yyl 已提交
1123

DCloud-yyl's avatar
DCloud-yyl 已提交
1124
		} else {
DCloud-yyl's avatar
DCloud-yyl 已提交
1125

DCloud-yyl's avatar
DCloud-yyl 已提交
1126 1127 1128 1129
			let isSandyBox = isSandyBoxPath(tempFilePath, true)
			if (!isSandyBox) {
				return "1300013"
			}
DCloud-yyl's avatar
DCloud-yyl 已提交
1130

DCloud-yyl's avatar
DCloud-yyl 已提交
1131 1132 1133 1134
			let targetFile = new File(tempFilePath)
			if (!targetFile.exists()) {
				throw new UniError(FileSystemManagerUniErrorSubject, 1300002, msgPrefix + FileSystemManagerUniErrors.get(1300002)! + ":" + filePath);
			}
DCloud-yyl's avatar
DCloud-yyl 已提交
1135

DCloud-yyl's avatar
DCloud-yyl 已提交
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
			if (targetFile.isDirectory()) {
				throw new UniError(FileSystemManagerUniErrorSubject, 1301003, msgPrefix + FileSystemManagerUniErrors.get(1301003)! + ":" + filePath);
			}

			/**
			 * 文件超过100M,会超过应用内存
			 */
			if (targetFile.length() > 100 * 1024 * 1024) {
				throw new UniError(FileSystemManagerUniErrorSubject, 1300202, msgPrefix + FileSystemManagerUniErrors.get(1300202)! + ":" + filePath);
			}
DCloud-yyl's avatar
DCloud-yyl 已提交
1146

DCloud-yyl's avatar
DCloud-yyl 已提交
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
			if (tempEncoding.toLowerCase() == 'base64') {
				// base64
				let byteArray = targetFile.readBytes()
				let base64Content = Base64.encodeToString(byteArray, Base64.NO_WRAP)
				return base64Content

			} else if (tempEncoding.toLowerCase() == "ascii") {
				// ascii
				let text = targetFile.readText(Charsets.US_ASCII)
				return text
			}
			else {
				// utf-8 兜底
				let text = targetFile.readText(Charsets.UTF_8)
				return text
			}
		}
DCloud-yyl's avatar
DCloud-yyl 已提交
1164 1165 1166 1167 1168
	}

	public readFile(options : ReadFileOptions) {

		// 判断type 是否合法
DCloud-yyl's avatar
DCloud-yyl 已提交
1169 1170
		if (options.encoding.toLowerCase() != 'base64' && options.encoding.toLowerCase() != 'utf-8' && options.encoding.toLowerCase() != 'ascii') {
			let err = new FileSystemManagerFailImpl(1200002);
DCloud-yyl's avatar
DCloud-yyl 已提交
1171 1172 1173 1174 1175 1176
			options.fail?.(err)
			options.complete?.(err)
			return
		}


DCloud-yyl's avatar
DCloud-yyl 已提交
1177 1178

		let currentDispatcher = UTSAndroid.getDispatcher("main")
DCloud-yyl's avatar
DCloud-yyl 已提交
1179 1180 1181
		/**
		 * 执行真正的加载行为,为了避免阻塞分发到 io任务序列
		 */
DCloud-yyl's avatar
DCloud-yyl 已提交
1182 1183
		UTSAndroid.getDispatcher('io').async(function (_) {

DCloud-yyl's avatar
DCloud-yyl 已提交
1184
			let filePath = UTSAndroid.convert2AbsFullPath(options.filePath)
DCloud-yyl's avatar
DCloud-yyl 已提交
1185 1186

			if (filePath.startsWith("/android_asset/")) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1187
				// 用户访问的是asset 路径,此时不是释放模式
DCloud-yyl's avatar
DCloud-yyl 已提交
1188 1189

				let exceptionInfo : Exception | null = null
DCloud-yyl's avatar
DCloud-yyl 已提交
1190 1191 1192 1193 1194 1195 1196
				let byteArray = new ByteArray(0)
				try {
					let assetName = filePath.substring("/android_asset/".length)
					let assetStream = UTSAndroid.getAppContext()!.getResources().getAssets().open(assetName);
					let byteLen = assetStream.available();
					byteArray = new ByteArray(byteLen)
					assetStream.read(byteArray);
DCloud-yyl's avatar
DCloud-yyl 已提交
1197
				} catch (e : Exception) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1198 1199
					exceptionInfo = e
				}
DCloud-yyl's avatar
DCloud-yyl 已提交
1200 1201 1202 1203

				if (exceptionInfo != null) {
					currentDispatcher.async(function (_) {
						let err = new FileSystemManagerFailImpl(1300201);
DCloud-yyl's avatar
DCloud-yyl 已提交
1204 1205 1206
						options.fail?.(err)
						options.complete?.(err)
					})
DCloud-yyl's avatar
DCloud-yyl 已提交
1207
					return
DCloud-yyl's avatar
DCloud-yyl 已提交
1208
				}
DCloud-yyl's avatar
DCloud-yyl 已提交
1209

DCloud-yyl's avatar
DCloud-yyl 已提交
1210
				let ret : ReadFileSuccessResult = {
DCloud-yyl's avatar
DCloud-yyl 已提交
1211
					data: ""
DCloud-yyl's avatar
DCloud-yyl 已提交
1212
				}
DCloud-yyl's avatar
DCloud-yyl 已提交
1213 1214

				if (options.encoding.toLowerCase() == 'base64') {
DCloud-yyl's avatar
DCloud-yyl 已提交
1215
					// base64
DCloud-yyl's avatar
DCloud-yyl 已提交
1216
					let base64Content = Base64.encodeToString(byteArray, Base64.NO_WRAP)
DCloud-yyl's avatar
DCloud-yyl 已提交
1217
					ret.data = base64Content
DCloud-yyl's avatar
DCloud-yyl 已提交
1218 1219

				} else if (options.encoding.toLowerCase() == "ascii") {
DCloud-yyl's avatar
DCloud-yyl 已提交
1220
					// ascii
DCloud-yyl's avatar
DCloud-yyl 已提交
1221 1222

					let text = new String(byteArray, Charsets.US_ASCII)
DCloud-yyl's avatar
DCloud-yyl 已提交
1223 1224
					ret.data = text
				}
DCloud-yyl's avatar
DCloud-yyl 已提交
1225
				else {
DCloud-yyl's avatar
DCloud-yyl 已提交
1226
					// utf-8 兜底
DCloud-yyl's avatar
DCloud-yyl 已提交
1227
					let text = new String(byteArray, Charsets.UTF_8)
DCloud-yyl's avatar
DCloud-yyl 已提交
1228 1229
					ret.data = text
				}
DCloud-yyl's avatar
DCloud-yyl 已提交
1230 1231

				currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1232 1233 1234
					options.success?.(ret)
					options.complete?.(ret)
				})
DCloud-yyl's avatar
DCloud-yyl 已提交
1235 1236 1237 1238

			} else {

				let isSandyBox = isSandyBoxPath(filePath, true)
DCloud-yyl's avatar
DCloud-yyl 已提交
1239
				if (!isSandyBox) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1240 1241
					currentDispatcher.async(function (_) {
						let err = new FileSystemManagerFailImpl(1300013);
DCloud-yyl's avatar
DCloud-yyl 已提交
1242 1243 1244 1245 1246
						options.fail?.(err)
						options.complete?.(err)
					})
					return
				}
DCloud-yyl's avatar
DCloud-yyl 已提交
1247

DCloud-yyl's avatar
DCloud-yyl 已提交
1248 1249
				let targetFile = new File(filePath)
				if (!targetFile.exists()) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1250 1251
					currentDispatcher.async(function (_) {
						let err = new FileSystemManagerFailImpl(1300002);
DCloud-yyl's avatar
DCloud-yyl 已提交
1252 1253 1254 1255 1256
						options.fail?.(err)
						options.complete?.(err)
					})
					return
				}
DCloud-yyl's avatar
DCloud-yyl 已提交
1257

DCloud-yyl's avatar
DCloud-yyl 已提交
1258
				if (targetFile.isDirectory()) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1259 1260
					currentDispatcher.async(function (_) {
						let err = new FileSystemManagerFailImpl(1301003);
DCloud-yyl's avatar
DCloud-yyl 已提交
1261 1262 1263 1264 1265
						options.fail?.(err)
						options.complete?.(err)
					})
					return
				}
DCloud-yyl's avatar
DCloud-yyl 已提交
1266

DCloud-yyl's avatar
DCloud-yyl 已提交
1267 1268 1269 1270
				/**
				 * 文件超过100M,会超过应用内存
				 */
				if (targetFile.length() > 100 * 1024 * 1024) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1271 1272
					currentDispatcher.async(function (_) {
						let err = new FileSystemManagerFailImpl(1300202);
DCloud-yyl's avatar
DCloud-yyl 已提交
1273 1274 1275 1276 1277
						options.fail?.(err)
						options.complete?.(err)
					})
					return
				}
DCloud-yyl's avatar
DCloud-yyl 已提交
1278

DCloud-yyl's avatar
DCloud-yyl 已提交
1279
				let ret : ReadFileSuccessResult = {
DCloud-yyl's avatar
DCloud-yyl 已提交
1280
					data: ""
DCloud-yyl's avatar
DCloud-yyl 已提交
1281
				}
DCloud-yyl's avatar
DCloud-yyl 已提交
1282 1283

				if (options.encoding.toLowerCase() == 'base64') {
DCloud-yyl's avatar
DCloud-yyl 已提交
1284 1285
					// base64
					let byteArray = targetFile.readBytes()
DCloud-yyl's avatar
DCloud-yyl 已提交
1286
					let base64Content = Base64.encodeToString(byteArray, Base64.NO_WRAP)
DCloud-yyl's avatar
DCloud-yyl 已提交
1287
					ret.data = base64Content
DCloud-yyl's avatar
DCloud-yyl 已提交
1288 1289

				} else if (options.encoding.toLowerCase() == "ascii") {
DCloud-yyl's avatar
DCloud-yyl 已提交
1290 1291 1292 1293
					// ascii
					let text = targetFile.readText(Charsets.US_ASCII)
					ret.data = text
				}
DCloud-yyl's avatar
DCloud-yyl 已提交
1294
				else {
DCloud-yyl's avatar
DCloud-yyl 已提交
1295 1296 1297 1298
					// utf-8 兜底
					let text = targetFile.readText(Charsets.UTF_8)
					ret.data = text
				}
DCloud-yyl's avatar
DCloud-yyl 已提交
1299 1300

				currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1301 1302 1303 1304 1305
					options.success?.(ret)
					options.complete?.(ret)
				})
			}

DCloud-yyl's avatar
DCloud-yyl 已提交
1306 1307

		}, null)
DCloud-yyl's avatar
DCloud-yyl 已提交
1308 1309 1310

	}

DCloud-yyl's avatar
DCloud-yyl 已提交
1311 1312 1313 1314 1315 1316 1317
	public writeFileSync(filePath : string, data : string, encoding : string) : void {
		let msgPrefix = "writeFileSync:fail "
		let tempEncoding = encoding
		if (tempEncoding.toLowerCase() != 'base64' && tempEncoding.toLowerCase() != 'utf-8' && tempEncoding.toLowerCase() != 'ascii') {
			throw new UniError(FileSystemManagerUniErrorSubject, 1200002, msgPrefix + FileSystemManagerUniErrors.get(1200002)!)
		}

DCloud-yyl's avatar
DCloud-yyl 已提交
1318
		// 判断type 是否合法
DCloud-yyl's avatar
DCloud-yyl 已提交
1319
		let tempFilePath = UTSAndroid.convert2AbsFullPath(filePath)
DCloud-yyl's avatar
DCloud-yyl 已提交
1320

DCloud-yyl's avatar
DCloud-yyl 已提交
1321
		let isSandyBox = isSandyBoxPath(tempFilePath, false)
DCloud-yyl's avatar
DCloud-yyl 已提交
1322
		if (!isSandyBox) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1323
			throw new UniError(FileSystemManagerUniErrorSubject, 1300013, msgPrefix + FileSystemManagerUniErrors.get(1300013)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1324 1325
		}

DCloud-yyl's avatar
DCloud-yyl 已提交
1326
		let nextFile = new File(tempFilePath)
DCloud-yyl's avatar
DCloud-yyl 已提交
1327

DCloud-yyl's avatar
DCloud-yyl 已提交
1328 1329
		if (nextFile.exists() && nextFile.isDirectory()) {
			throw new UniError(FileSystemManagerUniErrorSubject, 1301003, msgPrefix + FileSystemManagerUniErrors.get(1301003)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1330 1331 1332 1333 1334
		}

		/**
		 * 如果上一级目录不存在,创建之
		 */
DCloud-yyl's avatar
DCloud-yyl 已提交
1335
		if (!nextFile.parentFile!.exists()) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1336 1337 1338
			nextFile.parentFile!.mkdirs()
		}

DCloud-yyl's avatar
DCloud-yyl 已提交
1339
		if (!nextFile.exists()) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1340 1341
			nextFile.createNewFile()
		}
DCloud-yyl's avatar
DCloud-yyl 已提交
1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
		// 写入文本,根据不同的编码内容写入不同的数据
		if (tempEncoding.toLowerCase() == 'ascii') {
			// 与微信保持一致,以UTF-16 BE首字节为ascii
			let charArray = data.toCharArray()
			let charNum = charArray.size
			let byteArray = ByteArray(charNum)
			let index = 0;
			for (; index < charNum; index++) {

				let perByte = charArray[index.toInt()].toChar().code.toByte()
				byteArray.set(index.toInt(), perByte)
			}
			nextFile.writeBytes(byteArray)
		} else if (tempEncoding.toLowerCase() == 'base64') {
			// base64
			nextFile.writeBytes(Base64.decode(data, Base64.NO_WRAP))
		} else {
			// utf-8 兜底
			nextFile.writeText(data, Charsets.UTF_8)
		}
DCloud-yyl's avatar
DCloud-yyl 已提交
1362 1363 1364 1365

	}

	public writeFile(options : WriteFileOptions) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1366 1367
		if (options.encoding.toLowerCase() != 'base64' && options.encoding.toLowerCase() != 'utf-8' && options.encoding.toLowerCase() != 'ascii') {
			let err = new FileSystemManagerFailImpl(1200002);
DCloud-yyl's avatar
DCloud-yyl 已提交
1368 1369 1370 1371
			options.fail?.(err)
			options.complete?.(err)
			return
		}
DCloud-yyl's avatar
DCloud-yyl 已提交
1372

DCloud-yyl's avatar
DCloud-yyl 已提交
1373 1374 1375
		// 判断type 是否合法
		let filePath = UTSAndroid.convert2AbsFullPath(options.filePath)

DCloud-yyl's avatar
DCloud-yyl 已提交
1376
		let isSandyBox = isSandyBoxPath(filePath, false)
DCloud-yyl's avatar
DCloud-yyl 已提交
1377
		if (!isSandyBox) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1378
			let err = new FileSystemManagerFailImpl(1300013);
DCloud-yyl's avatar
DCloud-yyl 已提交
1379 1380 1381 1382 1383
			options.fail?.(err)
			options.complete?.(err)
			return
		}

DCloud-yyl's avatar
DCloud-yyl 已提交
1384
		let nextFile = new File(filePath)
DCloud-yyl's avatar
DCloud-yyl 已提交
1385

DCloud-yyl's avatar
DCloud-yyl 已提交
1386
		if (nextFile.exists() && nextFile.isDirectory()) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1387
			// 出错了,目标文件已存在,并且是个目录
DCloud-yyl's avatar
DCloud-yyl 已提交
1388
			let err = new FileSystemManagerFailImpl(1301003);
DCloud-yyl's avatar
DCloud-yyl 已提交
1389 1390 1391 1392 1393 1394 1395
			options.fail?.(err)
			options.complete?.(err)
			return
		}


		let currentDispatcher = UTSAndroid.getDispatcher()
DCloud-yyl's avatar
DCloud-yyl 已提交
1396
		UTSAndroid.getDispatcher('io').async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1397 1398 1399 1400

			/**
			 * 如果上一级目录不存在,创建之
			 */
DCloud-yyl's avatar
DCloud-yyl 已提交
1401
			if (!nextFile.parentFile!.exists()) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1402 1403 1404
				nextFile.parentFile!.mkdirs()
			}

DCloud-yyl's avatar
DCloud-yyl 已提交
1405
			if (!nextFile.exists()) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1406 1407 1408
				nextFile.createNewFile()
			}
			// 写入文本,根据不同的编码内容写入不同的数据
DCloud-yyl's avatar
DCloud-yyl 已提交
1409
			if (options.encoding.toLowerCase() == 'ascii') {
DCloud-yyl's avatar
DCloud-yyl 已提交
1410 1411
				// 与微信保持一致,以UTF-16 BE首字节为ascii
				let charArray = options.data.toCharArray()
DCloud-yyl's avatar
DCloud-yyl 已提交
1412
				let charNum = charArray.size
DCloud-yyl's avatar
DCloud-yyl 已提交
1413 1414
				let byteArray = ByteArray(charNum)
				let index = 0;
DCloud-yyl's avatar
DCloud-yyl 已提交
1415 1416 1417
				for (; index < charNum; index++) {
					let perByte = charArray[index.toInt()].toChar().code.toByte()
					byteArray.set(index.toInt(), perByte)
DCloud-yyl's avatar
DCloud-yyl 已提交
1418 1419
				}
				nextFile.writeBytes(byteArray)
DCloud-yyl's avatar
DCloud-yyl 已提交
1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529
			} else if (options.encoding.toLowerCase() == 'base64') {
				// base64
				nextFile.writeBytes(Base64.decode(options.data, Base64.NO_WRAP))
			} else {
				// utf-8 兜底
				nextFile.writeText(options.data, Charsets.UTF_8)
			}


			let ret : FileManagerSuccessResult = {
				errMsg: "writeFile:ok"
			}
			currentDispatcher.async(function (_) {
				options.success?.(ret)
				options.complete?.(ret)
			})
		}, null)
	}

	public appendFileSync(filePath : string, data : string, encoding : string) {
		let msgPrefix = "appendFileSync:fail "
		let tempEncoding = encoding
		if (tempEncoding.toLowerCase() != 'base64' && tempEncoding.toLowerCase() != 'utf-8' && tempEncoding.toLowerCase() != 'ascii') {
			throw new UniError(FileSystemManagerUniErrorSubject, 1200002, msgPrefix + FileSystemManagerUniErrors.get(1200002)!)
		}
		// 判断type 是否合法
		let tempFilePath = UTSAndroid.convert2AbsFullPath(filePath)
		let isSandyBox = isSandyBoxPath(tempFilePath, false)
		if (!isSandyBox) {
			throw new UniError(FileSystemManagerUniErrorSubject, 1300013, msgPrefix + FileSystemManagerUniErrors.get(1300013)!)
		}
		let nextFile = new File(tempFilePath)
		if (!nextFile.exists()) {
			throw new UniError(FileSystemManagerUniErrorSubject, 1300002, msgPrefix + FileSystemManagerUniErrors.get(1300002)!)
		} else if (nextFile.isDirectory()) {
			// 出错了,目标文件已存在,并且是个目录
			throw new UniError(FileSystemManagerUniErrorSubject, 1301003, msgPrefix + FileSystemManagerUniErrors.get(1301003)!)
		}
		// 写入文本,根据不同的编码内容写入不同的数据
		if (tempEncoding.toLowerCase() == 'ascii') {
			// 与微信保持一致,以UTF-16 BE首字节为ascii
			let charArray = data.toCharArray()
			let charNum = charArray.size
			let byteArray = ByteArray(charNum)
			let index = 0;
			for (; index < charNum; index++) {
				let perByte = charArray[index.toInt()].toChar().code.toByte()
				byteArray.set(index.toInt(), perByte)
			}
			nextFile.appendBytes(byteArray)
		} else if (tempEncoding.toLowerCase() == 'base64') {
			// base64
			nextFile.appendBytes(Base64.decode(data, Base64.NO_WRAP))
		} else {
			// utf-8 兜底
			nextFile.appendText(data, Charsets.UTF_8)
		}
	}

	public appendFile(options : AppendFileOptions) {
		if (options.encoding.toLowerCase() != 'base64' && options.encoding.toLowerCase() != 'utf-8' && options.encoding.toLowerCase() != 'ascii') {
			let err = new FileSystemManagerFailImpl(1200002);
			options.fail?.(err)
			options.complete?.(err)
			return
		}

		// 判断type 是否合法
		let filePath = UTSAndroid.convert2AbsFullPath(options.filePath)

		let isSandyBox = isSandyBoxPath(filePath, false)
		if (!isSandyBox) {
			let err = new FileSystemManagerFailImpl(1300013);
			options.fail?.(err)
			options.complete?.(err)
			return
		}

		let nextFile = new File(filePath)


		if (!nextFile.exists()) {
			let err = new FileSystemManagerFailImpl(1300002);
			options.fail?.(err)
			options.complete?.(err)
			return
		} else if (nextFile.isDirectory()) {
			// 出错了,目标文件已存在,并且是个目录
			let err = new FileSystemManagerFailImpl(1301003);
			options.fail?.(err)
			options.complete?.(err)
			return
		}


		let currentDispatcher = UTSAndroid.getDispatcher("main")
		UTSAndroid.getDispatcher('io').async(function (_) {
			// 写入文本,根据不同的编码内容写入不同的数据
			if (options.encoding.toLowerCase() == 'ascii') {
				// 与微信保持一致,以UTF-16 BE首字节为ascii
				let charArray = options.data.toCharArray()
				let charNum = charArray.size
				let byteArray = ByteArray(charNum)
				let index = 0;
				for (; index < charNum; index++) {
					let perByte = charArray[index.toInt()].toChar().code.toByte()
					byteArray.set(index.toInt(), perByte)
				}
				nextFile.appendBytes(byteArray)
			} else if (options.encoding.toLowerCase() == 'base64') {
DCloud-yyl's avatar
DCloud-yyl 已提交
1530
				// base64
DCloud-yyl's avatar
DCloud-yyl 已提交
1531 1532
				nextFile.appendBytes(Base64.decode(options.data, Base64.NO_WRAP))
			} else {
DCloud-yyl's avatar
DCloud-yyl 已提交
1533
				// utf-8 兜底
DCloud-yyl's avatar
DCloud-yyl 已提交
1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792
				nextFile.appendText(options.data, Charsets.UTF_8)
			}


			let ret : FileManagerSuccessResult = {
				errMsg: "appendFile:ok"
			}
			currentDispatcher.async(function (_) {
				options.success?.(ret)
				options.complete?.(ret)
			}, null)
		}, null)
	}
	public open(options : OpenFileOptions) {
		let optFlag = options.flag.toLowerCase()
		if (optFlag != 'a'
			&& optFlag != 'ax'
			&& optFlag != 'a+'
			&& optFlag != 'ax+'
			&& optFlag != "r"
			&& optFlag != 'r+'
			&& optFlag != 'w'
			&& optFlag != 'wx'
			&& optFlag != 'w+'
			&& optFlag != 'wx'
			&& optFlag != 'wx+') {
			let err = new FileSystemManagerFailImpl(1302003);
			options.fail?.(err)
			options.complete?.(err)
			return
		}

		let filePath = UTSAndroid.convert2AbsFullPath(options.filePath)

		let isSandyBox = isSandyBoxPath(filePath, false)
		if (!isSandyBox) {
			let err = new FileSystemManagerFailImpl(1300013);
			options.fail?.(err)
			options.complete?.(err)
			return
		}

		this.fileDesUtil.open(options, new File(filePath))

	}
	public openSync(options : OpenFileSyncOptions) : string {
		let msgPrefix = "openSync:fail "
		let optFlag = options.flag.toLowerCase()
		if (optFlag != 'a'
			&& optFlag != 'ax'
			&& optFlag != 'a+'
			&& optFlag != 'ax+'
			&& optFlag != "r"
			&& optFlag != 'r+'
			&& optFlag != 'w'
			&& optFlag != 'wx'
			&& optFlag != 'w+'
			&& optFlag != 'wx'
			&& optFlag != 'wx+') {
			throw new UniError(FileSystemManagerUniErrorSubject, 1302003, msgPrefix + FileSystemManagerUniErrors.get(1302003)!)
		}

		let filePath = UTSAndroid.convert2AbsFullPath(options.filePath)

		let isSandyBox = isSandyBoxPath(filePath, false)
		if (!isSandyBox) {
			throw new UniError(FileSystemManagerUniErrorSubject, 1300013, msgPrefix + FileSystemManagerUniErrors.get(1300013)!)
		}
		return this.fileDesUtil.openSync(options, new File(filePath))
	}

	public saveFile(options : SaveFileOptions) {
		let currentDispatcher = UTSAndroid.getDispatcher("main")
		UTSAndroid.getDispatcher('io').async(function (_) {
			// 检查来源文件
			let tempFilePath = UTSAndroid.convert2AbsFullPath(options.tempFilePath)
			let isSandyBox = isSandyBoxPath(tempFilePath, true)

			if (!isSandyBox) {
				currentDispatcher.async(function (_) {
					let err = new FileSystemManagerFailImpl(1300013);
					options.fail?.(err)
					options.complete?.(err)
				}, null)
				return
			}
			let targetFile = new File(tempFilePath)
			if (!targetFile.exists()) {
				/**
				 * 文件不存在
				 */
				currentDispatcher.async(function (_) {
					let err = new FileSystemManagerFailImpl(1300002);
					options.fail?.(err)
					options.complete?.(err)
				}, null)
				return
			}

			if (options.filePath == null) {
				options.filePath = uni.env.USER_DATA_PATH
			}
			// 检查目标文件
			let newFilePath = UTSAndroid.convert2AbsFullPath(options.filePath! + targetFile.getName())
			isSandyBox = isSandyBoxPath(newFilePath, false)
			if (!isSandyBox) {
				currentDispatcher.async(function (_) {
					let err = new FileSystemManagerFailImpl(1300013);
					options.fail?.(err)
					options.complete?.(err)
				}, null)
				return
			}
			let newFile = new File(newFilePath)
			console.log(newFile.getPath())
			if (newFile.getParentFile() != null && !newFile.getParentFile()!.exists()) {
				/**
				 * 父文件不存在
				 */
				currentDispatcher.async(function (_) {
					let err = new FileSystemManagerFailImpl(1300002);
					options.fail?.(err)
					options.complete?.(err)
				}, null)
				return
			}

			let copyRetFile = targetFile.copyTo(newFile, true)
			if (!copyRetFile.exists()) {
				// 调用系统api 失败
				currentDispatcher.async(function (_) {
					let err = new FileSystemManagerFailImpl(1300201);
					options.fail?.(err)
					options.complete?.(err)
				}, null)
				return
			}
			targetFile.delete()
			let ret : SaveFileSuccessResult = {
				savedFilePath: newFile.getPath()
			}

			currentDispatcher.async(function (_) {
				options.success?.(ret)
				options.complete?.(ret)
			}, null)

		}, null)
	}
	public saveFileSync(tempFilePath : string, filePath : string | null) : string {
		let msgPrefix = "saveFileSync:fail "
		// 检查来源文件
		let tfPath = UTSAndroid.convert2AbsFullPath(tempFilePath)
		let isSandyBox = isSandyBoxPath(tfPath, true)

		if (!isSandyBox) {
			throw new UniError(FileSystemManagerUniErrorSubject, 1300013, msgPrefix + FileSystemManagerUniErrors.get(1300013)!)
		}
		let targetFile = new File(tfPath)
		if (!targetFile.exists()) {
			/**
			 * 文件不存在
			 */
			throw new UniError(FileSystemManagerUniErrorSubject, 1300002, msgPrefix + FileSystemManagerUniErrors.get(1300002)!)
		}
		let fp = filePath
		if (fp == null) {
			fp = uni.env.USER_DATA_PATH
		}
		// 检查目标文件
		let newFilePath = UTSAndroid.convert2AbsFullPath(fp + targetFile.getName())
		isSandyBox = isSandyBoxPath(newFilePath, false)
		if (!isSandyBox) {
			throw new UniError(FileSystemManagerUniErrorSubject, 1300013, msgPrefix + FileSystemManagerUniErrors.get(1300013)!)
		}
		let newFile = new File(newFilePath)
		console.log(newFile.getPath())
		if (newFile.getParentFile() != null && !newFile.getParentFile()!.exists()) {
			/**
			 * 父文件不存在
			 */
			throw new UniError(FileSystemManagerUniErrorSubject, 1300002, msgPrefix + FileSystemManagerUniErrors.get(1300002)!)
		}

		let copyRetFile = targetFile.copyTo(newFile, true)
		if (!copyRetFile.exists()) {
			// 调用系统api 失败
			throw new UniError(FileSystemManagerUniErrorSubject, 1300201, msgPrefix + FileSystemManagerUniErrors.get(1300201)!)
		}
		targetFile.delete()
		return newFilePath
	}
	public unzip(options : UnzipFileOptions) {
		let msgPrefix = "unzip:fail "
		// 检查来源文件
		let zipFilePath = UTSAndroid.convert2AbsFullPath(options.zipFilePath)
		
		let currentDispatcher = UTSAndroid.getDispatcher("main")
		UTSAndroid.getDispatcher('io').async(function (_) {
			let is:InputStream;
			let targetPath = UTSAndroid.convert2AbsFullPath(options.targetPath)
			if (zipFilePath.startsWith("/android_asset/")) {
				try {
					let assetName = zipFilePath.substring("/android_asset/".length)
					is = UTSAndroid.getAppContext()!.getResources().getAssets().open(assetName);
				} catch (e : Exception) {
					/**
					 * 文件不存在
					 */
					currentDispatcher.async(function (_) {
						let err = new FileSystemManagerFailImpl(1300002);
						options.fail?.(err)
						options.complete?.(err)
					}, null)
					return
				}
			} else {
				let isSandyBox = isSandyBoxPath(zipFilePath, true)
				if (!isSandyBox) {
					currentDispatcher.async(function (_) {
						let err = new FileSystemManagerFailImpl(1300013);
						options.fail?.(err)
						options.complete?.(err)
					}, null)
					return
				}
				let zipFile = new File(zipFilePath)
				if (!zipFile.exists()) {
					/**
					 * 文件不存在
					 */
					currentDispatcher.async(function (_) {
						let err = new FileSystemManagerFailImpl(1300002);
						options.fail?.(err)
						options.complete?.(err)
					}, null)
					return
				}
				
				isSandyBox = isSandyBoxPath(targetPath, true)
				let targetFile = new File(targetPath)
				if (!isSandyBox) {
					currentDispatcher.async(function (_) {
						let err = new FileSystemManagerFailImpl(1300013);
						options.fail?.(err)
						options.complete?.(err)
					}, null)
					return
				
				}
				if (!targetFile.exists() || !targetFile.isDirectory()) {
					currentDispatcher.async(function (_) {
						let err = new FileSystemManagerFailImpl(1300002);
						options.fail?.(err)
						options.complete?.(err)
					}, null)
					return
				}
				is = new FileInputStream(zipFile);
DCloud-yyl's avatar
DCloud-yyl 已提交
1793 1794
			}
			
DCloud-yyl's avatar
DCloud-yyl 已提交
1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051
			try {
				let zis = new ZipInputStream(new BufferedInputStream(is));
				let buffer = new ByteArray(1024)
				let count : number = 0;
				let ze = zis.getNextEntry()
				while (ze != null) {
					// ze 是每个条目,我们需要获取对应的文件名
					let filename = ze.getName();

					// 若是文件夹,直接创建
					if (ze.isDirectory()) {
						let fmd = new File(targetPath + "/" + filename);
						fmd.mkdirs();
						ze = zis.getNextEntry()
						continue;
					}
					if (filename.startsWith("__MACOSX")) {//mac os上压缩的zip会产生该文件夹
						ze = zis.getNextEntry()
						continue;
					}
					// // 否则我们将文件提取出来
					let fout = new FileOutputStream(targetPath + "/" + filename);
					console.log(targetPath + "/" + filename)
					// 将压缩文件内容写入到这个文件中
					let count = zis.read(buffer)
					while (count != -1) {
						fout.write(buffer, 0, count);
						count = zis.read(buffer)
					}
					fout.close();
					zis.closeEntry();
					ze = zis.getNextEntry()
				}

				zis.close();
				let ret : FileManagerSuccessResult = {
					errMsg: "unzip:ok"
				}
				currentDispatcher.async(function (_) {
					options.success?.(ret)
					options.complete?.(ret)
				}, null)
			} catch (e : Exception) {
				console.log(e.message)
				currentDispatcher.async(function (_) {
					let err = new FileSystemManagerFailImpl(1300201);
					options.fail?.(err)
					options.complete?.(err)
				}, null)
				return
			}
		}, null)

	}

	public getSavedFileList(options : GetSavedFileListOptions) {

		let currentDispatcher = UTSAndroid.getDispatcher("main")
		UTSAndroid.getDispatcher('io').async(function (_) {
			let filePath = UTSAndroid.convert2AbsFullPath(uni.env.USER_DATA_PATH)

			let targetFile = new File(filePath)
			if (!targetFile.exists() || !targetFile.isDirectory()) {
				/**
				 * 文件不存在,或者不是文件夹,异常
				 */
				currentDispatcher.async(function (_) {
					let err = new FileSystemManagerFailImpl(1300002);
					options.fail?.(err)
					options.complete?.(err)
				}, null)
				return
			}
			let res : Array<string> = []
			targetFile.walk()
				.onEnter(function (file : File) : boolean {
					if (file.isDirectory()) {
						return true
					}
					return false
				})
				.iterator()
				.forEach(function (file : File) {
					if (!file.isDirectory()) {
						res.add(file.getPath())
					}
				})

			let success : GetSavedFileListResult = {
				fileList: UTSArray.fromNative(res)
			}
			currentDispatcher.async(function (_) {
				options.success?.(success)
				options.complete?.(success)
			}, null)
		}, null)
	}
	public truncate(options : TruncateFileOptions) {
		let currentDispatcher = UTSAndroid.getDispatcher("main")
		UTSAndroid.getDispatcher('io').async(function (_) {
			let filePath = UTSAndroid.convert2AbsFullPath(options.filePath)

			let targetFile = new File(filePath)
			if (!targetFile.exists() || targetFile.isDirectory()) {
				/**
				 * 文件不存在,或者是文件夹,异常
				 */
				currentDispatcher.async(function (_) {
					let err = new FileSystemManagerFailImpl(1300002);
					options.fail?.(err)
					options.complete?.(err)
				}, null)
				return
			}

			let content = targetFile.readText()
			if (content.length > options.length) {
				let truncatedContent = content.substring(0, options.length)
				targetFile.writeText(truncatedContent)
			}

			let success : FileManagerSuccessResult = {
				errMsg: "truncateFile:ok"
			}
			currentDispatcher.async(function (_) {
				options.success?.(success)
				options.complete?.(success)
			}, null)
		}, null)
	}

	truncateSync(filePath : string, length ?: number) {
		let msgPrefix = 'truncateSync:fail '
		let tempLength = length
		if (tempLength == null) {
			tempLength = 0
		}
		let tempFilePath = UTSAndroid.convert2AbsFullPath(filePath)

		let targetFile = new File(tempFilePath)
		if (!targetFile.exists() || targetFile.isDirectory()) {
			/**
			 * 文件不存在,或者是文件夹,异常
			 */
			throw new UniError(FileSystemManagerUniErrorSubject, 1300002, msgPrefix + FileSystemManagerUniErrors.get(1300002)!)
		}

		let content = targetFile.readText()
		if (content.length > tempLength) {
			let truncatedContent = content.substring(0, tempLength)
			targetFile.writeText(truncatedContent)
		}
	}

	readCompressedFile(options : ReadCompressedFileOptions) {
		let currentDispatcher = UTSAndroid.getDispatcher("main")
		UTSAndroid.getDispatcher('io').async(function (_) {
			let success : ReadCompressedFileResult = {
				data: ""
			}
			let is:InputStream
			let filePath = UTSAndroid.convert2AbsFullPath(options.filePath)
			if (filePath.startsWith("/android_asset/")) {
				try {
					let assetName = filePath.substring("/android_asset/".length)
					is = UTSAndroid.getAppContext()!.getResources().getAssets().open(assetName);
				
				} catch (e : Exception) {
					/**
					 * 文件不存在,或者是文件夹,异常
					 */
					currentDispatcher.async(function (_) {
						let err = new FileSystemManagerFailImpl(1300002);
						options.fail?.(err)
						options.complete?.(err)
					}, null)
					return
				}
			} else {
				let targetFile = new File(filePath)
				if (!targetFile.exists() || targetFile.isDirectory()) {
					/**
					 * 文件不存在,或者是文件夹,异常
					 */
					currentDispatcher.async(function (_) {
						let err = new FileSystemManagerFailImpl(1300002);
						options.fail?.(err)
						options.complete?.(err)
					}, null)
					return
				}
				if (options.compressionAlgorithm != 'br') {
					currentDispatcher.async(function (_) {
						let err = new FileSystemManagerFailImpl(1301111);
						options.fail?.(err)
						options.complete?.(err)
					}, null)
					return
				}
				is = new FileInputStream(targetFile)

			}
			try {
				let brInput = new BrotliInputStream(is)
				success.data = new String(brInput.readBytes())
				brInput.close()
			} catch (e : Exception) {
				currentDispatcher.async(function (_) {
					let err = new FileSystemManagerFailImpl(1300201);
					options.fail?.(err)
					options.complete?.(err)
				}, null)
				return
			}
			currentDispatcher.async(function (_) {
				options.success?.(success)
				options.complete?.(success)
			}, null)
		}, null)
	}

	readCompressedFileSync(filePath : string, compressionAlgorithm : string) : string {
		let msgPrefix = "readCompressedFileSync:fail "
		let tempFilePath = UTSAndroid.convert2AbsFullPath(filePath)
		let is : InputStream
		if (tempFilePath.startsWith("/android_asset/")) {
			try {
				let assetName = tempFilePath.substring("/android_asset/".length)
				is = UTSAndroid.getAppContext()!.getResources().getAssets().open(assetName);

			} catch (e : Exception) {
				throw new UniError(FileSystemManagerUniErrorSubject, 1300002, msgPrefix + FileSystemManagerUniErrors.get(1300002)!)
			}
		} else {
			let targetFile = new File(tempFilePath)
			if (!targetFile.exists() || targetFile.isDirectory()) {
				/**
				 * 文件不存在,或者是文件夹,异常
				 */
				throw new UniError(FileSystemManagerUniErrorSubject, 1300002, msgPrefix + FileSystemManagerUniErrors.get(1300002)!)
			}
			if (compressionAlgorithm != 'br') {
				throw new UniError(FileSystemManagerUniErrorSubject, 1301111, msgPrefix + FileSystemManagerUniErrors.get(1301111)!)
			}
			is = new FileInputStream(targetFile)
		}
		let data : string

		try {
			let brInput = new BrotliInputStream(is)
			data = new String(brInput.readBytes())
			brInput.close()
		} catch (e : Exception) {
			throw new UniError(FileSystemManagerUniErrorSubject, 1300201, msgPrefix + FileSystemManagerUniErrors.get(1300201)!)
		}
		return data
	}
DCloud-yyl's avatar
DCloud-yyl 已提交
2052

DCloud-yyl's avatar
DCloud-yyl 已提交
2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133
	removeSavedFile(options : RemoveSavedFileOptions) {
		let currentDispatcher = UTSAndroid.getDispatcher("main")
		UTSAndroid.getDispatcher('io').async(function (_) {
			let filePath = UTSAndroid.convert2AbsFullPath(options.filePath)

			let targetFile = new File(filePath)
			if (!targetFile.exists() || targetFile.isDirectory()) {
				/**
				 * 文件不存在,或者是文件夹,异常
				 */
				currentDispatcher.async(function (_) {
					let err = new FileSystemManagerFailImpl(1300002);
					options.fail?.(err)
					options.complete?.(err)
				}, null)
				return
			}
			targetFile.delete()
			currentDispatcher.async(function (_) {
				let success : FileManagerSuccessResult = {
					errMsg: "removeSavedFile:ok"
				}
				options.success?.(success)
				options.complete?.(success)
			}, null)
		}, null)
	}

	public write(options : WriteOptions) {
		console.log(JSON.stringify(options))
		if (options.encoding.toLowerCase() != 'base64' && options.encoding.toLowerCase() != 'utf-8' && options.encoding.toLowerCase() != 'ascii') {
			let err = new FileSystemManagerFailImpl(1200002);
			options.fail?.(err)
			options.complete?.(err)
			return
		}


		let currentDispatcher = UTSAndroid.getDispatcher()
		UTSAndroid.getDispatcher('io').async(function (_) {

			let fd = ParcelFileDescriptor.fromFd(options.fd.toInt())
			if (fd == null) {
				let err = new FileSystemManagerFailImpl(1300009);
				options.fail?.(err)
				options.complete?.(err)
				return
			}
			let outStream = new FileOutputStream(fd.getFileDescriptor())
			let fileChannel = outStream.getChannel()
			//创建ByteBuffer并写入数据
			let buffer = ByteBuffer.allocate(64)
			buffer.clear()
			// 写入文本,根据不同的编码内容写入不同的数据
			if (options.encoding.toLowerCase() == 'ascii') {
				// 与微信保持一致,以UTF-16 BE首字节为ascii
				let charArray = options.data.toCharArray()
				let charNum = charArray.size
				let byteArray = ByteArray(charNum)
				let index = 0;
				for (; index < charNum; index++) {
					let perByte = charArray[index.toInt()].toChar().code.toByte()
					byteArray.set(index.toInt(), perByte)
				}
				buffer.put(byteArray)
			} else if (options.encoding.toLowerCase() == 'base64') {
				// base64
				buffer.put(Base64.decode(options.data, Base64.NO_WRAP))
			} else {
				// utf-8 兜底
				buffer.put(options.data.toByteArray())
			}

			buffer.flip()
			while (buffer.hasRemaining()) {
				fileChannel.write(buffer, 0)
			}
			let ret : WriteResult = {
				bytesWritten: options.data.length
			}
			currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
2134 2135 2136
				options.success?.(ret)
				options.complete?.(ret)
			})
DCloud-yyl's avatar
DCloud-yyl 已提交
2137
		}, null)
DCloud-yyl's avatar
DCloud-yyl 已提交
2138 2139
	}

DCloud-yyl's avatar
DCloud-yyl 已提交
2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376
	public writeSync(options : WriteSyncOptions) : WriteResult {
		let msgPrefix = "writeSync:fail "
		if (options.encoding.toLowerCase() != 'base64' && options.encoding.toLowerCase() != 'utf-8' && options.encoding.toLowerCase() != 'ascii') {
			throw new UniError(FileSystemManagerUniErrorSubject, 1200002, msgPrefix + FileSystemManagerUniErrors.get(1200002)!)
		}
		let fd = ParcelFileDescriptor.fromFd(options.fd.toInt())
		if (fd == null) {
			throw new UniError(FileSystemManagerUniErrorSubject, 1300009, msgPrefix + FileSystemManagerUniErrors.get(1300009)!)
		}
		try {
			let outStream = new FileOutputStream(fd.getFileDescriptor())
			let fileChannel = outStream.getChannel()
			//创建ByteBuffer并写入数据
			let buffer = ByteBuffer.allocate(64)
			buffer.clear()
			// 写入文本,根据不同的编码内容写入不同的数据
			if (options.encoding.toLowerCase() == 'ascii') {
				// 与微信保持一致,以UTF-16 BE首字节为ascii
				let charArray = options.data.toCharArray()
				let charNum = charArray.size
				let byteArray = ByteArray(charNum)
				let index = 0;
				for (; index < charNum; index++) {
					let perByte = charArray[index.toInt()].toChar().code.toByte()
					byteArray.set(index.toInt(), perByte)
				}
				buffer.put(byteArray)
			} else if (options.encoding.toLowerCase() == 'base64') {
				// base64
				buffer.put(Base64.decode(options.data, Base64.NO_WRAP))
			} else {
				// utf-8 兜底
				buffer.put(options.data.toByteArray())
			}
			buffer.flip()
			while (buffer.hasRemaining()) {
				fileChannel.write(buffer, 0)
			}
		} catch (e) {
			throw new UniError(FileSystemManagerUniErrorSubject, 1300201, msgPrefix + FileSystemManagerUniErrors.get(1300201)!)
		}
		let ret : WriteResult = {
			bytesWritten: options.data.length
		}
		return ret
	}
	public close(options : CloseOptions) {
		let fd = ParcelFileDescriptor.fromFd(options.fd.toInt())
		UTSAndroid.getDispatcher('io').async(function (_) {
			try {
				fd.close()
				let success : FileManagerSuccessResult = {
					errMsg: "close:ok",
				}
				if (this.fileDesUtil.openMap.has(options.fd)) {
					this.fileDesUtil.openMap.delete(options.fd)
				}

				options.success?.(success)
				options.complete?.(success)
			} catch (e) {
				let err = new FileSystemManagerFailImpl(1300009);
				options.fail?.(err)
				options.complete?.(err)
			}
		})
	}
	public closeSync(options : CloseSyncOptions) {
		let msgPrefix = "closeSync:fail "
		let fd = ParcelFileDescriptor.fromFd(options.fd.toInt())
		try {
			fd.close()
			if (this.fileDesUtil.openMap.has(options.fd)) {
				this.fileDesUtil.openMap.delete(options.fd)
			}
		} catch (e) {
			throw new UniError(FileSystemManagerUniErrorSubject, 1300009, msgPrefix + FileSystemManagerUniErrors.get(1300009)!)
		}
	}
	public fstat(options : FStatOptions) {
		let currentDispatcher = UTSAndroid.getDispatcher("main")
		UTSAndroid.getDispatcher('io').async(function (_) {
			currentDispatcher.async(function (_) {
				if (!this.fileDesUtil.openMap.has(options.fd)) {
					currentDispatcher.async(function (_) {
						let err = new FileSystemManagerFailImpl(1300009);
						options.fail?.(err)
						options.complete?.(err)
					}, null)
					return
				}
				let success : FStatSuccessResult = {
					stats: wrapStats(this.fileDesUtil.openMap.get(options.fd)!)
				}
				options.success?.(success)
				options.complete?.(success)
			}, null)
		}, null)
	}
	public fstatSync(options : FStatSyncOptions) : Stats {
		let msgPrefix = "ftruncateSync:fail "
		if (!this.fileDesUtil.openMap.has(options.fd)) {
			throw new UniError(FileSystemManagerUniErrorSubject, 1300009, msgPrefix + FileSystemManagerUniErrors.get(1300009)!)
		}
		return wrapStats(this.fileDesUtil.openMap.get(options.fd)!)
	}
	public ftruncate(options : FTruncateFileOptions) {
		let currentDispatcher = UTSAndroid.getDispatcher("main")
		UTSAndroid.getDispatcher('io').async(function (_) {
			if (!this.fileDesUtil.openMap.has(options.fd)) {
				currentDispatcher.async(function (_) {
					let err = new FileSystemManagerFailImpl(1300009);
					options.fail?.(err)
					options.complete?.(err)
				}, null)
				return
			}
			let targetFile = this.fileDesUtil.openMap.get(options.fd)
			let content = targetFile!.readText()
			if (content.length > options.length) {
				let truncatedContent = content.substring(0, options.length)
				targetFile.writeText(truncatedContent)
			}

			let success : FileManagerSuccessResult = {
				errMsg: "ftruncate:ok"
			}
			currentDispatcher.async(function (_) {
				options.success?.(success)
				options.complete?.(success)
			}, null)
		}, null)
	}
	public ftruncateSync(options : FTruncateFileSyncOptions) {
		let msgPrefix = "ftruncateSync:fail "
		if (!this.fileDesUtil.openMap.has(options.fd)) {
			throw new UniError(FileSystemManagerUniErrorSubject, 1300009, msgPrefix + FileSystemManagerUniErrors.get(1300009)!)
		}
		let targetFile = this.fileDesUtil.openMap.get(options.fd)
		let content = targetFile!.readText()
		if (content.length > options.length) {
			let truncatedContent = content.substring(0, options.length)
			targetFile.writeText(truncatedContent)
		}
	}

	public readZipEntry(options : ReadZipEntryOptions) {
		let targetPath = uni.env.CACHE_PATH + "/" + Base64.encodeToString(options.filePath.toByteArray(), Base64.NO_WRAP)
		targetPath = UTSAndroid.convert2AbsFullPath(targetPath)
		console.log("readZipEntry", targetPath)
		let file : File = new File(targetPath)
		if (!file.exists()) {
			file.mkdirs()
		}

		let target = this
		let zipOptions : UnzipFileOptions = {//todo 需要去掉
			zipFilePath: options.filePath,
			targetPath: targetPath,
			success: (_) => {
				let currentDispatcher = UTSAndroid.getDispatcher("main")
				UTSAndroid.getDispatcher('io').async(function (_) {
					let targetFile = new File(targetPath)
					let resultMap : Map<string, ZipFileItem> = new Map()
					let filterEntries = options.entries

					filterEntries?.forEach((item) => {
						let fileItem = new File(targetPath + "/" + item.path)
						if (!fileItem.exists() || fileItem.isDirectory()) {
							let zipFileItem : ZipFileItem = {
								errMsg: 'no such file'
							}
							resultMap.set(item.path, zipFileItem)
						}
					})

					targetFile.walk()
						.onEnter(function (file : File) : boolean {
							if (file.isDirectory()) {
								return true
							}
							return false
						})
						.iterator()
						.forEach(function (file : File) {
							if (options.entries == null) {

								if (!file.isDirectory()) {
									let data = target.readFileSync(file.getPath(), options.encoding)
									let zipFileItem : ZipFileItem = {
										data: data,
										errMsg: 'readZipEntry:ok'
									}
									resultMap.set(file.getPath().substring(targetPath.length + 1), zipFileItem)
								}
							} else {
								if (!file.isDirectory()) {
									filterEntries?.forEach((item) => {
										let entryPath = targetPath + "/" + item.path
										console.log("readZipEntry", file.getPath())
										if (entryPath == file.getPath()) {
											let data = target.readFileSync(file.getPath(), item.encoding)
											let zipFileItem : ZipFileItem = {
												data: data,
												errMsg: 'readZipEntry:ok'
											}
											resultMap.set(item.path, zipFileItem)
										}
									})

								}

							}

						})

					let success : EntriesResult = {
						result: resultMap
					}
					currentDispatcher.async(function (_) {
						options.success?.(success)
						options.complete?.(success)
					}, null)
					targetFile.delete()
				})
			},
			fail: (res) => {
				let err = new FileSystemManagerFailImpl(res.errCode);
				options.fail?.(err)
				options.complete?.(err)
			}
		}
		this.unzip(zipOptions)
	}
}
export const getFileSystemManager : GetFileSystemManager = function () : FileSystemManager {
	return AndroidFileSystemManager.getManager()
DCloud-yyl's avatar
DCloud-yyl 已提交
2377 2378 2379 2380 2381
};

/**
 * 判断传入的路径是否是沙盒路径,如果不是则不能继续操作
 */
DCloud-yyl's avatar
DCloud-yyl 已提交
2382 2383 2384 2385 2386 2387 2388 2389 2390
function isSandyBoxPath(inputPath : string, onlyRead : boolean) : boolean {
	//处理非沙盒目录时 需要校验一下MANAGE_EXTERNAL_STORAGE权限 如果授权则可任意操作
	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
		if (Environment.isExternalStorageManager()) {
			console.log('Environment.isExternalStorageManager()')
			return true
		}
	}

DCloud-yyl's avatar
DCloud-yyl 已提交
2391 2392 2393 2394
	/**
	 * 第一条规则,判断是否操作的是应用资源目录。开发者具备该目录下读取的权限操作
	 */
	let appResRoot = UTSAndroid.convert2AbsFullPath(uni.env.APP_RESOURCE_PATH)
DCloud-yyl's avatar
DCloud-yyl 已提交
2395
	if (inputPath.startsWith(appResRoot)) {
DCloud-yyl's avatar
DCloud-yyl 已提交
2396
		// 路径是应用资源目录
DCloud-yyl's avatar
DCloud-yyl 已提交
2397
		if (onlyRead) {
DCloud-yyl's avatar
DCloud-yyl 已提交
2398 2399
			// 并且是读取操作,返回
			return true
DCloud-yyl's avatar
DCloud-yyl 已提交
2400
		} else {
DCloud-yyl's avatar
DCloud-yyl 已提交
2401 2402 2403 2404 2405 2406 2407 2408
			// 不是读取
			return false
		}
	}
	/**
	 * 第二条规则,是否属于应用根目录 SANDBOX_PATH
	 */
	let sandyBoxRoot = UTSAndroid.convert2AbsFullPath(uni.env.SANDBOX_PATH)
DCloud-yyl's avatar
DCloud-yyl 已提交
2409
	if (inputPath.startsWith(sandyBoxRoot)) {
DCloud-yyl's avatar
DCloud-yyl 已提交
2410 2411 2412 2413 2414 2415 2416 2417
		// 是否是应用根目录
		return true
	}

	/**
	 * 第三条规则,是否属于应用内置根目录 ANDROID_INTERNAL_SANDBOX_PATH
	 */
	let innerSandyBoxRoot = UTSAndroid.convert2AbsFullPath(uni.env.ANDROID_INTERNAL_SANDBOX_PATH)
DCloud-yyl's avatar
DCloud-yyl 已提交
2418
	if (inputPath.startsWith(innerSandyBoxRoot)) {
DCloud-yyl's avatar
DCloud-yyl 已提交
2419 2420 2421
		// 是否是应用根目录
		return true
	}
DCloud-yyl's avatar
DCloud-yyl 已提交
2422 2423


DCloud-yyl's avatar
DCloud-yyl 已提交
2424 2425 2426 2427
	return false
}


DCloud-yyl's avatar
DCloud-yyl 已提交
2428 2429
function wrapStats(sourceFile : File) : Stats {
	let stats = new AndroidStats()
DCloud-yyl's avatar
DCloud-yyl 已提交
2430 2431
	stats.mIsFile = sourceFile.isFile()
	stats.lastModifiedTime = sourceFile.lastModified()
DCloud-yyl's avatar
DCloud-yyl 已提交
2432 2433
	stats.mode = getFileMode(sourceFile)
	if (sourceFile.isFile()) {
DCloud-yyl's avatar
DCloud-yyl 已提交
2434 2435 2436 2437
		stats.size = sourceFile.length()
	}
	return stats
}
DCloud-yyl's avatar
DCloud-yyl 已提交
2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468

function getFileMode(sourceFile : File) : number {
	let res = "0"
	if (sourceFile.isFile()) {
		res += "10"
	} else if (sourceFile.isDirectory()) {
		res += "04"
	}
	//let file=new File("")
	// S_IRUSR (00400):所有者读权限。
	// S_IWUSR (00200):所有者写权限。
	// S_IXUSR (00100):所有者执行权限。
	//S_IRWXU (00700):所有者的权限。100644

	let permission = 7
	if (sourceFile.canRead() && sourceFile.canWrite() && sourceFile.canExecute()) {
		permission = 7
	} else {
		if (!sourceFile.canRead()) {
			permission = permission - 4
		}
		if (!sourceFile.canWrite()) {
			permission = permission - 2
		}
		if (!sourceFile.canExecute()) {
			permission = permission - 1
		}
	}
	res += "0" + permission + "00"
	return Integer.parseInt(res, 8)
}