index.uts 72.4 KB
Newer Older
DCloud-yyl's avatar
DCloud-yyl 已提交
1 2 3
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"
DCloud-yyl's avatar
DCloud-yyl 已提交
4
import { UniErrorSubject, UniErrors } from "../unierror.uts"
DCloud-yyl's avatar
DCloud-yyl 已提交
5 6 7 8 9 10 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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
import { FileDescriptorUtil } from "./FileDescriptorUtil"
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"
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';

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
	}

}
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 (_) {

			currentDispatcher.async(function (_) {

				let filePath = UTSAndroid.convert2AbsFullPath(options.path)

				let isSandyBox = isSandyBoxPath(filePath, true)
				if (!isSandyBox) {
					currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
99
						let err = new UniError(UniErrorSubject, 1300013, UniErrors.get(1300013)! + ":" + options.path);
DCloud-yyl's avatar
DCloud-yyl 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112
						options.fail?.(err)
						options.complete?.(err)
					}, null)
					return
				}

				let targetFile = new File(filePath)

				if (!targetFile.exists()) {
					/**
					 * 文件不存在
					 */
					currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
113
						let err = new UniError(UniErrorSubject, 1300002, UniErrors.get(1300002)! + ":" + options.path);
DCloud-yyl's avatar
DCloud-yyl 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
						options.fail?.(err)
						options.complete?.(err)
					}, null)
					return
				}

				if (options.recursive == 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)
						})


					let success : StatSuccessResult = {
						errMsg: "stat:ok",
						stats: res
					}
					options.success?.(success)
					options.complete?.(success)


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

					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)
					options.success?.(success)
					options.complete?.(success)
				}


			}, null)
		}, null)
	}

	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) {
DCloud-yyl's avatar
DCloud-yyl 已提交
181
			throw new UniError(UniErrorSubject, 1300013, msgPrefix + UniErrors.get(1300013)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
182 183 184 185 186 187
		}
		let targetFile = new File(filePath)
		if (!targetFile.exists()) {
			/**
			 * 文件不存在
			 */
DCloud-yyl's avatar
DCloud-yyl 已提交
188
			throw new UniError(UniErrorSubject, 1300002, msgPrefix + UniErrors.get(1300002)!);
DCloud-yyl's avatar
DCloud-yyl 已提交
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 231 232 233 234 235
		}
		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 (_) {

			let filePath = UTSAndroid.convert2AbsFullPath(options.filePath)

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

			let targetFile = new File(filePath)

			if (!targetFile.exists()) {
				/**
				 * 文件不存在
				 */
				currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
250
					let err = new UniError(UniErrorSubject, 1300002, UniErrors.get(1300002)! + ":" + options.filePath);
DCloud-yyl's avatar
DCloud-yyl 已提交
251 252 253 254 255 256 257 258 259 260 261
					options.fail?.(err)
					options.complete?.(err)
				}, null)
				return
			}

			if (targetFile.isDirectory()) {
				/**
				 * 文件是个目录
				 */
				currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
262
					let err = new UniError(UniErrorSubject, 1300021, UniErrors.get(1300021)! + ":" + options.filePath);
DCloud-yyl's avatar
DCloud-yyl 已提交
263 264 265 266 267 268 269 270 271 272 273 274 275 276
					options.fail?.(err)
					options.complete?.(err)
				}, null)
				return
			}

			if (options.digestAlgorithm == null) {
				options.digestAlgorithm = "md5"
			}
			if (options.digestAlgorithm!.toLowerCase() != 'md5' && options.digestAlgorithm!.toLowerCase() != 'sha1') {
				/**
				 * invalid digestAlgorithm
				 */
				currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
277
					let err = new UniError(UniErrorSubject, 1300022, UniErrors.get(1300022)! + ":invalid digestAlgorithm " + options.digestAlgorithm);
DCloud-yyl's avatar
DCloud-yyl 已提交
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
					options.fail?.(err)
					options.complete?.(err)
				}, null)
				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)
				if (len != -1) {
					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();

			currentDispatcher.async(function (_) {
				let success : GetFileInfoSuccessResult = {
					digest: sign,
					size: fileLen,
					errMsg: "getFileInfo:ok"
				}
				options.success?.(success)
				options.complete?.(success)
			}, null)
		}, null)

	}


	public copyFileSync(srcPath : string, destPath : string) : void {
		// 检查来源文件
		let msgPrefix = "copyFileSync:fail "
		let filePath = UTSAndroid.convert2AbsFullPath(srcPath)
		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) {
DCloud-yyl's avatar
DCloud-yyl 已提交
343
				throw new UniError(UniErrorSubject, 1300201, msgPrefix + UniErrors.get(1300201)!);
DCloud-yyl's avatar
DCloud-yyl 已提交
344 345 346 347 348
			}
			// 检查目标文件
			let newFilePath = UTSAndroid.convert2AbsFullPath(destPath)
			let isSandyBox = isSandyBoxPath(newFilePath, false)
			if (!isSandyBox) {
DCloud-yyl's avatar
DCloud-yyl 已提交
349
				throw new UniError(UniErrorSubject, 1300013, msgPrefix + UniErrors.get(1300013)!);
DCloud-yyl's avatar
DCloud-yyl 已提交
350 351 352 353 354 355
			}
			let newFile = new File(newFilePath)
			if (newFile.getParentFile() != null && !newFile.getParentFile()!.exists()) {
				/**
				 * 父文件不存在
				 */
DCloud-yyl's avatar
DCloud-yyl 已提交
356
				throw new UniError(UniErrorSubject, 1300002, msgPrefix + UniErrors.get(1300002)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
357 358 359 360
			}
			newFile.writeBytes(byteArray)
			if (!newFile.exists()) {
				// 调用系统api 失败
DCloud-yyl's avatar
DCloud-yyl 已提交
361
				throw new UniError(UniErrorSubject, 1300201, msgPrefix + UniErrors.get(1300201)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
362 363 364 365 366
			}

		} else {
			let isSandyBox = isSandyBoxPath(filePath, true)
			if (!isSandyBox) {
DCloud-yyl's avatar
DCloud-yyl 已提交
367
				throw new UniError(UniErrorSubject, 1300013, msgPrefix + UniErrors.get(1300013)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
368 369 370 371 372 373
			}
			let targetFile = new File(filePath)
			if (!targetFile.exists()) {
				/**
				 * 文件不存在
				 */
DCloud-yyl's avatar
DCloud-yyl 已提交
374
				throw new UniError(UniErrorSubject, 1300002, msgPrefix + UniErrors.get(1300002)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
375 376 377 378 379
			}
			// 检查目标文件
			let newFilePath = UTSAndroid.convert2AbsFullPath(destPath)
			isSandyBox = isSandyBoxPath(newFilePath, false)
			if (!isSandyBox) {
DCloud-yyl's avatar
DCloud-yyl 已提交
380
				throw new UniError(UniErrorSubject, 1300013, msgPrefix + UniErrors.get(1300013)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
381 382 383 384 385 386
			}
			let newFile = new File(newFilePath)
			if (newFile.getParentFile() != null && !newFile.getParentFile()!.exists()) {
				/**
				 * 父文件不存在
				 */
DCloud-yyl's avatar
DCloud-yyl 已提交
387
				throw new UniError(UniErrorSubject, 1300002, msgPrefix + UniErrors.get(1300002)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
388 389 390 391 392
			}

			let copyRetFile = targetFile.copyTo(newFile, true)
			if (!copyRetFile.exists()) {
				// 调用系统api 失败
DCloud-yyl's avatar
DCloud-yyl 已提交
393
				throw new UniError(UniErrorSubject, 1300201, msgPrefix + UniErrors.get(1300201)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
			}
		}
	}


	public copyFile(options : CopyFileOptions) {

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

			// 检查来源文件
			let filePath = UTSAndroid.convert2AbsFullPath(options.srcPath)

			if (filePath.startsWith("/android_asset/")) {
				// 用户访问的是asset 路径,此时不是释放模式

				let exceptionInfo : Exception | null = null
				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);
				} catch (e : Exception) {
					exceptionInfo = e
				}

				if (exceptionInfo != null) {
					currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
424
						let err = new UniError(UniErrorSubject, 1300201, UniErrors.get(1300201)! + ":" + exceptionInfo.message);
DCloud-yyl's avatar
DCloud-yyl 已提交
425 426 427 428 429 430 431 432 433 434 435 436
						options.fail?.(err)
						options.complete?.(err)
					}, null)
					return
				}


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

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

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

				let copyRetFile = targetFile.copyTo(newFile, true)
				if (!copyRetFile.exists()) {
					// 调用系统api 失败
					currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
524
						let err = new UniError(UniErrorSubject, 1300201, UniErrors.get(1300201)!);
DCloud-yyl's avatar
DCloud-yyl 已提交
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
						options.fail?.(err)
						options.complete?.(err)
					})
					return
				}

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

		}, null)

	}


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

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

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


		let renameRet = targetFile.renameTo(newFile)
		if (!renameRet) {
			// 调用系统api 失败
DCloud-yyl's avatar
DCloud-yyl 已提交
578
			throw new UniError(UniErrorSubject, 1300201, msgPrefix + UniErrors.get(1300201)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
579 580 581 582 583 584 585 586 587 588 589 590
		}
	}

	public rename(options : RenameOptions) {

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

			let filePath = UTSAndroid.convert2AbsFullPath(options.oldPath)
			let isSandyBox = isSandyBoxPath(filePath, false)
			if (!isSandyBox) {
				currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
591
					let err = new UniError(UniErrorSubject, 1300013, UniErrors.get(1300013)! + ":" + options.oldPath);
DCloud-yyl's avatar
DCloud-yyl 已提交
592 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()) {
				/**
				 * 文件不存在
				 */
				currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
604
					let err = new UniError(UniErrorSubject, 1300002, UniErrors.get(1300002)! + ":" + options.oldPath);
DCloud-yyl's avatar
DCloud-yyl 已提交
605 606 607 608 609 610 611 612 613
					options.fail?.(err)
					options.complete?.(err)
				})
				return
			}
			let newFilePath = UTSAndroid.convert2AbsFullPath(options.newPath)
			isSandyBox = isSandyBoxPath(newFilePath, false)
			if (!isSandyBox) {
				currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
614
					let err = new UniError(UniErrorSubject, 1300013, UniErrors.get(1300013)! + ":" + options.newPath);
DCloud-yyl's avatar
DCloud-yyl 已提交
615 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()) {
				/**
				 * 父文件不存在
				 */
				currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
627
					let err = new UniError(UniErrorSubject, 1300002, UniErrors.get(1300002)! + ":" + options.newPath);
DCloud-yyl's avatar
DCloud-yyl 已提交
628 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 失败
				currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
639
					let err = new UniError(UniErrorSubject, 1300201, UniErrors.get(1300201)!);
DCloud-yyl's avatar
DCloud-yyl 已提交
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
					options.fail?.(err)
					options.complete?.(err)
				})
				return
			}

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

	}

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

		if (!targetFile.exists()) {
			/**
			 * 文件不存在,或者不是文件夹,异常
			 */
DCloud-yyl's avatar
DCloud-yyl 已提交
670
			throw new UniError(UniErrorSubject, 1300002, msgPrefix + UniErrors.get(1300002)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
671 672 673 674 675 676 677 678 679 680 681 682 683 684
		}
	}

	public access(options : AccessOptions) {

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

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

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

			if (!targetFile.exists()) {
				/**
				 * 文件不存在,或者不是文件夹,异常
				 */
				currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
697
					let err = new UniError(UniErrorSubject, 1300002, UniErrors.get(1300002)! + ":" + options.path);
DCloud-yyl's avatar
DCloud-yyl 已提交
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
					options.fail?.(err)
					options.complete?.(err)
				})
				return
			}

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



	}

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

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

	public readdir(options : ReadDirOptions) {

		let currentDispatcher = UTSAndroid.getDispatcher("main")
		UTSAndroid.getDispatcher('io').async(function (_) {
			let filePath = UTSAndroid.convert2AbsFullPath(options.dirPath)
			let isSandyBox = isSandyBoxPath(filePath, true)
			if (!isSandyBox) {
				currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
744
					let err = new UniError(UniErrorSubject, 1300013, UniErrors.get(1300013)! + ":" + options.dirPath);
DCloud-yyl's avatar
DCloud-yyl 已提交
745 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()) {
				/**
				 * 文件不存在,或者不是文件夹,异常
				 */
				currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
757
					let err = new UniError(UniErrorSubject, 1300002, UniErrors.get(1300002)! + ":" + options.dirPath);
DCloud-yyl's avatar
DCloud-yyl 已提交
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
					options.fail?.(err)
					options.complete?.(err)
				})
				return
			}

			let success : ReadDirSuccessResult = {
				files: UTSArray.fromNative(targetFile.list()!)
			}
			currentDispatcher.async(function (_) {
				options.success?.(success)
				options.complete?.(success)
			})
		}, null)
	}

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

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


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

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

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

	public rmdir(options : RmDirOptions) {

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

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

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

			if (!targetFile.exists() || !targetFile.isDirectory()) {
				/**
				 * 文件不存在 或者 文件是不是文件夹
				 */
				currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
870
					let err = new UniError(UniErrorSubject, 1300002, UniErrors.get(1300002)! + ":" + options.dirPath);
DCloud-yyl's avatar
DCloud-yyl 已提交
871 872 873 874 875 876 877 878 879 880 881 882 883
					options.fail?.(err)
					options.complete?.(err)
				})
				return
			}


			if (!options.recursive) {
				// 没有设置递归,则需要检查是否保存子目录
				let childList = targetFile.list()
				if (childList != null && childList.size > 0) {
					// 存在子目录
					currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
884
						let err = new UniError(UniErrorSubject, 1300066, UniErrors.get(1300066)! + ":" + options.dirPath);
DCloud-yyl's avatar
DCloud-yyl 已提交
885 886 887 888 889 890 891 892 893 894 895
						options.fail?.(err)
						options.complete?.(err)
					})

					return
				}
			} else {
				let delRet = targetFile.deleteRecursively()
				if (!delRet) {
					// 调用系统api 失败
					currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
896
						let err = new UniError(UniErrorSubject, 1300201, UniErrors.get(1300201)! + ":" + options.dirPath);
DCloud-yyl's avatar
DCloud-yyl 已提交
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924
						options.fail?.(err)
						options.complete?.(err)
					})
					return
				}
			}

			let success : FileManagerSuccessResult = {
				errMsg: "rmdir:ok"
			}
			// 删除成功
			currentDispatcher.async(function (_) {
				options.success?.(success)
				options.complete?.(success)
			})
		}, null)



	}

	public mkdir(options : MkDirOptions) {

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

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

			let filePath = UTSAndroid.convert2AbsFullPath(options.dirPath)
			let isSandyBox = isSandyBoxPath(filePath, false)
			if (!isSandyBox) {
				currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
936
					let err = new UniError(UniErrorSubject, 1300013, UniErrors.get(1300013)! + ":" + options.dirPath);
DCloud-yyl's avatar
DCloud-yyl 已提交
937 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()) {
				/**
				 * 文件已经存在,则无法继续创建
				 */
				currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
949
					let err = new UniError(UniErrorSubject, 1301005, UniErrors.get(1301005)! + ":" + options.dirPath);
DCloud-yyl's avatar
DCloud-yyl 已提交
950 951 952 953 954 955 956 957 958 959
					options.fail?.(err)
					options.complete?.(err)
				})
				return
			}

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

						return
					}
				}
			} else {
				// 设置了递归
				let mkRet = targetFile.mkdirs()
				if (!mkRet) {
					// 调用系统api 失败
					currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
985
						let err = new UniError(UniErrorSubject, 1300201, UniErrors.get(1300201)! + ":" + options.dirPath);
DCloud-yyl's avatar
DCloud-yyl 已提交
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
						options.fail?.(err)
						options.complete?.(err)
					})
					return
				}
			}


			currentDispatcher.async(function (_) {
				let success : FileManagerSuccessResult = {
					errMsg: "mkdir:ok"
				}
				// 删除成功
				options.success?.(success)
				options.complete?.(success)
			})
		}, null)



	}

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

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

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

	public unlink(options : UnLinkOptions) {

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


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

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

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

			// 文件存在,则进行删除操作
			let delRet = targetFile.delete()
			if (!delRet) {
				// 调用系统api 删除失败
				currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1065
					let err = new UniError(UniErrorSubject, 1300201, UniErrors.get(1300201)! + ":" + options.filePath);
DCloud-yyl's avatar
DCloud-yyl 已提交
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
					options.fail?.(err)
					options.complete?.(err)
				})
				return
			}

			currentDispatcher.async(function (_) {
				let success : FileManagerSuccessResult = {
					errMsg: "unlink:ok"
				}
				// 删除成功
				options.success?.(success)
				options.complete?.(success)
			})
		}, null)



	}

	public readFileSync(filePath : string, encoding : string | null) : string {
		let msgPrefix = "readFileSync:fail "
		// 判断type 是否合法
		let tempEncoding = encoding
		if (tempEncoding == null) {
			tempEncoding = "utf-8"
		}
		if (tempEncoding.toLowerCase() != 'base64' && tempEncoding.toLowerCase() != 'utf-8' && tempEncoding.toLowerCase() != 'ascii') {
DCloud-yyl's avatar
DCloud-yyl 已提交
1094
			throw new UniError(UniErrorSubject, 1200002, msgPrefix + UniErrors.get(1200002)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
		}
		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) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1106
				throw new UniError(UniErrorSubject, 1300201, msgPrefix + UniErrors.get(1300201)! + ":" + filePath)
DCloud-yyl's avatar
DCloud-yyl 已提交
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
			}

			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
			}

		} else {

			let isSandyBox = isSandyBoxPath(tempFilePath, true)
			if (!isSandyBox) {
				return "1300013"
			}

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

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

			/**
			 * 文件超过100M,会超过应用内存
			 */
			if (targetFile.length() > 100 * 1024 * 1024) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1143
				throw new UniError(UniErrorSubject, 1300202, msgPrefix + UniErrors.get(1300202)! + ":" + filePath);
DCloud-yyl's avatar
DCloud-yyl 已提交
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
			}

			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
			}
		}
	}

	public readFile(options : ReadFileOptions) {

		// 判断type 是否合法
		if (options.encoding.toLowerCase() != 'base64' && options.encoding.toLowerCase() != 'utf-8' && options.encoding.toLowerCase() != 'ascii') {
DCloud-yyl's avatar
DCloud-yyl 已提交
1169
			let err = new UniError(UniErrorSubject, 1200002, UniErrors.get(1200002)!);
DCloud-yyl's avatar
DCloud-yyl 已提交
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
			options.fail?.(err)
			options.complete?.(err)
			return
		}



		let currentDispatcher = UTSAndroid.getDispatcher("main")
		/**
		 * 执行真正的加载行为,为了避免阻塞分发到 io任务序列
		 */
		UTSAndroid.getDispatcher('io').async(function (_) {

			let filePath = UTSAndroid.convert2AbsFullPath(options.filePath)

			if (filePath.startsWith("/android_asset/")) {
				// 用户访问的是asset 路径,此时不是释放模式

				let exceptionInfo : Exception | null = null
				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);
				} catch (e : Exception) {
					exceptionInfo = e
				}

				if (exceptionInfo != null) {
					currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1202
						let err = new UniError(UniErrorSubject, 1300201, UniErrors.get(1300201)! + ":" + exceptionInfo.message);
DCloud-yyl's avatar
DCloud-yyl 已提交
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
						options.fail?.(err)
						options.complete?.(err)
					})
					return
				}

				let ret : ReadFileSuccessResult = {
					data: ""
				}

				if (options.encoding.toLowerCase() == 'base64') {
					// base64
					let base64Content = Base64.encodeToString(byteArray, Base64.NO_WRAP)
					ret.data = base64Content

				} else if (options.encoding.toLowerCase() == "ascii") {
					// ascii

					let text = new String(byteArray, Charsets.US_ASCII)
					ret.data = text
				}
				else {
					// utf-8 兜底
					let text = new String(byteArray, Charsets.UTF_8)
					ret.data = text
				}

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

			} else {

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

				let targetFile = new File(filePath)
				if (!targetFile.exists()) {
					currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1250
						let err = new UniError(UniErrorSubject, 1300002, UniErrors.get(1300002)! + ":" + options.filePath);
DCloud-yyl's avatar
DCloud-yyl 已提交
1251 1252 1253 1254 1255 1256 1257 1258
						options.fail?.(err)
						options.complete?.(err)
					})
					return
				}

				if (targetFile.isDirectory()) {
					currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1259
						let err = new UniError(UniErrorSubject, 1301003, UniErrors.get(1301003)!);
DCloud-yyl's avatar
DCloud-yyl 已提交
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
						options.fail?.(err)
						options.complete?.(err)
					})
					return
				}

				/**
				 * 文件超过100M,会超过应用内存
				 */
				if (targetFile.length() > 100 * 1024 * 1024) {
					currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1271
						let err = new UniError(UniErrorSubject, 1300202, UniErrors.get(1300202)!);
DCloud-yyl's avatar
DCloud-yyl 已提交
1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313
						options.fail?.(err)
						options.complete?.(err)
					})
					return
				}

				let ret : ReadFileSuccessResult = {
					data: ""
				}

				if (options.encoding.toLowerCase() == 'base64') {
					// base64
					let byteArray = targetFile.readBytes()
					let base64Content = Base64.encodeToString(byteArray, Base64.NO_WRAP)
					ret.data = base64Content

				} else if (options.encoding.toLowerCase() == "ascii") {
					// ascii
					let text = targetFile.readText(Charsets.US_ASCII)
					ret.data = text
				}
				else {
					// utf-8 兜底
					let text = targetFile.readText(Charsets.UTF_8)
					ret.data = text
				}

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


		}, null)

	}

	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') {
DCloud-yyl's avatar
DCloud-yyl 已提交
1314
			throw new UniError(UniErrorSubject, 1200002, msgPrefix + UniErrors.get(1200002)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1315 1316 1317 1318 1319 1320 1321
		}

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

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

		let nextFile = new File(tempFilePath)

		if (nextFile.exists() && nextFile.isDirectory()) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1328
			throw new UniError(UniErrorSubject, 1301003, msgPrefix + UniErrors.get(1301003)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
		}

		/**
		 * 如果上一级目录不存在,创建之
		 */
		if (!nextFile.parentFile!.exists()) {
			nextFile.parentFile!.mkdirs()
		}

		if (!nextFile.exists()) {
			nextFile.createNewFile()
		}
		// 写入文本,根据不同的编码内容写入不同的数据
		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)
		}

	}

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

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

		let isSandyBox = isSandyBoxPath(filePath, false)
		if (!isSandyBox) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1377
			let err = new UniError(UniErrorSubject, 1300013, UniErrors.get(1300013)! + ":" + options.filePath);
DCloud-yyl's avatar
DCloud-yyl 已提交
1378 1379 1380 1381 1382 1383 1384 1385 1386
			options.fail?.(err)
			options.complete?.(err)
			return
		}

		let nextFile = new File(filePath)

		if (nextFile.exists() && nextFile.isDirectory()) {
			// 出错了,目标文件已存在,并且是个目录
DCloud-yyl's avatar
DCloud-yyl 已提交
1387
			let err = new UniError(UniErrorSubject, 1301003, UniErrors.get(1301003)!);
DCloud-yyl's avatar
DCloud-yyl 已提交
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441
			options.fail?.(err)
			options.complete?.(err)
			return
		}


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

			/**
			 * 如果上一级目录不存在,创建之
			 */
			if (!nextFile.parentFile!.exists()) {
				nextFile.parentFile!.mkdirs()
			}

			if (!nextFile.exists()) {
				nextFile.createNewFile()
			}
			// 写入文本,根据不同的编码内容写入不同的数据
			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.writeBytes(byteArray)
			} 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') {
DCloud-yyl's avatar
DCloud-yyl 已提交
1442
			throw new UniError(UniErrorSubject, 1200002, msgPrefix + UniErrors.get(1200002)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1443 1444 1445 1446 1447
		}
		// 判断type 是否合法
		let tempFilePath = UTSAndroid.convert2AbsFullPath(filePath)
		let isSandyBox = isSandyBoxPath(tempFilePath, false)
		if (!isSandyBox) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1448
			throw new UniError(UniErrorSubject, 1300013, msgPrefix + UniErrors.get(1300013)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1449 1450 1451
		}
		let nextFile = new File(tempFilePath)
		if (!nextFile.exists()) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1452
			throw new UniError(UniErrorSubject, 1300002, msgPrefix + UniErrors.get(1300002)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1453 1454
		} else if (nextFile.isDirectory()) {
			// 出错了,目标文件已存在,并且是个目录
DCloud-yyl's avatar
DCloud-yyl 已提交
1455
			throw new UniError(UniErrorSubject, 1301003, msgPrefix + UniErrors.get(1301003)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479
		}
		// 写入文本,根据不同的编码内容写入不同的数据
		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') {
DCloud-yyl's avatar
DCloud-yyl 已提交
1480
			let err = new UniError(UniErrorSubject, 1200002, UniErrors.get(1200002)!);
DCloud-yyl's avatar
DCloud-yyl 已提交
1481 1482 1483 1484 1485 1486 1487 1488 1489 1490
			options.fail?.(err)
			options.complete?.(err)
			return
		}

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

		let isSandyBox = isSandyBoxPath(filePath, false)
		if (!isSandyBox) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1491
			let err = new UniError(UniErrorSubject, 1300013, UniErrors.get(1300013)! + ":" + options.filePath);
DCloud-yyl's avatar
DCloud-yyl 已提交
1492 1493 1494 1495 1496 1497 1498 1499 1500
			options.fail?.(err)
			options.complete?.(err)
			return
		}

		let nextFile = new File(filePath)


		if (!nextFile.exists()) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1501
			let err = new UniError(UniErrorSubject, 1300002, UniErrors.get(1300002)!);
DCloud-yyl's avatar
DCloud-yyl 已提交
1502 1503 1504 1505 1506
			options.fail?.(err)
			options.complete?.(err)
			return
		} else if (nextFile.isDirectory()) {
			// 出错了,目标文件已存在,并且是个目录
DCloud-yyl's avatar
DCloud-yyl 已提交
1507
			let err = new UniError(UniErrorSubject, 1301003, UniErrors.get(1301003)!);
DCloud-yyl's avatar
DCloud-yyl 已提交
1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 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
			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') {
				// base64
				nextFile.appendBytes(Base64.decode(options.data, Base64.NO_WRAP))
			} else {
				// utf-8 兜底
				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+') {
DCloud-yyl's avatar
DCloud-yyl 已提交
1559
			let err = new UniError(UniErrorSubject, 1302003, UniErrors.get(1302003)!);
DCloud-yyl's avatar
DCloud-yyl 已提交
1560 1561 1562 1563 1564 1565 1566 1567 1568
			options.fail?.(err)
			options.complete?.(err)
			return
		}

		let filePath = UTSAndroid.convert2AbsFullPath(options.filePath)

		let isSandyBox = isSandyBoxPath(filePath, false)
		if (!isSandyBox) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1569
			let err = new UniError(UniErrorSubject, 1300013, UniErrors.get(1300013)! + ":" + options.filePath);
DCloud-yyl's avatar
DCloud-yyl 已提交
1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591
			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+') {
DCloud-yyl's avatar
DCloud-yyl 已提交
1592
			throw new UniError(UniErrorSubject, 1302003, msgPrefix + UniErrors.get(1302003)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1593 1594 1595 1596 1597 1598
		}

		let filePath = UTSAndroid.convert2AbsFullPath(options.filePath)

		let isSandyBox = isSandyBoxPath(filePath, false)
		if (!isSandyBox) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1599
			throw new UniError(UniErrorSubject, 1300013, msgPrefix + UniErrors.get(1300013)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612
		}
		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 (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1613
					let err = new UniError(UniErrorSubject, 1300013, UniErrors.get(1300013)! + ":" + options.tempFilePath);
DCloud-yyl's avatar
DCloud-yyl 已提交
1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624
					options.fail?.(err)
					options.complete?.(err)
				}, null)
				return
			}
			let targetFile = new File(tempFilePath)
			if (!targetFile.exists()) {
				/**
				 * 文件不存在
				 */
				currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1625
					let err = new UniError(UniErrorSubject, 1300002, UniErrors.get(1300002)! + ":" + options.tempFilePath);
DCloud-yyl's avatar
DCloud-yyl 已提交
1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639
					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 (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1640
					let err = new UniError(UniErrorSubject, 1300013, UniErrors.get(1300013)! + ":" + options.filePath);
DCloud-yyl's avatar
DCloud-yyl 已提交
1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652
					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 (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1653
					let err = new UniError(UniErrorSubject, 1300002, UniErrors.get(1300002)! + ":" + options.filePath);
DCloud-yyl's avatar
DCloud-yyl 已提交
1654 1655 1656 1657 1658 1659 1660 1661 1662 1663
					options.fail?.(err)
					options.complete?.(err)
				}, null)
				return
			}

			let copyRetFile = targetFile.copyTo(newFile, true)
			if (!copyRetFile.exists()) {
				// 调用系统api 失败
				currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1664
					let err = new UniError(UniErrorSubject, 1300201, UniErrors.get(1300201)!);
DCloud-yyl's avatar
DCloud-yyl 已提交
1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688
					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) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1689
			throw new UniError(UniErrorSubject, 1300013, msgPrefix + UniErrors.get(1300013)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1690 1691 1692 1693 1694 1695
		}
		let targetFile = new File(tfPath)
		if (!targetFile.exists()) {
			/**
			 * 文件不存在
			 */
DCloud-yyl's avatar
DCloud-yyl 已提交
1696
			throw new UniError(UniErrorSubject, 1300002, msgPrefix + UniErrors.get(1300002)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1697 1698 1699 1700 1701 1702 1703 1704 1705
		}
		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) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1706
			throw new UniError(UniErrorSubject, 1300013, msgPrefix + UniErrors.get(1300013)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1707 1708 1709 1710 1711 1712 1713
		}
		let newFile = new File(newFilePath)
		console.log(newFile.getPath())
		if (newFile.getParentFile() != null && !newFile.getParentFile()!.exists()) {
			/**
			 * 父文件不存在
			 */
DCloud-yyl's avatar
DCloud-yyl 已提交
1714
			throw new UniError(UniErrorSubject, 1300002, msgPrefix + UniErrors.get(1300002)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1715 1716 1717 1718 1719
		}

		let copyRetFile = targetFile.copyTo(newFile, true)
		if (!copyRetFile.exists()) {
			// 调用系统api 失败
DCloud-yyl's avatar
DCloud-yyl 已提交
1720
			throw new UniError(UniErrorSubject, 1300201, msgPrefix + UniErrors.get(1300201)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1721 1722 1723 1724 1725 1726 1727 1728
		}
		targetFile.delete()
		return newFilePath
	}
	public unzip(options : UnzipFileOptions) {
		let msgPrefix = "unzip:fail "
		// 检查来源文件
		let zipFilePath = UTSAndroid.convert2AbsFullPath(options.zipFilePath)
DCloud-yyl's avatar
DCloud-yyl 已提交
1729
		let isSandyBox = isSandyBoxPath(zipFilePath, true)
DCloud-yyl's avatar
DCloud-yyl 已提交
1730 1731
		let currentDispatcher = UTSAndroid.getDispatcher("main")
		UTSAndroid.getDispatcher('io').async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752
			if (!isSandyBox) {
				currentDispatcher.async(function (_) {
					let err = new UniError(UniErrorSubject, 1300013, UniErrors.get(1300013)! + ":" + options.zipFilePath);
					options.fail?.(err)
					options.complete?.(err)
				}, null)
				return
			}
			let zipFile = new File(zipFilePath)
			if (!zipFile.exists()) {
				/**
				 * 文件不存在
				 */
				currentDispatcher.async(function (_) {
					let err = new UniError(UniErrorSubject, 1300002, UniErrors.get(1300002)! + ":" + options.zipFilePath);
					options.fail?.(err)
					options.complete?.(err)
				}, null)
				return
			}

DCloud-yyl's avatar
DCloud-yyl 已提交
1753
			let targetPath = UTSAndroid.convert2AbsFullPath(options.targetPath)
DCloud-yyl's avatar
DCloud-yyl 已提交
1754 1755 1756 1757 1758 1759 1760 1761 1762 1763
			isSandyBox = isSandyBoxPath(targetPath, true)
			let targetFile = new File(targetPath)
			if (!isSandyBox) {
				currentDispatcher.async(function (_) {
					let err = new UniError(UniErrorSubject, 1300013, UniErrors.get(1300013)! + ":" + options.zipFilePath);
					options.fail?.(err)
					options.complete?.(err)
				}, null)
				return

DCloud-yyl's avatar
DCloud-yyl 已提交
1764
			}
DCloud-yyl's avatar
DCloud-yyl 已提交
1765 1766 1767 1768 1769 1770 1771 1772 1773 1774
			if (!targetFile.exists() || !targetFile.isDirectory()) {
				currentDispatcher.async(function (_) {
					let err = new UniError(UniErrorSubject, 1300002, UniErrors.get(1300002)! + ":" + options.zipFilePath);
					options.fail?.(err)
					options.complete?.(err)
				}, null)
				return
			}


DCloud-yyl's avatar
DCloud-yyl 已提交
1775
			try {
DCloud-yyl's avatar
DCloud-yyl 已提交
1776
				let is = new FileInputStream(zipFile);
DCloud-yyl's avatar
DCloud-yyl 已提交
1777
				let zis = new ZipInputStream(new BufferedInputStream(is));
DCloud-yyl's avatar
DCloud-yyl 已提交
1778
				console.log(zipFile.getPath())
DCloud-yyl's avatar
DCloud-yyl 已提交
1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 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
				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 (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1822
					let err = new UniError(UniErrorSubject, 1300201, UniErrors.get(1300201)! + ":" + options.zipFilePath);
DCloud-yyl's avatar
DCloud-yyl 已提交
1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843
					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 (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1844
					let err = new UniError(UniErrorSubject, 1300002, UniErrors.get(1300002)! + ":" + filePath);
DCloud-yyl's avatar
DCloud-yyl 已提交
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
					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 (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1885
					let err = new UniError(UniErrorSubject, 1300002, UniErrors.get(1300002)! + ":" + filePath);
DCloud-yyl's avatar
DCloud-yyl 已提交
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
					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()) {
			/**
			 * 文件不存在,或者是文件夹,异常
			 */
DCloud-yyl's avatar
DCloud-yyl 已提交
1921
			throw new UniError(UniErrorSubject, 1300002, msgPrefix + UniErrors.get(1300002)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935
		}

		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 filePath = UTSAndroid.convert2AbsFullPath(options.filePath)

DCloud-yyl's avatar
DCloud-yyl 已提交
1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957
			let targetFile = new File(filePath)
			if (!targetFile.exists() || targetFile.isDirectory()) {
				/**
				 * 文件不存在,或者是文件夹,异常
				 */
				currentDispatcher.async(function (_) {
					let err = new UniError(UniErrorSubject, 1300002, UniErrors.get(1300002)! + ":" + filePath);
					options.fail?.(err)
					options.complete?.(err)
				}, null)
				return
			}
			if (options.compressionAlgorithm != 'br') {
				currentDispatcher.async(function (_) {
					let err = new UniError(UniErrorSubject, 1301111, UniErrors.get(1301111)! + ":" + filePath);
					options.fail?.(err)
					options.complete?.(err)
				}, null)
				return
			}
			let success : ReadCompressedFileResult = {
				data: ""
DCloud-yyl's avatar
DCloud-yyl 已提交
1958 1959
			}
			try {
DCloud-yyl's avatar
DCloud-yyl 已提交
1960
				let brInput = new BrotliInputStream(new FileInputStream(targetFile))
DCloud-yyl's avatar
DCloud-yyl 已提交
1961 1962 1963 1964
				success.data = new String(brInput.readBytes())
				brInput.close()
			} catch (e : Exception) {
				currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1965
					let err = new UniError(UniErrorSubject, 1300201, UniErrors.get(1300201)! + ":" + filePath);
DCloud-yyl's avatar
DCloud-yyl 已提交
1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980
					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)
DCloud-yyl's avatar
DCloud-yyl 已提交
1981 1982 1983 1984 1985 1986 1987 1988 1989
		let targetFile = new File(tempFilePath)
		if (!targetFile.exists() || targetFile.isDirectory()) {
			/**
			 * 文件不存在,或者是文件夹,异常
			 */
			throw new UniError(UniErrorSubject, 1300002, msgPrefix + UniErrors.get(1300002)!)
		}
		if (compressionAlgorithm != 'br') {
			throw new UniError(UniErrorSubject, 1301111, msgPrefix + UniErrors.get(1301111)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1990 1991 1992
		}
		let data : string
		try {
DCloud-yyl's avatar
DCloud-yyl 已提交
1993
			let brInput = new BrotliInputStream(new FileInputStream(targetFile))
DCloud-yyl's avatar
DCloud-yyl 已提交
1994 1995 1996
			data = new String(brInput.readBytes())
			brInput.close()
		} catch (e : Exception) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1997
			throw new UniError(UniErrorSubject, 1300201, msgPrefix + UniErrors.get(1300201)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012
		}
		return data
	}

	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 (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
2013
					let err = new UniError(UniErrorSubject, 1300002, UniErrors.get(1300002)! + ":" + filePath);
DCloud-yyl's avatar
DCloud-yyl 已提交
2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032
					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') {
DCloud-yyl's avatar
DCloud-yyl 已提交
2033
			let err = new UniError(UniErrorSubject, 1200002, UniErrors.get(1200002)!);
DCloud-yyl's avatar
DCloud-yyl 已提交
2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044
			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) {
DCloud-yyl's avatar
DCloud-yyl 已提交
2045
				let err = new UniError(UniErrorSubject, 1300009, UniErrors.get(1300009)!);
DCloud-yyl's avatar
DCloud-yyl 已提交
2046 2047 2048 2049 2050 2051 2052 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
				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 (_) {
				options.success?.(ret)
				options.complete?.(ret)
			})
		}, null)
	}

	public writeSync(options : WriteSyncOptions) : WriteResult {
		let msgPrefix = "writeSync:fail "
		if (options.encoding.toLowerCase() != 'base64' && options.encoding.toLowerCase() != 'utf-8' && options.encoding.toLowerCase() != 'ascii') {
DCloud-yyl's avatar
DCloud-yyl 已提交
2092
			throw new UniError(UniErrorSubject, 1200002, msgPrefix + UniErrors.get(1200002)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
2093 2094 2095
		}
		let fd = ParcelFileDescriptor.fromFd(options.fd.toInt())
		if (fd == null) {
DCloud-yyl's avatar
DCloud-yyl 已提交
2096
			throw new UniError(UniErrorSubject, 1300009, msgPrefix + UniErrors.get(1300009)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
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
		}
		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) {
DCloud-yyl's avatar
DCloud-yyl 已提交
2128
			throw new UniError(UniErrorSubject, 1300201, msgPrefix + UniErrors.get(1300201)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149
		}
		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) {
DCloud-yyl's avatar
DCloud-yyl 已提交
2150
				let err = new UniError(UniErrorSubject, 1300009, UniErrors.get(1300009)!);
DCloud-yyl's avatar
DCloud-yyl 已提交
2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164
				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) {
DCloud-yyl's avatar
DCloud-yyl 已提交
2165
			throw new UniError(UniErrorSubject, 1300009, msgPrefix + UniErrors.get(1300009)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
2166 2167 2168 2169 2170 2171 2172 2173
		}
	}
	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 (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
2174
						let err = new UniError(UniErrorSubject, 1300009, UniErrors.get(1300009)! + ":" + options.fd);
DCloud-yyl's avatar
DCloud-yyl 已提交
2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190
						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)) {
DCloud-yyl's avatar
DCloud-yyl 已提交
2191
			throw new UniError(UniErrorSubject, 1300009, msgPrefix + UniErrors.get(1300009)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
2192 2193 2194 2195 2196 2197 2198 2199
		}
		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 (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
2200
					let err = new UniError(UniErrorSubject, 1300009, UniErrors.get(1300009)! + ":" + options.fd);
DCloud-yyl's avatar
DCloud-yyl 已提交
2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224
					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)) {
DCloud-yyl's avatar
DCloud-yyl 已提交
2225
			throw new UniError(UniErrorSubject, 1300009, msgPrefix + UniErrors.get(1300009)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237
		}
		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)
DCloud-yyl's avatar
DCloud-yyl 已提交
2238

DCloud-yyl's avatar
DCloud-yyl 已提交
2239 2240 2241 2242 2243 2244
		console.log("readZipEntry", targetPath)
		let file : File = new File(targetPath)
		if (!file.exists()) {
			file.mkdirs()
		}
		let target = this
DCloud-yyl's avatar
DCloud-yyl 已提交
2245
		let zipOptions : UnzipFileOptions = {
DCloud-yyl's avatar
DCloud-yyl 已提交
2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256
			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)
DCloud-yyl's avatar
DCloud-yyl 已提交
2257
						console.log("readZipEntry", file.getPath())
DCloud-yyl's avatar
DCloud-yyl 已提交
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
						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()
				})
			},
DCloud-yyl's avatar
DCloud-yyl 已提交
2316 2317
			fail: (res : UniError) => {
				let err = new UniError(UniErrorSubject, res.errCode, res.errMsg);
DCloud-yyl's avatar
DCloud-yyl 已提交
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 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418
				options.fail?.(err)
				options.complete?.(err)
			}
		}
		this.unzip(zipOptions)
	}
}
export const getFileSystemManager : GetFileSystemManager = function () : FileSystemManager {
	return AndroidFileSystemManager.getManager()
};

/**
 * 判断传入的路径是否是沙盒路径,如果不是则不能继续操作
 */
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
		}
	}

	/**
	 * 第一条规则,判断是否操作的是应用资源目录。开发者具备该目录下读取的权限操作
	 */
	let appResRoot = UTSAndroid.convert2AbsFullPath(uni.env.APP_RESOURCE_PATH)
	if (inputPath.startsWith(appResRoot)) {
		// 路径是应用资源目录
		if (onlyRead) {
			// 并且是读取操作,返回
			return true
		} else {
			// 不是读取
			return false
		}
	}
	/**
	 * 第二条规则,是否属于应用根目录 SANDBOX_PATH
	 */
	let sandyBoxRoot = UTSAndroid.convert2AbsFullPath(uni.env.SANDBOX_PATH)
	if (inputPath.startsWith(sandyBoxRoot)) {
		// 是否是应用根目录
		return true
	}

	/**
	 * 第三条规则,是否属于应用内置根目录 ANDROID_INTERNAL_SANDBOX_PATH
	 */
	let innerSandyBoxRoot = UTSAndroid.convert2AbsFullPath(uni.env.ANDROID_INTERNAL_SANDBOX_PATH)
	if (inputPath.startsWith(innerSandyBoxRoot)) {
		// 是否是应用根目录
		return true
	}


	return false
}


function wrapStats(sourceFile : File) : Stats {
	let stats = new AndroidStats()
	stats.mIsFile = sourceFile.isFile()
	stats.lastModifiedTime = sourceFile.lastModified()
	stats.mode = getFileMode(sourceFile)
	if (sourceFile.isFile()) {
		stats.size = sourceFile.length()
	}
	return stats
}

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)
}