index.uts 8.3 KB
Newer Older
DCloud-yyl's avatar
DCloud-yyl 已提交
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 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 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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
import { WriteFileOptions, ReadFileOptions, MkDirOptions, RmDirOptions, UnLinkOptions, ReadDirOptions, AccessOptions, RenameOptions, GetFileInfoOptions, CopyFileOptions, StatOptions } from "../interface.uts"
import { ReadFileSuccessResult, FileManagerSuccessResult, ReadDirSuccessResult, GetFileInfoSuccessResult, StatSuccessResult, FileStats, Stats } from "../interface.uts"
import { GetFileSystemManager, FileSystemManager } from "../interface.uts"
import { FileSystemManagerFailImpl, FileSystemManagerUniErrorSubject, FileSystemManagerUniErrors } from "../unierror.uts"

class InnerStats implements Stats {
	/**
		 * 文件的类型和存取的权限,对应 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
	}

}

function wrapFileStats(nativeStats) : FileStats {
	let stats = new InnerStats()
	stats.mIsFile = nativeStats.isFile
	stats.mode = nativeStats.mode
	stats.size = nativeStats.size
	stats.lastAccessedTime = nativeStats.lastAccessedTime
	stats.lastModifiedTime = nativeStats.lastModifiedTime
	return new FileStats({ path: nativeStats.path, stats: stats })
}

function wrapStatsArgument(nativeStats) : FileStats {
	// if ( nativeStats.length == 1 ){
	// 	return wrapFileStats(nativeStats[0])
	// }
	return nativeStats.map( item => {
		return wrapFileStats(item)
	})
}

class JsFileSystemManager implements FileSystemManager {
	public stat(options : StatOptions) {
		nativeFileManager.stat(options.path,
			options.recursive,
			function (res) {
				var ret = new StatSuccessResult({ stats: wrapStatsArgument(res), "errMsg": "stat:ok" });
				options.success?.(ret)
				options.complete?.(ret)
			},
			function (code) {
				let err = new FileSystemManagerFailImpl(code)
				options.fail?.(err)
				options.complete?.(err)
			})
	}

	public getFileInfo(options : GetFileInfoOptions) {
		if(options.digestAlgorithm == null || options.digestAlgorithm == undefined){
			options.digestAlgorithm = "md5"
		} else if (options.digestAlgorithm!.toLowerCase() != 'md5' && options.digestAlgorithm!.toLowerCase() != 'sha1') {
			let err = new FileSystemManagerFailImpl(1300022);
			options.fail?.(err)
			options.complete?.(err)
			return
		}
		
		nativeFileManager.getFileInfo(options.filePath,
			options.digestAlgorithm,
			function (res) {
				var ret = new GetFileInfoSuccessResult({ size: res.size, digest: res.digest, errMsg: "getFileInfo:ok" });
				options.success?.(ret)
				options.complete?.(ret)
			},
			function (code) {
				let err = new FileSystemManagerFailImpl(code);
				options.fail?.(err)
				options.complete?.(err)
			})
	}

	// public copyFileSync(srcPath:string,destPath:string):number{
	// 	return 0
	// }

	public copyFile(options : CopyFileOptions) {
		nativeFileManager.copyFile(options.srcPath,
			options.destPath,
			function (res) {
				var ret = new FileManagerSuccessResult({ "errMsg": "copyFile:ok" });
				options.success?.(ret)
				options.complete?.(ret)
			},
			function (code) {
				let err = new FileSystemManagerFailImpl(code);
				options.fail?.(err)
				options.complete?.(err)
			})
	}

	// public renameSync(oldPath:string,newPath:string):number{
	// 	return 0
	// }

	public rename(options : RenameOptions) {
		nativeFileManager.rename(options.oldPath,
			options.newPath,
			function (res) {
				var ret = new FileManagerSuccessResult({ "errMsg": "rename:ok" });
				options.success?.(ret)
				options.complete?.(ret)
			},
			function (code) {
				let err = new FileSystemManagerFailImpl(code);
				options.fail?.(err)
				options.complete?.(err)
			})
	}

	// public accessSync(path: string):number{
	// 	return 0
	// }

	public access(options : AccessOptions) {
		nativeFileManager.access(options.path,
			function (res) {
				var ret = new FileManagerSuccessResult({ "errMsg": "access:ok" });
				options.success?.(ret)
				options.complete?.(ret)
			},
			function (code) {
				let err = new FileSystemManagerFailImpl(code);
				options.fail?.(err)
				options.complete?.(err)
			})
	}

	// readdirSync(dirPath: string): string[]|null{
	// 	return null
	// }

	public readdir(options : ReadDirOptions) {
		nativeFileManager.readdir(options.dirPath,
			function (res) {
				var ret = new ReadDirSuccessResult(res);
				options.success?.(ret)
				options.complete?.(ret)
			},
			function (code) {
				let err = new FileSystemManagerFailImpl(code);
				options.fail?.(err)
				options.complete?.(err)
			})
	}

	// public rmdirSync(dirPath: string,recursive:boolean): number {
	// 	return 0
	// }

	// public mkdirSync(dirPath: string,recursive:boolean): number {
	// 	return 0
	// }

	public rmdir(options : RmDirOptions) {
		nativeFileManager.rmdir(options.dirPath,
			options.recursive,
			function (res) {
				var ret = new FileManagerSuccessResult({ "errMsg": "rmdir:ok" });
				options.success?.(ret)
				options.complete?.(ret)
			},
			function (code) {
				let err = new FileSystemManagerFailImpl(code);
				options.fail?.(err)
				options.complete?.(err)
			})
	}

	public mkdir(options : MkDirOptions) {
		nativeFileManager.mkdir(options.dirPath,
			options.recursive,
			function (res) {
				var ret = new FileManagerSuccessResult({ "errMsg": "mkdir:ok" });
				options.success?.(ret)
				options.complete?.(ret)
			},
			function (code) {
				let err = new FileSystemManagerFailImpl(code);
				options.fail?.(err)
				options.complete?.(err)
			})
	}

	// unlinkSync(filePath: string): number {
	// 	return 0
	// }

	public unlink(options : UnLinkOptions) {
		nativeFileManager.unlink(
			options.filePath,
			function (res) {
				var ret = new FileManagerSuccessResult({ "errMsg": "unlink:ok" });
				options.success?.(ret)
				options.complete?.(ret)
			},
			function (code) {
				let err = new FileSystemManagerFailImpl(code);
				options.fail?.(err)
				options.complete?.(err)
			})
	}

	// public readFileSync(filePath: string,encoding:string): string|null{
	// }

	public readFile(options : ReadFileOptions) {
		nativeFileManager.readFile(options.encoding,
			options.filePath,
			function (res) {
				var ret = new ReadFileSuccessResult(res);
				options.success?.(ret)
				options.complete?.(ret)
			},
			function (code) {
				let err = new FileSystemManagerFailImpl(code);
				options.fail?.(err)
				options.complete?.(err)
			})
	}

	// public writeFileSync(filePath: string,data:string): number{
	// 	return 0
	// }

	public writeFile(options : WriteFileOptions) {
		nativeFileManager.writeFile(options.filePath,
			options.encoding,
			options.data,
			function () {
				var ret = new FileManagerSuccessResult({ "errMsg": "writeFile:ok" });
				options.success?.(ret)
				options.complete?.(ret)
			},
			function (code) {
				let err = new FileSystemManagerFailImpl(code);
				options.fail?.(err)
				options.complete?.(err)
			})
	}
}

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