index.uts 3.3 KB
Newer Older
lizhongyi_'s avatar
lizhongyi_ 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
import { ReadFileSuccessResult, ReadFile,ReadFileOptions } from "../interface.uts"
import { WriteFileSuccessResult, WriteFile,WriteFileOptions } from "../interface.uts"
import { GetFileSystemManager,FileSystemManager} from "../interface.uts"
import { UniErrorSubject, UniErrors } from "../unierror.uts"

import File from "java.io.File" 
import UTSAndroid from 'io.dcloud.uts.UTSAndroid';
import Base64 from "android.util.Base64"

export { ReadFileOptions, WriteFileOptions } from "../interface.uts"


class AndroidFileSystemManager implements FileSystemManager{
	
	public readFile(options : ReadFileOptions) {
		
		// 判断type 是否合法
		if(options.encoding != 'base64' && options.encoding != 'utf-8'){
			let err = new UniError(UniErrorSubject, 1200002, UniErrors.get(1200002)!);
			options.fail?.(err)
			options.complete?.(err)
			return
		}
		
		let filePath = UTSAndroid.convert2AbsFullPath(options.filePath)
		let targetFile = new File(filePath)
		if (!targetFile.exists()) {
			let err = new UniError(UniErrorSubject, 1300002, UniErrors.get(1300002)! + filePath);
			options.fail?.(err)
			options.complete?.(err)
			return
		}
		
		if (targetFile.isDirectory()) {
			let err = new UniError(UniErrorSubject,1301003,UniErrors.get(1301003)!);
			options.fail?.(err)
			options.complete?.(err)
			return
		}
		
		/**
		 * 文件超过16M,会超过应用内存  
		 */
		if (targetFile.length() > 16 * 1024 * 1024) {
			let err = new UniError(UniErrorSubject,1300202,UniErrors.get(1300202)!);
			options.fail?.(err)
			options.complete?.(err)
			return
		}
		
		let currentDispatcher = UTSAndroid.getDispatcher()
		/**
		 * 执行真正的加载行为,为了避免阻塞分发到 io任务序列
		 */
		UTSAndroid.dispatchAsync('io',function(_){
			
			let ret : ReadFileSuccessResult = {
				data : ""
			}
			
			if(options.encoding == 'base64'){
				// base64
				let byteArray = targetFile.readBytes()
				let base64Content = Base64.encodeToString(byteArray,Base64.NO_WRAP)
				ret.data = base64Content
				
			}else{
				// text
				let text = targetFile.readText()
				ret.data = text
			}
			
			currentDispatcher.async(function(_){
				options.success?.(ret)
				options.complete?.(ret)
			})
			
		},null)
		
	}
	
	
	public writeFile(options : WriteFileOptions) {
		// 判断type 是否合法
		let nextFile = new File(UTSAndroid.getAppContext()?.getFilesDir(),options.filePath)
		if(nextFile.exists() && nextFile.isDirectory()){
			// 出错了,目标文件已存在,并且是个目录
			let err = new UniError(UniErrorSubject,1301003,UniErrors.get(1301003)!);
			options.fail?.(err)
			options.complete?.(err)
			return
		}
		
		
		let currentDispatcher = UTSAndroid.getDispatcher()
		UTSAndroid.dispatchAsync('io',function(_){
			
			/**
			 * 如果上一级目录不存在,创建之
			 */
			if(!nextFile.parentFile.exists()){
				nextFile.parentFile.mkdirs()
			}
			
			if(!nextFile.exists()){
				nextFile.createNewFile()
			}
			// 写入文本,暂时只支持覆盖写入
			nextFile.writeText(options.data)
			
			let ret : WriteFileSuccessResult = {
				filePath: nextFile.getPath()
			}
			
			currentDispatcher.async(function(_){
				options.success?.(ret)
				options.complete?.(ret)
			})
		},null)
	}
}

export const getFileSystemManager:GetFileSystemManager = function ():FileSystemManager {
	return new AndroidFileSystemManager()
};