1/* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import { HiLog } from '../../common/HiLog'; 17import { defaultAppInfo, defaultDlpFile, getAuthPerm } from '../../common/FileUtils/utils'; 18import Constants from '../../common/constant'; 19import { dlpPermission } from '@kit.DataProtectionKit'; 20import Result from '../../common/Result'; 21import { ResultMsg } from '../../common/ResultMsg'; 22import OpenDlpFileData from './OpenDlpFileData'; 23import fs from '@ohos.file.fs'; 24import { fileUri } from '@kit.CoreFileKit'; 25import FileMetaInfo from '../../bean/data/FileMetaInfo'; 26import FileUtils from '../../common/FileUtils/FileUtils'; 27 28const TAG: string = 'DecryptContent'; 29 30export default class DecryptContent { 31 private _fileMetaInfo: FileMetaInfo; 32 private _openDlpFileData: OpenDlpFileData; 33 private _dlpFd: number = -1; 34 private _dlpFile: dlpPermission.DLPFile = defaultDlpFile; 35 private _appInfo: dlpPermission.DLPSandboxInfo = defaultAppInfo; 36 private _distributedInfoId: string = ''; 37 private _linkUri: string = ''; 38 private _linkFileName: string = ''; 39 private _authPerm: dlpPermission.DLPFileAccess = dlpPermission.DLPFileAccess.READ_ONLY; 40 private _fileName: string = ''; 41 private _userId: number = -1; 42 private _accountName: string = ''; 43 private _linkUriStat?: fs.Stat | undefined; 44 private _linkFileWriteable: boolean = false; 45 private _uriInfo: fileUri.FileUri = new fileUri.FileUri(''); 46 private _hasDecrypted: boolean = false; 47 48 constructor(fileMetaInfo: FileMetaInfo, openDlpFileData: OpenDlpFileData) { 49 this._fileMetaInfo = fileMetaInfo; 50 this._openDlpFileData = openDlpFileData; 51 let strArray: string[] = this._openDlpFileData.uri.split('/'); 52 this._fileName = strArray[strArray.length - 1]; 53 } 54 55 // DecryptContent里添加成员变量,一定要在clone()里添加复制操作 56 public clone(): DecryptContent { 57 const clone = new DecryptContent(this._fileMetaInfo, this._openDlpFileData); 58 clone.dlpFd = this._dlpFd; 59 clone.dlpFile = this._dlpFile; 60 clone.appInfo = this._appInfo; 61 clone.distributedInfoId = this._distributedInfoId; 62 clone.linkUri = this._linkUri; 63 clone.linkFileName = this._linkFileName; 64 clone.authPerm = this._authPerm; 65 clone.fileName = this._fileName; 66 clone.userId = this._userId; 67 clone.accountName = this._accountName; 68 clone.linkUriStat = this._linkUriStat; 69 clone.linkFileWriteable = this._linkFileWriteable; 70 clone.uriInfo = this._uriInfo; 71 clone.hasDecrypted = this._hasDecrypted; 72 return clone; 73 } 74 75 public set fileMetaInfo(value: FileMetaInfo) { 76 this._fileMetaInfo = value; 77 } 78 79 public get fileMetaInfo(): FileMetaInfo { 80 return this._fileMetaInfo; 81 } 82 83 public set openDlpFileData(value: OpenDlpFileData) { 84 this._openDlpFileData = value; 85 } 86 87 public get openDlpFileData(): OpenDlpFileData { 88 return this._openDlpFileData; 89 } 90 91 public set dlpFd(value: number) { 92 this._dlpFd = value; 93 } 94 95 public get dlpFd(): number { 96 return this._dlpFd; 97 } 98 99 public set dlpFile(value: dlpPermission.DLPFile) { 100 this._dlpFile = value; 101 } 102 103 public get dlpFile(): dlpPermission.DLPFile { 104 return this._dlpFile; 105 } 106 107 public set appInfo(value: dlpPermission.DLPSandboxInfo) { 108 this._appInfo = value; 109 } 110 111 public get appInfo(): dlpPermission.DLPSandboxInfo { 112 return this._appInfo; 113 } 114 115 public set distributedInfoId(value: string) { 116 this._distributedInfoId = value; 117 } 118 119 public get distributedInfoId(): string { 120 return this._distributedInfoId; 121 } 122 123 public set linkUri(value: string) { 124 this._linkUri = value; 125 } 126 127 public get linkUri(): string { 128 return this._linkUri; 129 } 130 131 public set linkFileName(value: string) { 132 this._linkFileName = value; 133 } 134 135 public get linkFileName(): string { 136 return this._linkFileName; 137 } 138 139 public set authPerm(value: dlpPermission.DLPFileAccess) { 140 this._authPerm = value; 141 } 142 143 public get authPerm(): dlpPermission.DLPFileAccess { 144 return this._authPerm; 145 } 146 147 public set fileName(value: string) { 148 this._fileName = value; 149 } 150 151 public get fileName(): string { 152 return this._fileName; 153 } 154 155 public set userId(value: number) { 156 this._userId = value; 157 } 158 159 public get userId(): number { 160 return this._userId; 161 } 162 163 public set accountName(value: string) { 164 this._accountName = value; 165 } 166 167 public get accountName(): string { 168 return this._accountName; 169 } 170 171 public get linkUriStat(): fs.Stat | undefined { 172 return this._linkUriStat; 173 } 174 175 public set linkUriStat(value: fs.Stat | undefined) { 176 this._linkUriStat = value; 177 } 178 179 public set linkFileWriteable(value: boolean) { 180 this._linkFileWriteable = value; 181 } 182 183 public get linkFileWriteable(): boolean { 184 return this._linkFileWriteable; 185 } 186 187 public set uriInfo(value: fileUri.FileUri) { 188 this._uriInfo = value; 189 } 190 191 public get uriInfo(): fileUri.FileUri { 192 return this._uriInfo; 193 } 194 195 public set hasDecrypted(value: boolean) { 196 this._hasDecrypted = value; 197 } 198 199 public get hasDecrypted(): boolean { 200 return this._hasDecrypted; 201 } 202 203 public generateLinkFileName(): Result<string> { 204 let timestamp = new Date().getTime(); 205 let splitNames = this._fileName.split('.'); 206 207 HiLog.debug(TAG, `splitNames: ${splitNames}`); 208 if (splitNames.length < Constants.NUMBER_TWO) { 209 return ResultMsg.getErrMsg(Constants.ERR_JS_APP_INSIDE_ERROR); 210 } 211 212 let secondarySuffix = (this._fileMetaInfo.fileType === '') ? 213 splitNames[splitNames.length - Constants.NUMBER_TWO] : this._fileMetaInfo.fileType; 214 this._linkFileName = String(this._openDlpFileData.sandboxBundleName).substring(0, Constants.BUNDLE_LEN) + '_' + 215 this._appInfo.appIndex + '_' + timestamp + 216 String(Math.random()).substring(Constants.RAND_START, Constants.RAND_END) + '.dlp.link.' + secondarySuffix; 217 return ResultMsg.buildSuccess(this._linkFileName); 218 } 219 220 public generateLinkUri(): Result<string> { 221 const linkFilePath = Constants.FUSE_PATH + this._linkFileName; 222 try { 223 const stat = fs.statSync(linkFilePath); 224 const WRITE_ACCESS: number = 0o0200; 225 if (stat.mode & WRITE_ACCESS) { 226 this._linkFileWriteable = true; 227 } else { 228 this._linkFileWriteable = false; 229 } 230 this._linkUri = fileUri.getUriFromPath(linkFilePath); 231 this._uriInfo = new fileUri.FileUri(this._openDlpFileData.uri); 232 this._linkUriStat = fs.statSync(this._uriInfo.path); 233 return ResultMsg.buildSuccess(); 234 } catch (error) { 235 HiLog.wrapError(TAG, error, `open: ${FileUtils.getFileNameByUri(this._openDlpFileData.uri)} failed`); 236 return ResultMsg.getErrMsg(Constants.ERR_JS_APP_GET_FILE_ASSET_ERROR); 237 } 238 } 239 240 public setDlpGetAuthPerm(): Result<void> { 241 if (!this._openDlpFileData.isFromPlugin && 242 this._fileMetaInfo.accountType === dlpPermission.AccountType.DOMAIN_ACCOUNT) { 243 this._authPerm = getAuthPerm(this._accountName ?? '', this._dlpFile.dlpProperty); 244 } else { 245 this._authPerm = dlpPermission.DLPFileAccess.READ_ONLY; 246 } 247 if (this._authPerm < dlpPermission.DLPFileAccess.READ_ONLY || 248 this._authPerm > dlpPermission.DLPFileAccess.FULL_CONTROL) { 249 HiLog.error(TAG, 'get authPerm error'); 250 return ResultMsg.getErrMsg(Constants.ERR_CODE_OPEN_FILE_ERROR); 251 } 252 return ResultMsg.buildSuccess(); 253 } 254}