feat: usb日志、轨迹拷入
This commit is contained in:
		
							parent
							
								
									2590117346
								
							
						
					
					
						commit
						4c81726cfe
					
				
							
								
								
									
										91
									
								
								entry/src/main/ets/common/utils/UsbUtils.ets
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										91
									
								
								entry/src/main/ets/common/utils/UsbUtils.ets
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,91 @@ | ||||
| import common from '@ohos.app.ability.common' | ||||
| import fs from '@ohos.file.fs'; | ||||
| import zlib from '@ohos.zlib'; | ||||
| 
 | ||||
| async function compressFileToCache(input: string, output: string, context: common.UIAbilityContext): Promise<string> { | ||||
|   if (!fs.accessSync(input)) { | ||||
|     return Promise.reject("no such file or directory") | ||||
|   } | ||||
|   try { | ||||
|     const tempDir = context.cacheDir + '/' + output | ||||
|     if (fs.statSync(input).isDirectory()) { | ||||
|       if (!fs.accessSync(tempDir)) { | ||||
|         fs.mkdirSync(tempDir) | ||||
|       } | ||||
|       fs.listFileSync(input).forEach(name => { | ||||
|         console.log(name) | ||||
|         fs.copyFileSync(input + '/' + name, tempDir + '/' + name) | ||||
|       }) | ||||
|       await zlib.compressFile(tempDir, tempDir + '.zip', { | ||||
|         level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION, | ||||
|         memLevel: zlib.MemLevel.MEM_LEVEL_DEFAULT, | ||||
|         strategy: zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY | ||||
|       }) | ||||
|       fs.rmdirSync(context.cacheDir + '/' + output) | ||||
|       return Promise.resolve(tempDir + '.zip') | ||||
|     } else { | ||||
|       const tempDir = context.cacheDir + '/' + output | ||||
|       fs.copyFileSync(input, tempDir) | ||||
|       await zlib.compressFile(tempDir, tempDir + '.zip', { | ||||
|         level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION, | ||||
|         memLevel: zlib.MemLevel.MEM_LEVEL_DEFAULT, | ||||
|         strategy: zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY | ||||
|       }) | ||||
|       fs.unlinkSync(context.cacheDir + '/' + output) | ||||
|       return Promise.resolve(tempDir + '.zip') | ||||
|     } | ||||
|   } catch (e) { | ||||
|     console.log("UsbUtils", JSON.stringify(e)) | ||||
|     return Promise.reject(e) | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| export class UsbUtils { | ||||
|   private context: common.UIAbilityContext | ||||
| 
 | ||||
|   constructor(context: common.UIAbilityContext) { | ||||
|     this.context = context | ||||
|   } | ||||
| 
 | ||||
|   getUsbDiskPath(): Promise<string> { | ||||
|     return new Promise((resolve, reject) => { | ||||
|       let path = "" | ||||
|       try { | ||||
|         path = fs.listFileSync("/mnt/data/external")[0] | ||||
|       } catch (error) { | ||||
|         console.log("err", JSON.stringify(error)) | ||||
|         reject(error) | ||||
|         return | ||||
|       } | ||||
|       resolve('/mnt/data/external' + '/' + path) | ||||
|     }) | ||||
|   } | ||||
| 
 | ||||
|   async copyTrackToUsb() { | ||||
|     let output = "" | ||||
|     let temp = "" | ||||
|     return this.getUsbDiskPath().then(path => { | ||||
|       output = path | ||||
|       return compressFileToCache('/data/log/duolun/logs', "track", this.context) | ||||
|     }).then((trackPath) => { | ||||
|       temp = trackPath | ||||
|       return fs.copyFile(trackPath, output + '/' + "track.zip") | ||||
|     }).then(() => { | ||||
|       return fs.unlink(temp) | ||||
|     }) | ||||
|   } | ||||
| 
 | ||||
|   async copyLogToUsb() { | ||||
|     let output = "" | ||||
|     let temp = "" | ||||
|     return this.getUsbDiskPath().then(path => { | ||||
|       output = path | ||||
|       return compressFileToCache('/data/log/hilog', "log", this.context) | ||||
|     }).then((trackPath) => { | ||||
|       temp = trackPath | ||||
|       return fs.copyFile(trackPath, output + '/' + "log.zip") | ||||
|     }).then(() => { | ||||
|       return fs.unlink(temp) | ||||
|     }) | ||||
|   } | ||||
| } | ||||
| @ -88,44 +88,18 @@ | ||||
|       }, | ||||
|       { | ||||
|         "name": "ohos.permission.WRITE_IMAGEVIDEO", | ||||
|         "usedScene": { | ||||
|           "abilities": [ | ||||
|             "EntryAbility" | ||||
|           ], | ||||
|           "when": "always" | ||||
|         } | ||||
|       }, | ||||
|       { | ||||
|         "name": "ohos.permission.READ_IMAGEVIDEO", | ||||
|         "usedScene": { | ||||
|           "abilities": [ | ||||
|             "EntryAbility" | ||||
|           ], | ||||
|           "when": "always" | ||||
|         } | ||||
|       }, | ||||
|       { | ||||
|         "name": "ohos.permission.FILE_ACCESS_MANAGER", | ||||
|         "reason": "$string:module_desc", | ||||
|         "usedScene": { | ||||
|           "abilities": [ | ||||
|             "MainAbility" | ||||
|           ], | ||||
|           "when": "always" | ||||
|         } | ||||
|       }, | ||||
|       { | ||||
|         "name": "ohos.permission.STORAGE_MANAGER" | ||||
|       }, | ||||
|       { | ||||
|         "name": "ohos.permission.GET_BUNDLE_INFO_PRIVILEGED", | ||||
|         "reason": "$string:module_desc", | ||||
|         "usedScene": { | ||||
|           "abilities": [ | ||||
|             "MainAbility" | ||||
|           ], | ||||
|           "when": "always" | ||||
|         } | ||||
|       }, | ||||
|     ] | ||||
|   } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user