index.uts 57.0 KB
Newer Older
DCloud-yyl's avatar
DCloud-yyl 已提交
1
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, ReadOption, ReadSyncOption, ReadResult } from "../interface.uts"
DCloud-yyl's avatar
DCloud-yyl 已提交
2 3
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
import { FileDescriptorUtil } from "./FileDescriptorUtil"
DCloud-yyl's avatar
DCloud-yyl 已提交
6 7
import { WriteFileUtil } from "./WriteFileUtil"
import { ReadFileUtil } from "./ReadFileUtil"
DCloud-yyl's avatar
DCloud-yyl 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
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 已提交
23
import InputStream from 'java.io.InputStream';
DCloud-yyl's avatar
DCloud-yyl 已提交
24 25 26
import DA from 'io.dcloud.common.cs.DA';
import Op from 'androidx.fragment.app.FragmentTransaction.Op';

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

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 {
DCloud-yyl's avatar
DCloud-yyl 已提交
88 89 90 91
	private fileDesUtil : FileDescriptorUtil = new FileDescriptorUtil()
	private writeFileUtil : WriteFileUtil = new WriteFileUtil()
	private readFileUtil : ReadFileUtil = new ReadFileUtil()

DCloud-yyl's avatar
DCloud-yyl 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
	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 已提交
108
						let err = new FileSystemManagerFailImpl(1300013);
DCloud-yyl's avatar
DCloud-yyl 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121
						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 已提交
122
						let err = new FileSystemManagerFailImpl(1300002);
DCloud-yyl's avatar
DCloud-yyl 已提交
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 182 183 184 185 186 187 188 189
						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 已提交
190
			throw new UniError(FileSystemManagerUniErrorSubject, 1300013, msgPrefix + FileSystemManagerUniErrors.get(1300013)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
191 192 193 194 195 196
		}
		let targetFile = new File(filePath)
		if (!targetFile.exists()) {
			/**
			 * 文件不存在
			 */
DCloud-yyl's avatar
DCloud-yyl 已提交
197
			throw new UniError(FileSystemManagerUniErrorSubject, 1300002, msgPrefix + FileSystemManagerUniErrors.get(1300002)!);
DCloud-yyl's avatar
DCloud-yyl 已提交
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 237 238 239 240 241 242 243 244
		}
		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 已提交
245
					let err = new FileSystemManagerFailImpl(1300013);
DCloud-yyl's avatar
DCloud-yyl 已提交
246 247 248 249 250 251 252 253 254 255 256 257 258
					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 已提交
259
					let err = new FileSystemManagerFailImpl(1300002);
DCloud-yyl's avatar
DCloud-yyl 已提交
260 261 262 263 264 265 266 267 268 269 270
					options.fail?.(err)
					options.complete?.(err)
				}, null)
				return
			}

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

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

			let copyRetFile = targetFile.copyTo(newFile, true)
			if (!copyRetFile.exists()) {
				// 调用系统api 失败
DCloud-yyl's avatar
DCloud-yyl 已提交
402
				throw new UniError(FileSystemManagerUniErrorSubject, 1300201, msgPrefix + FileSystemManagerUniErrors.get(1300201)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
			}
		}
	}


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

		let targetFile = new File(filePath)
		if (!targetFile.exists()) {
			/**
			 * 文件不存在
			 */
DCloud-yyl's avatar
DCloud-yyl 已提交
567
			throw new UniError(FileSystemManagerUniErrorSubject, 1300002, msgPrefix + FileSystemManagerUniErrors.get(1300002)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
568 569 570 571
		}
		let newFilePath = UTSAndroid.convert2AbsFullPath(newPath)
		isSandyBox = isSandyBoxPath(newFilePath, false)
		if (!isSandyBox) {
DCloud-yyl's avatar
DCloud-yyl 已提交
572
			throw new UniError(FileSystemManagerUniErrorSubject, 1300013, msgPrefix + FileSystemManagerUniErrors.get(1300013)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
573 574 575 576 577 578 579
		}
		let newFile = new File(newFilePath)

		if (newFile.getParentFile() == null || !newFile.getParentFile()!.exists()) {
			/**
			 * 父文件不存在
			 */
DCloud-yyl's avatar
DCloud-yyl 已提交
580
			throw new UniError(FileSystemManagerUniErrorSubject, 1300002, msgPrefix + FileSystemManagerUniErrors.get(1300002)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
581 582 583 584 585 586
		}


		let renameRet = targetFile.renameTo(newFile)
		if (!renameRet) {
			// 调用系统api 失败
DCloud-yyl's avatar
DCloud-yyl 已提交
587
			throw new UniError(FileSystemManagerUniErrorSubject, 1300201, msgPrefix + FileSystemManagerUniErrors.get(1300201)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
588 589 590 591 592 593 594 595 596 597 598 599
		}
	}

	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 已提交
600
					let err = new FileSystemManagerFailImpl(1300013);
DCloud-yyl's avatar
DCloud-yyl 已提交
601 602 603 604 605 606 607 608 609 610 611 612
					options.fail?.(err)
					options.complete?.(err)
				})
				return
			}

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


			let renameRet = targetFile.renameTo(newFile)
			if (!renameRet) {
				// 调用系统api 失败
				currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
648
					let err = new FileSystemManagerFailImpl(1300201);
DCloud-yyl's avatar
DCloud-yyl 已提交
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
					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 已提交
672
			throw new UniError(FileSystemManagerUniErrorSubject, 1300013, msgPrefix + FileSystemManagerUniErrors.get(1300013)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
673 674 675 676 677 678
		}

		if (!targetFile.exists()) {
			/**
			 * 文件不存在,或者不是文件夹,异常
			 */
DCloud-yyl's avatar
DCloud-yyl 已提交
679
			throw new UniError(FileSystemManagerUniErrorSubject, 1300002, msgPrefix + FileSystemManagerUniErrors.get(1300002)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
680 681 682 683 684 685 686 687 688 689 690 691 692 693
		}
	}

	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 已提交
694
					let err = new FileSystemManagerFailImpl(1300013);
DCloud-yyl's avatar
DCloud-yyl 已提交
695 696 697 698 699 700 701 702 703 704 705
					options.fail?.(err)
					options.complete?.(err)
				})
				return
			}

			if (!targetFile.exists()) {
				/**
				 * 文件不存在,或者不是文件夹,异常
				 */
				currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
706
					let err = new FileSystemManagerFailImpl(1300002);
DCloud-yyl's avatar
DCloud-yyl 已提交
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
					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 已提交
732
			throw new UniError(FileSystemManagerUniErrorSubject, 1300013, msgPrefix + FileSystemManagerUniErrors.get(1300013)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
733 734 735 736 737 738 739
		}

		let targetFile = new File(filePath)
		if (!targetFile.exists() || !targetFile.isDirectory()) {
			/**
			 * 文件不存在,或者不是文件夹,异常
			 */
DCloud-yyl's avatar
DCloud-yyl 已提交
740
			throw new UniError(FileSystemManagerUniErrorSubject, 1300002, msgPrefix + FileSystemManagerUniErrors.get(1300002)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
741 742 743 744 745 746 747 748 749 750 751 752
		}
		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 已提交
753
					let err = new FileSystemManagerFailImpl(1300013);
DCloud-yyl's avatar
DCloud-yyl 已提交
754 755 756 757 758 759 760 761 762 763 764 765
					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 已提交
766
					let err = new FileSystemManagerFailImpl(1300002);
DCloud-yyl's avatar
DCloud-yyl 已提交
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
					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 已提交
789
			throw new UniError(FileSystemManagerUniErrorSubject, 1300013, msgPrefix + FileSystemManagerUniErrors.get(1300013)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
790 791 792 793 794 795
		}

		if (!targetFile.exists() || !targetFile.isDirectory()) {
			/**
			 * 文件不存在 或者 文件是不是文件夹
			 */
DCloud-yyl's avatar
DCloud-yyl 已提交
796
			throw new UniError(FileSystemManagerUniErrorSubject, 1300002, msgPrefix + FileSystemManagerUniErrors.get(1300002)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
797 798 799 800 801 802 803 804
		}


		if (!recursive) {
			// 没有设置递归,则需要检查是否保存子目录
			let childList = targetFile.list()
			if (childList != null && childList.size > 0) {
				// 存在子目录
DCloud-yyl's avatar
DCloud-yyl 已提交
805
				throw new UniError(FileSystemManagerUniErrorSubject, 1300066, msgPrefix + FileSystemManagerUniErrors.get(1300066)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
806 807 808 809 810
			}
		} else {
			let delRet = targetFile.deleteRecursively()
			if (!delRet) {
				// 调用系统api 失败
DCloud-yyl's avatar
DCloud-yyl 已提交
811
				throw new UniError(FileSystemManagerUniErrorSubject, 1300201, msgPrefix + FileSystemManagerUniErrors.get(1300201)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
812 813 814 815 816 817 818
			}
		}
	}

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

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

	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 已提交
867
					let err = new FileSystemManagerFailImpl(1300013);
DCloud-yyl's avatar
DCloud-yyl 已提交
868 869 870 871 872 873 874 875 876 877 878
					options.fail?.(err)
					options.complete?.(err)
				})
				return
			}

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

					return
				}
			} else {
				let delRet = targetFile.deleteRecursively()
				if (!delRet) {
					// 调用系统api 失败
					currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
905
						let err = new FileSystemManagerFailImpl(1300201);
DCloud-yyl's avatar
DCloud-yyl 已提交
906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933
						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 已提交
934
					let err = new FileSystemManagerFailImpl(1300066);
DCloud-yyl's avatar
DCloud-yyl 已提交
935 936 937 938 939 940 941 942 943 944
					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 已提交
945
					let err = new FileSystemManagerFailImpl(1300013);
DCloud-yyl's avatar
DCloud-yyl 已提交
946 947 948 949 950 951 952 953 954 955 956 957
					options.fail?.(err)
					options.complete?.(err)
				})
				return
			}

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

						return
					}
				}
			} else {
				// 设置了递归
				let mkRet = targetFile.mkdirs()
				if (!mkRet) {
					// 调用系统api 失败
					currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
994
						let err = new FileSystemManagerFailImpl(1300201);
DCloud-yyl's avatar
DCloud-yyl 已提交
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
						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 已提交
1022
			throw new UniError(FileSystemManagerUniErrorSubject, 1300013, msgPrefix + FileSystemManagerUniErrors.get(1300013)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1023 1024 1025 1026
		}

		let targetFile = new File(temFilePath)
		if (!targetFile.exists()) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1027
			throw new UniError(FileSystemManagerUniErrorSubject, 1300002, msgPrefix + FileSystemManagerUniErrors.get(1300002)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1028 1029 1030 1031 1032 1033
		}

		// 文件存在,则进行删除操作
		let delRet = targetFile.delete()
		if (!delRet) {
			// 调用系统api 删除失败
DCloud-yyl's avatar
DCloud-yyl 已提交
1034
			throw new UniError(FileSystemManagerUniErrorSubject, 1300201, msgPrefix + FileSystemManagerUniErrors.get(1300201)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
		}
	}

	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 已提交
1052
					let err = new FileSystemManagerFailImpl(1300013);
DCloud-yyl's avatar
DCloud-yyl 已提交
1053 1054 1055 1056 1057 1058 1059 1060 1061
					options.fail?.(err)
					options.complete?.(err)
				})
				return
			}

			let targetFile = new File(filePath)
			if (!targetFile.exists()) {
				currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1062
					let err = new FileSystemManagerFailImpl(1300002);
DCloud-yyl's avatar
DCloud-yyl 已提交
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
					options.fail?.(err)
					options.complete?.(err)
				})
				return
			}

			// 文件存在,则进行删除操作
			let delRet = targetFile.delete()
			if (!delRet) {
				// 调用系统api 删除失败
				currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1074
					let err = new FileSystemManagerFailImpl(1300201);
DCloud-yyl's avatar
DCloud-yyl 已提交
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)



	}

DCloud-yyl's avatar
DCloud-yyl 已提交
1095 1096
	public readFileSync(filePath : string, encoding ?: string) : any {
		return this.readFileUtil.readFileSync(filePath, encoding)
DCloud-yyl's avatar
DCloud-yyl 已提交
1097 1098 1099
	}

	public readFile(options : ReadFileOptions) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1100
		this.readFileUtil.readFile(options)
DCloud-yyl's avatar
DCloud-yyl 已提交
1101 1102
	}

DCloud-yyl's avatar
DCloud-yyl 已提交
1103 1104
	public writeFileSync(filePath : string, data : any, encoding ?: string) : void {
		this.writeFileUtil.writeFileSync(filePath, data, encoding)
DCloud-yyl's avatar
DCloud-yyl 已提交
1105 1106 1107
	}


DCloud-yyl's avatar
DCloud-yyl 已提交
1108 1109
	public writeFile(options : WriteFileOptions) {
		this.writeFileUtil.writeFile(options)
DCloud-yyl's avatar
DCloud-yyl 已提交
1110 1111
	}

DCloud-yyl's avatar
DCloud-yyl 已提交
1112 1113
	public appendFileSync(filePath : string, data : any, encoding ?: string) {
		this.writeFileUtil.appendFileSync(filePath, data, encoding)
DCloud-yyl's avatar
DCloud-yyl 已提交
1114
	}
DCloud-yyl's avatar
DCloud-yyl 已提交
1115 1116 1117
	// public appendFileSync(filePath : string, data : any) {
	// 	this.writeFileUtil.appendFileSync(filePath, data)
	// }
DCloud-yyl's avatar
DCloud-yyl 已提交
1118 1119

	public appendFile(options : AppendFileOptions) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1120
		this.writeFileUtil.appendFile(options)
DCloud-yyl's avatar
DCloud-yyl 已提交
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
	}
	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 已提交
1135
			let err = new FileSystemManagerFailImpl(1302003);
DCloud-yyl's avatar
DCloud-yyl 已提交
1136 1137 1138 1139 1140 1141 1142 1143 1144
			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 已提交
1145
			let err = new FileSystemManagerFailImpl(1300013);
DCloud-yyl's avatar
DCloud-yyl 已提交
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
			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 已提交
1168
			throw new UniError(FileSystemManagerUniErrorSubject, 1302003, msgPrefix + FileSystemManagerUniErrors.get(1302003)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1169 1170 1171 1172 1173 1174
		}

		let filePath = UTSAndroid.convert2AbsFullPath(options.filePath)

		let isSandyBox = isSandyBoxPath(filePath, false)
		if (!isSandyBox) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1175
			throw new UniError(FileSystemManagerUniErrorSubject, 1300013, msgPrefix + FileSystemManagerUniErrors.get(1300013)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
		}
		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 已提交
1189
					let err = new FileSystemManagerFailImpl(1300013);
DCloud-yyl's avatar
DCloud-yyl 已提交
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
					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 已提交
1201
					let err = new FileSystemManagerFailImpl(1300002);
DCloud-yyl's avatar
DCloud-yyl 已提交
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
					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 已提交
1216
					let err = new FileSystemManagerFailImpl(1300013);
DCloud-yyl's avatar
DCloud-yyl 已提交
1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
					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 已提交
1229
					let err = new FileSystemManagerFailImpl(1300002);
DCloud-yyl's avatar
DCloud-yyl 已提交
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
					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 已提交
1240
					let err = new FileSystemManagerFailImpl(1300201);
DCloud-yyl's avatar
DCloud-yyl 已提交
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
					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 已提交
1265
			throw new UniError(FileSystemManagerUniErrorSubject, 1300013, msgPrefix + FileSystemManagerUniErrors.get(1300013)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1266 1267 1268 1269 1270 1271
		}
		let targetFile = new File(tfPath)
		if (!targetFile.exists()) {
			/**
			 * 文件不存在
			 */
DCloud-yyl's avatar
DCloud-yyl 已提交
1272
			throw new UniError(FileSystemManagerUniErrorSubject, 1300002, msgPrefix + FileSystemManagerUniErrors.get(1300002)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1273 1274 1275 1276 1277 1278 1279 1280 1281
		}
		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 已提交
1282
			throw new UniError(FileSystemManagerUniErrorSubject, 1300013, msgPrefix + FileSystemManagerUniErrors.get(1300013)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1283 1284 1285 1286 1287 1288 1289
		}
		let newFile = new File(newFilePath)
		console.log(newFile.getPath())
		if (newFile.getParentFile() != null && !newFile.getParentFile()!.exists()) {
			/**
			 * 父文件不存在
			 */
DCloud-yyl's avatar
DCloud-yyl 已提交
1290
			throw new UniError(FileSystemManagerUniErrorSubject, 1300002, msgPrefix + FileSystemManagerUniErrors.get(1300002)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1291 1292 1293 1294 1295
		}

		let copyRetFile = targetFile.copyTo(newFile, true)
		if (!copyRetFile.exists()) {
			// 调用系统api 失败
DCloud-yyl's avatar
DCloud-yyl 已提交
1296
			throw new UniError(FileSystemManagerUniErrorSubject, 1300201, msgPrefix + FileSystemManagerUniErrors.get(1300201)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1297 1298 1299 1300 1301 1302 1303 1304
		}
		targetFile.delete()
		return newFilePath
	}
	public unzip(options : UnzipFileOptions) {
		let msgPrefix = "unzip:fail "
		// 检查来源文件
		let zipFilePath = UTSAndroid.convert2AbsFullPath(options.zipFilePath)
DCloud-yyl's avatar
DCloud-yyl 已提交
1305

DCloud-yyl's avatar
DCloud-yyl 已提交
1306 1307
		let currentDispatcher = UTSAndroid.getDispatcher("main")
		UTSAndroid.getDispatcher('io').async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1308
			let is : InputStream;
DCloud-yyl's avatar
DCloud-yyl 已提交
1309
			let targetPath = UTSAndroid.convert2AbsFullPath(options.targetPath)
DCloud-yyl's avatar
DCloud-yyl 已提交
1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
			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
				}
DCloud-yyl's avatar
DCloud-yyl 已提交
1347

DCloud-yyl's avatar
DCloud-yyl 已提交
1348 1349 1350 1351 1352 1353 1354 1355 1356
				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
DCloud-yyl's avatar
DCloud-yyl 已提交
1357

DCloud-yyl's avatar
DCloud-yyl 已提交
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
				}
				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 已提交
1368
			}
DCloud-yyl's avatar
DCloud-yyl 已提交
1369

DCloud-yyl's avatar
DCloud-yyl 已提交
1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414
			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 已提交
1415
					let err = new FileSystemManagerFailImpl(1300201);
DCloud-yyl's avatar
DCloud-yyl 已提交
1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
					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 已提交
1437
					let err = new FileSystemManagerFailImpl(1300002);
DCloud-yyl's avatar
DCloud-yyl 已提交
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477
					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 已提交
1478
					let err = new FileSystemManagerFailImpl(1300002);
DCloud-yyl's avatar
DCloud-yyl 已提交
1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513
					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 已提交
1514
			throw new UniError(FileSystemManagerUniErrorSubject, 1300002, msgPrefix + FileSystemManagerUniErrors.get(1300002)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529
		}

		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 已提交
1530
			let is : InputStream
DCloud-yyl's avatar
DCloud-yyl 已提交
1531 1532 1533 1534 1535
			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);
DCloud-yyl's avatar
DCloud-yyl 已提交
1536

DCloud-yyl's avatar
DCloud-yyl 已提交
1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571
				} 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 已提交
1572
			try {
DCloud-yyl's avatar
DCloud-yyl 已提交
1573
				let brInput = new BrotliInputStream(is)
DCloud-yyl's avatar
DCloud-yyl 已提交
1574 1575 1576 1577
				success.data = new String(brInput.readBytes())
				brInput.close()
			} catch (e : Exception) {
				currentDispatcher.async(function (_) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1578
					let err = new FileSystemManagerFailImpl(1300201);
DCloud-yyl's avatar
DCloud-yyl 已提交
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593
					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 已提交
1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614
		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 已提交
1615 1616
		}
		let data : string
DCloud-yyl's avatar
DCloud-yyl 已提交
1617

DCloud-yyl's avatar
DCloud-yyl 已提交
1618
		try {
DCloud-yyl's avatar
DCloud-yyl 已提交
1619
			let brInput = new BrotliInputStream(is)
DCloud-yyl's avatar
DCloud-yyl 已提交
1620 1621 1622
			data = new String(brInput.readBytes())
			brInput.close()
		} catch (e : Exception) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1623
			throw new UniError(FileSystemManagerUniErrorSubject, 1300201, msgPrefix + FileSystemManagerUniErrors.get(1300201)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638
		}
		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 已提交
1639
					let err = new FileSystemManagerFailImpl(1300002);
DCloud-yyl's avatar
DCloud-yyl 已提交
1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656
					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) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1657 1658
		this.writeFileUtil.write(options)
	}
DCloud-yyl's avatar
DCloud-yyl 已提交
1659

DCloud-yyl's avatar
DCloud-yyl 已提交
1660 1661 1662 1663 1664 1665
	public writeSync(options : WriteSyncOptions) : WriteResult {
		return this.writeFileUtil.writeSync(options)
	}
	public read(option : ReadOption) : void {
		this.readFileUtil.read(option)
	}
DCloud-yyl's avatar
DCloud-yyl 已提交
1666 1667


DCloud-yyl's avatar
DCloud-yyl 已提交
1668 1669
	public readSync(option : ReadSyncOption) : ReadResult {
		return this.readFileUtil.readSync(option)
DCloud-yyl's avatar
DCloud-yyl 已提交
1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686
	}

	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 已提交
1687
				let err = new FileSystemManagerFailImpl(1300009);
DCloud-yyl's avatar
DCloud-yyl 已提交
1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701
				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 已提交
1702
			throw new UniError(FileSystemManagerUniErrorSubject, 1300009, msgPrefix + FileSystemManagerUniErrors.get(1300009)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1703 1704 1705 1706 1707 1708 1709 1710
		}
	}
	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 已提交
1711
						let err = new FileSystemManagerFailImpl(1300009);
DCloud-yyl's avatar
DCloud-yyl 已提交
1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727
						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 已提交
1728
			throw new UniError(FileSystemManagerUniErrorSubject, 1300009, msgPrefix + FileSystemManagerUniErrors.get(1300009)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1729 1730 1731 1732 1733 1734 1735 1736
		}
		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 已提交
1737
					let err = new FileSystemManagerFailImpl(1300009);
DCloud-yyl's avatar
DCloud-yyl 已提交
1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761
					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 已提交
1762
			throw new UniError(FileSystemManagerUniErrorSubject, 1300009, msgPrefix + FileSystemManagerUniErrors.get(1300009)!)
DCloud-yyl's avatar
DCloud-yyl 已提交
1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779
		}
		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 已提交
1780

DCloud-yyl's avatar
DCloud-yyl 已提交
1781
		let target = this
DCloud-yyl's avatar
DCloud-yyl 已提交
1782
		let zipOptions : UnzipFileOptions = {//todo 需要去掉
DCloud-yyl's avatar
DCloud-yyl 已提交
1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813
			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()) {
DCloud-yyl's avatar
DCloud-yyl 已提交
1814 1815
									let data : any | null
									data = target.readFileSync(file.getPath(), options.encoding)
DCloud-yyl's avatar
DCloud-yyl 已提交
1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852
									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 已提交
1853 1854
			fail: (res) => {
				let err = new FileSystemManagerFailImpl(res.errCode);
DCloud-yyl's avatar
DCloud-yyl 已提交
1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868
				options.fail?.(err)
				options.complete?.(err)
			}
		}
		this.unzip(zipOptions)
	}
}
export const getFileSystemManager : GetFileSystemManager = function () : FileSystemManager {
	return AndroidFileSystemManager.getManager()
};

/**
 * 判断传入的路径是否是沙盒路径,如果不是则不能继续操作
 */
DCloud-yyl's avatar
DCloud-yyl 已提交
1869
export function isSandyBoxPath(inputPath : string, onlyRead : boolean) : boolean {
DCloud-yyl's avatar
DCloud-yyl 已提交
1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955
	//处理非沙盒目录时 需要校验一下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)
}