index.uts 72.9 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 { FileSystemManagerFailImpl, FileSystemManagerUniErrorSubject, FileSystemManagerUniErrors, 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
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';
DCloud-yyl's avatar
DCloud-yyl 已提交
21
import InputStream from 'java.io.InputStream';
DCloud-yyl's avatar
DCloud-yyl 已提交
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 99

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 已提交
100
						let err = new FileSystemManagerFailImpl(1300013);
DCloud-yyl's avatar
DCloud-yyl 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113
						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 已提交
114
						let err = new FileSystemManagerFailImpl(1300002);
DCloud-yyl's avatar
DCloud-yyl 已提交
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 181
						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 已提交
182
			throw new UniError(FileSystemManagerUniErrorSubject, 1300013, msgPrefix + FileSystemManagerUniErrors.get(1300013)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
183 184 185 186 187 188
		}
		let targetFile = new File(filePath)
		if (!targetFile.exists()) {
			/**
			 * 文件不存在
			 */
DCloud-yyl's avatar
DCloud-yyl 已提交
189
			throw new UniError(FileSystemManagerUniErrorSubject, 1300002, msgPrefix + FileSystemManagerUniErrors.get(1300002)!);
DCloud-yyl's avatar
DCloud-yyl 已提交
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 236
		}
		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 已提交
237
					let err = new FileSystemManagerFailImpl(1300013);
DCloud-yyl's avatar
DCloud-yyl 已提交
238 239 240 241 242 243 244 245 246 247 248 249 250
					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 已提交
251
					let err = new FileSystemManagerFailImpl(1300002);
DCloud-yyl's avatar
DCloud-yyl 已提交
252 253 254 255 256 257 258 259 260 261 262
					options.fail?.(err)
					options.complete?.(err)
				}, null)
				return
			}

			if (targetFile.isDirectory()) {
				/**
				 * 文件是个目录
				 */
				currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
263
					let err = new FileSystemManagerFailImpl( 1300021);
DCloud-yyl's avatar
DCloud-yyl 已提交
264 265 266 267 268 269 270 271 272 273 274 275 276 277
					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 已提交
278
					let err = new FileSystemManagerFailImpl(1300022);
DCloud-yyl's avatar
DCloud-yyl 已提交
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 343
					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 已提交
344
				throw new UniError(FileSystemManagerUniErrorSubject, 1300201, msgPrefix + FileSystemManagerUniErrors.get(1300201)!);
DCloud-yyl's avatar
DCloud-yyl 已提交
345 346 347 348 349
			}
			// 检查目标文件
			let newFilePath = UTSAndroid.convert2AbsFullPath(destPath)
			let isSandyBox = isSandyBoxPath(newFilePath, false)
			if (!isSandyBox) {
DCloud-yyl's avatar
DCloud-yyl 已提交
350
				throw new UniError(FileSystemManagerUniErrorSubject, 1300013, msgPrefix + FileSystemManagerUniErrors.get(1300013)!);
DCloud-yyl's avatar
DCloud-yyl 已提交
351 352 353 354 355 356
			}
			let newFile = new File(newFilePath)
			if (newFile.getParentFile() != null && !newFile.getParentFile()!.exists()) {
				/**
				 * 父文件不存在
				 */
DCloud-yyl's avatar
DCloud-yyl 已提交
357
				throw new UniError(FileSystemManagerUniErrorSubject, 1300002, msgPrefix + FileSystemManagerUniErrors.get(1300002)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
358 359 360 361
			}
			newFile.writeBytes(byteArray)
			if (!newFile.exists()) {
				// 调用系统api 失败
DCloud-yyl's avatar
DCloud-yyl 已提交
362
				throw new UniError(FileSystemManagerUniErrorSubject, 1300201, msgPrefix + FileSystemManagerUniErrors.get(1300201)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
363 364 365 366 367
			}

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

			let copyRetFile = targetFile.copyTo(newFile, true)
			if (!copyRetFile.exists()) {
				// 调用系统api 失败
DCloud-yyl's avatar
DCloud-yyl 已提交
394
				throw new UniError(FileSystemManagerUniErrorSubject, 1300201, msgPrefix + FileSystemManagerUniErrors.get(1300201)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
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 424
			}
		}
	}


	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 已提交
425
						let err = new FileSystemManagerFailImpl(1300201);
DCloud-yyl's avatar
DCloud-yyl 已提交
426 427 428 429 430 431 432 433 434 435 436 437
						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 已提交
438
						let err = new FileSystemManagerFailImpl(1300013);
DCloud-yyl's avatar
DCloud-yyl 已提交
439 440 441 442 443 444 445 446 447 448 449
						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 已提交
450
						let err = new FileSystemManagerFailImpl(1300002);
DCloud-yyl's avatar
DCloud-yyl 已提交
451 452 453 454 455 456 457 458 459
						options.fail?.(err)
						options.complete?.(err)
					}, null)
					return
				}
				newFile.writeBytes(byteArray)
				if (!newFile.exists()) {
					// 调用系统api 失败
					currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
460
						let err = new FileSystemManagerFailImpl(1300201);
DCloud-yyl's avatar
DCloud-yyl 已提交
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
						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 已提交
479
						let err = new FileSystemManagerFailImpl(1300013);
DCloud-yyl's avatar
DCloud-yyl 已提交
480 481 482 483 484 485 486 487 488 489 490
						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 已提交
491
						let err = new FileSystemManagerFailImpl(1300002);
DCloud-yyl's avatar
DCloud-yyl 已提交
492 493 494 495 496 497 498 499 500 501
						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 已提交
502
						let err = new FileSystemManagerFailImpl(1300013);
DCloud-yyl's avatar
DCloud-yyl 已提交
503 504 505 506 507 508 509 510 511 512 513
						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 已提交
514
						let err = new FileSystemManagerFailImpl(1300002);
DCloud-yyl's avatar
DCloud-yyl 已提交
515 516 517 518 519 520 521 522 523 524
						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 已提交
525
						let err = new FileSystemManagerFailImpl(1300201);
DCloud-yyl's avatar
DCloud-yyl 已提交
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
						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 已提交
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 562 563
		}
		let newFilePath = UTSAndroid.convert2AbsFullPath(newPath)
		isSandyBox = isSandyBoxPath(newFilePath, false)
		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 567 568 569 570 571
		}
		let newFile = new File(newFilePath)

		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 574 575 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 583 584 585 586 587 588 589 590 591
		}
	}

	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 已提交
592
					let err = new FileSystemManagerFailImpl(1300013);
DCloud-yyl's avatar
DCloud-yyl 已提交
593 594 595 596 597 598 599 600 601 602 603 604
					options.fail?.(err)
					options.complete?.(err)
				})
				return
			}

			let targetFile = new File(filePath)
			if (!targetFile.exists()) {
				/**
				 * 文件不存在
				 */
				currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
605
					let err = new FileSystemManagerFailImpl(1300002);
DCloud-yyl's avatar
DCloud-yyl 已提交
606 607 608 609 610 611 612 613 614
					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 已提交
615
					let err = new FileSystemManagerFailImpl(1300013);
DCloud-yyl's avatar
DCloud-yyl 已提交
616 617 618 619 620 621 622 623 624 625 626 627
					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 已提交
628
					let err = new FileSystemManagerFailImpl(1300002);
DCloud-yyl's avatar
DCloud-yyl 已提交
629 630 631 632 633 634 635 636 637 638 639
					options.fail?.(err)
					options.complete?.(err)
				})
				return
			}


			let renameRet = targetFile.renameTo(newFile)
			if (!renameRet) {
				// 调用系统api 失败
				currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
640
					let err = new FileSystemManagerFailImpl(1300201);
DCloud-yyl's avatar
DCloud-yyl 已提交
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
					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 已提交
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 675 676 677 678 679 680 681 682 683 684 685
		}
	}

	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 已提交
686
					let err = new FileSystemManagerFailImpl(1300013);
DCloud-yyl's avatar
DCloud-yyl 已提交
687 688 689 690 691 692 693 694 695 696 697
					options.fail?.(err)
					options.complete?.(err)
				})
				return
			}

			if (!targetFile.exists()) {
				/**
				 * 文件不存在,或者不是文件夹,异常
				 */
				currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
698
					let err = new FileSystemManagerFailImpl(1300002);
DCloud-yyl's avatar
DCloud-yyl 已提交
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
					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 已提交
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 737 738 739 740 741 742 743 744
		}
		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 已提交
745
					let err = new FileSystemManagerFailImpl(1300013);
DCloud-yyl's avatar
DCloud-yyl 已提交
746 747 748 749 750 751 752 753 754 755 756 757
					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 已提交
758
					let err = new FileSystemManagerFailImpl(1300002);
DCloud-yyl's avatar
DCloud-yyl 已提交
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
					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 已提交
781
			throw new UniError(FileSystemManagerUniErrorSubject, 1300013, msgPrefix + FileSystemManagerUniErrors.get(1300013)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
782 783 784 785 786 787
		}

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


		if (!recursive) {
			// 没有设置递归,则需要检查是否保存子目录
			let childList = targetFile.list()
			if (childList != null && childList.size > 0) {
				// 存在子目录
DCloud-yyl's avatar
DCloud-yyl 已提交
797
				throw new UniError(FileSystemManagerUniErrorSubject, 1300066, msgPrefix + FileSystemManagerUniErrors.get(1300066)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
798 799 800 801 802
			}
		} else {
			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 808 809 810
			}
		}
	}

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

		if (!recursive) {
			//  没有设置递归创建
			if (targetFile.getParentFile() == null || !targetFile.getParentFile()!.exists()) {
DCloud-yyl's avatar
DCloud-yyl 已提交
829
				throw new UniError(FileSystemManagerUniErrorSubject, 1300002, msgPrefix + FileSystemManagerUniErrors.get(1300002)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
830 831 832 833 834
			} else {
				// 父文件夹存在,则继续创建
				let mkRet = targetFile.mkdir()
				if (!mkRet) {
					// 调用系统api 失败
DCloud-yyl's avatar
DCloud-yyl 已提交
835
					throw new UniError(FileSystemManagerUniErrorSubject, 1300201, msgPrefix + FileSystemManagerUniErrors.get(1300201)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
836 837 838 839 840 841 842
				}
			}
		} else {
			// 设置了递归
			let mkRet = targetFile.mkdirs()
			if (!mkRet) {
				// 调用系统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 853 854 855 856 857 858
			}
		}
	}

	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 已提交
859
					let err = new FileSystemManagerFailImpl(1300013);
DCloud-yyl's avatar
DCloud-yyl 已提交
860 861 862 863 864 865 866 867 868 869 870
					options.fail?.(err)
					options.complete?.(err)
				})
				return
			}

			if (!targetFile.exists() || !targetFile.isDirectory()) {
				/**
				 * 文件不存在 或者 文件是不是文件夹
				 */
				currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
871
					let err = new FileSystemManagerFailImpl(1300002);
DCloud-yyl's avatar
DCloud-yyl 已提交
872 873 874 875 876 877 878 879 880 881 882 883 884
					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 已提交
885
						let err = new FileSystemManagerFailImpl(1300066);
DCloud-yyl's avatar
DCloud-yyl 已提交
886 887 888 889 890 891 892 893 894 895 896
						options.fail?.(err)
						options.complete?.(err)
					})

					return
				}
			} else {
				let delRet = targetFile.deleteRecursively()
				if (!delRet) {
					// 调用系统api 失败
					currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
897
						let err = new FileSystemManagerFailImpl(1300201);
DCloud-yyl's avatar
DCloud-yyl 已提交
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 925
						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 已提交
926
					let err = new FileSystemManagerFailImpl(1300066);
DCloud-yyl's avatar
DCloud-yyl 已提交
927 928 929 930 931 932 933 934 935 936
					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 已提交
937
					let err = new FileSystemManagerFailImpl(1300013);
DCloud-yyl's avatar
DCloud-yyl 已提交
938 939 940 941 942 943 944 945 946 947 948 949
					options.fail?.(err)
					options.complete?.(err)
				})
				return
			}

			let targetFile = new File(filePath)
			if (targetFile.exists()) {
				/**
				 * 文件已经存在,则无法继续创建
				 */
				currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
950
					let err = new FileSystemManagerFailImpl(1301005);
DCloud-yyl's avatar
DCloud-yyl 已提交
951 952 953 954 955 956 957 958 959 960
					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 已提交
961
						let err = new FileSystemManagerFailImpl(1300002);
DCloud-yyl's avatar
DCloud-yyl 已提交
962 963 964 965 966 967 968 969 970 971
						options.fail?.(err)
						options.complete?.(err)
					})
					return
				} else {
					// 父文件夹存在,则继续创建
					let mkRet = targetFile.mkdir()
					if (!mkRet) {
						// 调用系统api 失败
						currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
972
							let err = new FileSystemManagerFailImpl(1300201);
DCloud-yyl's avatar
DCloud-yyl 已提交
973 974 975 976 977 978 979 980 981 982 983 984 985
							options.fail?.(err)
							options.complete?.(err)
						})

						return
					}
				}
			} else {
				// 设置了递归
				let mkRet = targetFile.mkdirs()
				if (!mkRet) {
					// 调用系统api 失败
					currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
986
						let err = new FileSystemManagerFailImpl(1300201);
DCloud-yyl's avatar
DCloud-yyl 已提交
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 1013
						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 已提交
1014
			throw new UniError(FileSystemManagerUniErrorSubject, 1300013, msgPrefix + FileSystemManagerUniErrors.get(1300013)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1015 1016 1017 1018
		}

		let targetFile = new File(temFilePath)
		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 1021 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 1038 1039 1040 1041 1042 1043
		}
	}

	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 已提交
1044
					let err = new FileSystemManagerFailImpl(1300013);
DCloud-yyl's avatar
DCloud-yyl 已提交
1045 1046 1047 1048 1049 1050 1051 1052 1053
					options.fail?.(err)
					options.complete?.(err)
				})
				return
			}

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

			// 文件存在,则进行删除操作
			let delRet = targetFile.delete()
			if (!delRet) {
				// 调用系统api 删除失败
				currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1066
					let err = new FileSystemManagerFailImpl(1300201);
DCloud-yyl's avatar
DCloud-yyl 已提交
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 1094
					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 已提交
1095
			throw new UniError(FileSystemManagerUniErrorSubject, 1200002, msgPrefix + FileSystemManagerUniErrors.get(1200002)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
		}
		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 已提交
1107
				throw new UniError(FileSystemManagerUniErrorSubject, 1300201, msgPrefix + FileSystemManagerUniErrors.get(1300201)! + ":" + filePath)
DCloud-yyl's avatar
DCloud-yyl 已提交
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
			}

			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 已提交
1133
				throw new UniError(FileSystemManagerUniErrorSubject, 1300002, msgPrefix + FileSystemManagerUniErrors.get(1300002)! + ":" + filePath);
DCloud-yyl's avatar
DCloud-yyl 已提交
1134 1135 1136
			}

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

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

			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 已提交
1170
			let err = new FileSystemManagerFailImpl(1200002);
DCloud-yyl's avatar
DCloud-yyl 已提交
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 1202
			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 已提交
1203
						let err = new FileSystemManagerFailImpl(1300201);
DCloud-yyl's avatar
DCloud-yyl 已提交
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 1240
						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 已提交
1241
						let err = new FileSystemManagerFailImpl(1300013);
DCloud-yyl's avatar
DCloud-yyl 已提交
1242 1243 1244 1245 1246 1247 1248 1249 1250
						options.fail?.(err)
						options.complete?.(err)
					})
					return
				}

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

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

				/**
				 * 文件超过100M,会超过应用内存
				 */
				if (targetFile.length() > 100 * 1024 * 1024) {
					currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1272
						let err = new FileSystemManagerFailImpl(1300202);
DCloud-yyl's avatar
DCloud-yyl 已提交
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 1314
						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 已提交
1315
			throw new UniError(FileSystemManagerUniErrorSubject, 1200002, msgPrefix + FileSystemManagerUniErrors.get(1200002)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1316 1317 1318 1319 1320 1321 1322
		}

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

		let isSandyBox = isSandyBoxPath(tempFilePath, false)
		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 1326 1327 1328
		}

		let nextFile = new File(tempFilePath)

		if (nextFile.exists() && nextFile.isDirectory()) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1329
			throw new UniError(FileSystemManagerUniErrorSubject, 1301003, msgPrefix + FileSystemManagerUniErrors.get(1301003)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
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 1366
		}

		/**
		 * 如果上一级目录不存在,创建之
		 */
		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 已提交
1367
			let err = new FileSystemManagerFailImpl(1200002);
DCloud-yyl's avatar
DCloud-yyl 已提交
1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
			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 已提交
1378
			let err = new FileSystemManagerFailImpl(1300013);
DCloud-yyl's avatar
DCloud-yyl 已提交
1379 1380 1381 1382 1383 1384 1385 1386 1387
			options.fail?.(err)
			options.complete?.(err)
			return
		}

		let nextFile = new File(filePath)

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

		let nextFile = new File(filePath)


		if (!nextFile.exists()) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1502
			let err = new FileSystemManagerFailImpl(1300002);
DCloud-yyl's avatar
DCloud-yyl 已提交
1503 1504 1505 1506 1507
			options.fail?.(err)
			options.complete?.(err)
			return
		} else if (nextFile.isDirectory()) {
			// 出错了,目标文件已存在,并且是个目录
DCloud-yyl's avatar
DCloud-yyl 已提交
1508
			let err = new FileSystemManagerFailImpl(1301003);
DCloud-yyl's avatar
DCloud-yyl 已提交
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 1559
			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 已提交
1560
			let err = new FileSystemManagerFailImpl(1302003);
DCloud-yyl's avatar
DCloud-yyl 已提交
1561 1562 1563 1564 1565 1566 1567 1568 1569
			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 已提交
1570
			let err = new FileSystemManagerFailImpl(1300013);
DCloud-yyl's avatar
DCloud-yyl 已提交
1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592
			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 已提交
1593
			throw new UniError(FileSystemManagerUniErrorSubject, 1302003, msgPrefix + FileSystemManagerUniErrors.get(1302003)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1594 1595 1596 1597 1598 1599
		}

		let filePath = UTSAndroid.convert2AbsFullPath(options.filePath)

		let isSandyBox = isSandyBoxPath(filePath, false)
		if (!isSandyBox) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1600
			throw new UniError(FileSystemManagerUniErrorSubject, 1300013, msgPrefix + FileSystemManagerUniErrors.get(1300013)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613
		}
		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 已提交
1614
					let err = new FileSystemManagerFailImpl(1300013);
DCloud-yyl's avatar
DCloud-yyl 已提交
1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625
					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 已提交
1626
					let err = new FileSystemManagerFailImpl(1300002);
DCloud-yyl's avatar
DCloud-yyl 已提交
1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640
					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 已提交
1641
					let err = new FileSystemManagerFailImpl(1300013);
DCloud-yyl's avatar
DCloud-yyl 已提交
1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653
					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 已提交
1654
					let err = new FileSystemManagerFailImpl(1300002);
DCloud-yyl's avatar
DCloud-yyl 已提交
1655 1656 1657 1658 1659 1660 1661 1662 1663 1664
					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 已提交
1665
					let err = new FileSystemManagerFailImpl(1300201);
DCloud-yyl's avatar
DCloud-yyl 已提交
1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689
					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 已提交
1690
			throw new UniError(FileSystemManagerUniErrorSubject, 1300013, msgPrefix + FileSystemManagerUniErrors.get(1300013)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1691 1692 1693 1694 1695 1696
		}
		let targetFile = new File(tfPath)
		if (!targetFile.exists()) {
			/**
			 * 文件不存在
			 */
DCloud-yyl's avatar
DCloud-yyl 已提交
1697
			throw new UniError(FileSystemManagerUniErrorSubject, 1300002, msgPrefix + FileSystemManagerUniErrors.get(1300002)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1698 1699 1700 1701 1702 1703 1704 1705 1706
		}
		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 已提交
1707
			throw new UniError(FileSystemManagerUniErrorSubject, 1300013, msgPrefix + FileSystemManagerUniErrors.get(1300013)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1708 1709 1710 1711 1712 1713 1714
		}
		let newFile = new File(newFilePath)
		console.log(newFile.getPath())
		if (newFile.getParentFile() != null && !newFile.getParentFile()!.exists()) {
			/**
			 * 父文件不存在
			 */
DCloud-yyl's avatar
DCloud-yyl 已提交
1715
			throw new UniError(FileSystemManagerUniErrorSubject, 1300002, msgPrefix + FileSystemManagerUniErrors.get(1300002)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1716 1717 1718 1719 1720
		}

		let copyRetFile = targetFile.copyTo(newFile, true)
		if (!copyRetFile.exists()) {
			// 调用系统api 失败
DCloud-yyl's avatar
DCloud-yyl 已提交
1721
			throw new UniError(FileSystemManagerUniErrorSubject, 1300201, msgPrefix + FileSystemManagerUniErrors.get(1300201)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1722 1723 1724 1725 1726 1727 1728 1729
		}
		targetFile.delete()
		return newFilePath
	}
	public unzip(options : UnzipFileOptions) {
		let msgPrefix = "unzip:fail "
		// 检查来源文件
		let zipFilePath = UTSAndroid.convert2AbsFullPath(options.zipFilePath)
DCloud-yyl's avatar
DCloud-yyl 已提交
1730
		
DCloud-yyl's avatar
DCloud-yyl 已提交
1731 1732
		let currentDispatcher = UTSAndroid.getDispatcher("main")
		UTSAndroid.getDispatcher('io').async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1733
			let is:InputStream;
DCloud-yyl's avatar
DCloud-yyl 已提交
1734
			let targetPath = UTSAndroid.convert2AbsFullPath(options.targetPath)
DCloud-yyl's avatar
DCloud-yyl 已提交
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
			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
			}
DCloud-yyl's avatar
DCloud-yyl 已提交
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
			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 (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1840
					let err = new FileSystemManagerFailImpl(1300201);
DCloud-yyl's avatar
DCloud-yyl 已提交
1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861
					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 已提交
1862
					let err = new FileSystemManagerFailImpl(1300002);
DCloud-yyl's avatar
DCloud-yyl 已提交
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
					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 已提交
1903
					let err = new FileSystemManagerFailImpl(1300002);
DCloud-yyl's avatar
DCloud-yyl 已提交
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
					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 已提交
1939
			throw new UniError(FileSystemManagerUniErrorSubject, 1300002, msgPrefix + FileSystemManagerUniErrors.get(1300002)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954
		}

		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: ""
			}
DCloud-yyl's avatar
DCloud-yyl 已提交
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
			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)

			}
DCloud-yyl's avatar
DCloud-yyl 已提交
1997
			try {
DCloud-yyl's avatar
DCloud-yyl 已提交
1998
				let brInput = new BrotliInputStream(is)
DCloud-yyl's avatar
DCloud-yyl 已提交
1999 2000 2001 2002
				success.data = new String(brInput.readBytes())
				brInput.close()
			} catch (e : Exception) {
				currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
2003
					let err = new FileSystemManagerFailImpl(1300201);
DCloud-yyl's avatar
DCloud-yyl 已提交
2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018
					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 已提交
2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039
		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)
DCloud-yyl's avatar
DCloud-yyl 已提交
2040 2041
		}
		let data : string
DCloud-yyl's avatar
DCloud-yyl 已提交
2042

DCloud-yyl's avatar
DCloud-yyl 已提交
2043
		try {
DCloud-yyl's avatar
DCloud-yyl 已提交
2044
			let brInput = new BrotliInputStream(is)
DCloud-yyl's avatar
DCloud-yyl 已提交
2045 2046 2047
			data = new String(brInput.readBytes())
			brInput.close()
		} catch (e : Exception) {
DCloud-yyl's avatar
DCloud-yyl 已提交
2048
			throw new UniError(FileSystemManagerUniErrorSubject, 1300201, msgPrefix + FileSystemManagerUniErrors.get(1300201)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063
		}
		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 已提交
2064
					let err = new FileSystemManagerFailImpl(1300002);
DCloud-yyl's avatar
DCloud-yyl 已提交
2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083
					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 已提交
2084
			let err = new FileSystemManagerFailImpl(1200002);
DCloud-yyl's avatar
DCloud-yyl 已提交
2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095
			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 已提交
2096
				let err = new FileSystemManagerFailImpl(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 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142
				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 已提交
2143
			throw new UniError(FileSystemManagerUniErrorSubject, 1200002, msgPrefix + FileSystemManagerUniErrors.get(1200002)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
2144 2145 2146
		}
		let fd = ParcelFileDescriptor.fromFd(options.fd.toInt())
		if (fd == null) {
DCloud-yyl's avatar
DCloud-yyl 已提交
2147
			throw new UniError(FileSystemManagerUniErrorSubject, 1300009, msgPrefix + FileSystemManagerUniErrors.get(1300009)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
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
		}
		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 已提交
2179
			throw new UniError(FileSystemManagerUniErrorSubject, 1300201, msgPrefix + FileSystemManagerUniErrors.get(1300201)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200
		}
		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 已提交
2201
				let err = new FileSystemManagerFailImpl(1300009);
DCloud-yyl's avatar
DCloud-yyl 已提交
2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215
				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 已提交
2216
			throw new UniError(FileSystemManagerUniErrorSubject, 1300009, msgPrefix + FileSystemManagerUniErrors.get(1300009)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
2217 2218 2219 2220 2221 2222 2223 2224
		}
	}
	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 已提交
2225
						let err = new FileSystemManagerFailImpl(1300009);
DCloud-yyl's avatar
DCloud-yyl 已提交
2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241
						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 已提交
2242
			throw new UniError(FileSystemManagerUniErrorSubject, 1300009, msgPrefix + FileSystemManagerUniErrors.get(1300009)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
2243 2244 2245 2246 2247 2248 2249 2250
		}
		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 已提交
2251
					let err = new FileSystemManagerFailImpl(1300009);
DCloud-yyl's avatar
DCloud-yyl 已提交
2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275
					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 已提交
2276
			throw new UniError(FileSystemManagerUniErrorSubject, 1300009, msgPrefix + FileSystemManagerUniErrors.get(1300009)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293
		}
		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()
		}
DCloud-yyl's avatar
DCloud-yyl 已提交
2294

DCloud-yyl's avatar
DCloud-yyl 已提交
2295
		let target = this
DCloud-yyl's avatar
DCloud-yyl 已提交
2296
		let zipOptions : UnzipFileOptions = {//todo 需要去掉
DCloud-yyl's avatar
DCloud-yyl 已提交
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
			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()
				})
			},
DCloud-yyl's avatar
DCloud-yyl 已提交
2366 2367
			fail: (res) => {
				let err = new FileSystemManagerFailImpl(res.errCode);
DCloud-yyl's avatar
DCloud-yyl 已提交
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 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 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
				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)
}