1/* 2 * Copyright (c) 2023 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 '@ohos.hilog'; 17const BUNDLE_NAME = 'docs'; 18const DOMAIN_CODE = 0x0001; 19const SLICE_PREFIX_URI = 12; 20const TAG = 'ExternalFileManager'; 21const FILE_PREFIX_NAME = 'file://'; 22 23function checkUri(uri: string): boolean { 24 try { 25 if (uri?.indexOf(FILE_PREFIX_NAME) === 0) { 26 hilog.info(DOMAIN_CODE, TAG, 'uri is ' + uri); 27 return true; 28 } else { 29 hilog.error(DOMAIN_CODE, TAG, 'checkUri error, uri is ' + uri); 30 return false; 31 } 32 } catch (error) { 33 hilog.error(DOMAIN_CODE, TAG, 'checkUri error, uri is ' + uri); 34 return false; 35 } 36} 37 38function getPath(uri): string { 39 let sep = '://'; 40 let arr = uri.split(sep); 41 let minLength = 2; 42 if (arr.length < minLength) { 43 hilog.error(DOMAIN_CODE, TAG, 'getPath-parameter-uri format exception, uri is' + uri); 44 return ''; 45 } 46 let path = uri.replace(arr[0] + sep, ''); 47 if (arr[1].indexOf('/') > 0 && arr[1].split('/')[0] === BUNDLE_NAME) { 48 path = path.replace(arr[1].split('/')[0], ''); 49 } else { 50 hilog.error(DOMAIN_CODE, TAG, 'getPath-parameter-uri format exception, uri is ' + uri); 51 return ''; 52 } 53 54 if (path.charAt(path.length - 1) === '/') { 55 path = path.substr(0, path.length - 1); 56 } 57 hilog.info(DOMAIN_CODE, TAG, 'getPath after ' + path); 58 return path; 59} 60 61interface Fileinfo { 62 uri: string, 63 relativePath: string, 64 fileName: string, 65 mode: number, 66 size: number, 67 mtime: number, 68 mimeType: string 69} 70 71function uriReturnObject(uri: string, code: number) { 72 return { 73 uri: uri, 74 code: code 75 }; 76} 77 78function infosReturnObject(infos: Fileinfo[], code: number): {infos: Fileinfo[], code: number} { 79 return { 80 infos: infos, 81 code: code 82 }; 83} 84 85function fdReturnObject(fd: number, code: number): {fd: number, code: number} { 86 return { 87 fd: fd, 88 code: code 89 }; 90} 91 92function boolReturnObject(isExist: boolean, code: number): {isExist: boolean, code: number} { 93 return { 94 isExist: isExist, 95 code: code 96 }; 97} 98 99function fileinfoReturnObject(fileInfo: object, code: number): {fileInfo: object, code: number} { 100 return { 101 fileInfo: fileInfo, 102 code: code 103 }; 104} 105 106function resultsResultObject(results: object, code: number): {results: object, code: number} { 107 return { 108 results: results, 109 code: code 110 }; 111} 112 113function rootsReturnObject(roots: object, code: number): {roots: object, code: number} { 114 return { 115 roots: roots, 116 code: code 117 }; 118} 119 120function encodePathOfUri(uri): string { 121 try { 122 let suffixUri = uri.slice(SLICE_PREFIX_URI, uri.length); 123 let prefixUri = uri.slice(0, SLICE_PREFIX_URI); 124 uri = prefixUri.concat(encodeURIComponent(suffixUri).replace(/%2F/g, '/')); 125 } catch (e) { 126 hilog.error(DOMAIN_CODE, TAG, 'The reason of encodeURIComponent: ' + e.message + ' code: ' + e.code); 127 uri = ''; 128 } 129 return uri; 130} 131 132function decodeUri(uri): string { 133 try { 134 uri = decodeURIComponent(uri); 135 } catch (e) { 136 hilog.error(DOMAIN_CODE, TAG, 'The reason of decodeURIComponent: ' + e.message + ' code: ' + e.code); 137 uri = ''; 138 } 139 return uri; 140} 141 142export { 143 getPath, checkUri, encodePathOfUri, decodeUri, uriReturnObject, infosReturnObject, fdReturnObject, boolReturnObject, resultsResultObject, 144 fileinfoReturnObject, rootsReturnObject, BUNDLE_NAME, DOMAIN_CODE, FILE_PREFIX_NAME, TAG 145}; 146 147export type { Fileinfo };