From 1144472f06fa062a5d602f6720f0be4aecb83df6 Mon Sep 17 00:00:00 2001 From: lvchao Date: Thu, 21 Sep 2023 21:04:24 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix:=E8=A7=A3=E5=86=B3=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E8=B7=AF=E5=BE=84=E4=B8=8A#=E4=B8=8D=E8=83=BD=E7=BC=96?= =?UTF-8?q?=E8=AF=91=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lvchao --- .../FileExtensionAbility.ts | 35 ++++++++++++++----- .../FileExtensionAbility/ListScanFileInfo.ts | 5 ++- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/FileExtensionAbility.ts b/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/FileExtensionAbility.ts index 83671a90..fd9fb099 100644 --- a/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/FileExtensionAbility.ts +++ b/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/FileExtensionAbility.ts @@ -82,11 +82,14 @@ export default class FileExtAbility extends Extension { hilog.info(DOMAIN_CODE, TAG, 'Extension init process'); } - encode(uri): string { + encode(uri): string { try { - uri = encodeURI(uri); + let suffixUri = uri.slice(12,uri.length - 1); + let prefixUri = uri.slice(0,12) + uri = encodeURIComponent(suffixUri); + uri = prefixUri.concat(uri) } catch (e) { - hilog.error(DOMAIN_CODE, TAG, 'The reason of encodeURI: ' + e.message + ' code: ' + e.code); + hilog.error(DOMAIN_CODE, TAG, 'The reason of encodeURIComponent: ' + e.message + ' code: ' + e.code); uri = ''; } return uri; @@ -232,7 +235,10 @@ export default class FileExtAbility extends Extension { let path = getPath(newFileUri); let file = fs.openSync(path, fs.OpenMode.CREATE); fs.closeSync(file); - newFileUri = encodeURI(newFileUri); + let suffixUri = newFileUri.slice(12,newFileUri.length - 1); + let prefixUri = newFileUri.slice(0,12) + newFileUri = encodeURIComponent(suffixUri); + newFileUri = prefixUri.concat(newFileUri) return { uri: newFileUri, code: ERR_OK, @@ -264,7 +270,10 @@ export default class FileExtAbility extends Extension { let newFileUri = this.genNewFileUri(parentUri, displayName); let path = getPath(newFileUri); fs.mkdirSync(path); - newFileUri = encodeURI(newFileUri); + let suffixUri = newFileUri.slice(12,newFileUri.length - 1); + let prefixUri = newFileUri.slice(0,12) + newFileUri = encodeURIComponent(suffixUri); + newFileUri = prefixUri.concat(newFileUri) return { uri: newFileUri, code: ERR_OK, @@ -495,7 +504,10 @@ export default class FileExtAbility extends Extension { }; } fs.renameSync(oldPath, newPath); - newFileUri = encodeURI(newFileUri); + let suffixUri = newFileUri.slice(12,newFileUri.length - 1); + let prefixUri = newFileUri.slice(0,12) + newFileUri = encodeURIComponent(suffixUri); + newFileUri = prefixUri.concat(newFileUri) return { uri: newFileUri, code: ERR_OK, @@ -756,7 +768,10 @@ export default class FileExtAbility extends Extension { } else { mode |= documentFlag.REPRESENTS_FILE; } - selectFileUri = encodeURI(selectFileUri); + let suffixUri = selectFileUri.slice(12,selectFileUri.length - 1); + let prefixUri = selectFileUri.slice(0,12) + selectFileUri = encodeURIComponent(suffixUri); + selectFileUri = prefixUri.concat(selectFileUri) fileInfo = { uri: selectFileUri, relativePath: path, @@ -819,7 +834,11 @@ export default class FileExtAbility extends Extension { } else { mode |= documentFlag.REPRESENTS_FILE; } - let selectFileUri = encodeURI(this.relativePath2uri(selectFileRelativePath)); + let selectFileUri = this.relativePath2uri(selectFileRelativePath) + let suffixUri = selectFileUri.slice(12,selectFileUri.length - 1); + let prefixUri = selectFileUri.slice(0,12) + selectFileUri = encodeURIComponent(suffixUri); + selectFileUri = prefixUri.concat(selectFileUri) fileInfo = { uri: selectFileUri, relativePath: selectFileRelativePath, diff --git a/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/ListScanFileInfo.ts b/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/ListScanFileInfo.ts index 26672add..bee53147 100644 --- a/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/ListScanFileInfo.ts +++ b/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/ListScanFileInfo.ts @@ -165,7 +165,10 @@ function getFileInfos(sourceFileUri: string, offset: number, count: number, filt mode |= documentFlag.REPRESENTS_FILE; } let newFileUri = getNewPathOrUri(sourceFileUri, fileNameList[i]); - newFileUri = encodeURI(newFileUri); + let suffixUri = newFileUri.slice(12,newFileUri.length - 1); + let prefixUri = newFileUri.slice(0,12) + newFileUri = encodeURIComponent(suffixUri); + newFileUri = prefixUri.concat(newFileUri) infos.push({ uri: newFileUri, relativePath: filePath, -- Gitee From 69d02f66f6bd7ae4fc16930acfe8e5c3ff365701 Mon Sep 17 00:00:00 2001 From: lvchao Date: Fri, 22 Sep 2023 15:54:28 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix:=E8=B7=AF=E5=BE=84#=E4=B8=8D=E8=83=BD?= =?UTF-8?q?=E8=A2=AB=E8=AF=86=E5=88=AB=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lvchao --- .../main/ets/FileExtensionAbility/Common.ts | 27 +++++- .../FileExtensionAbility.ts | 83 +++++-------------- .../FileExtensionAbility/ListScanFileInfo.ts | 7 +- 3 files changed, 50 insertions(+), 67 deletions(-) diff --git a/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/Common.ts b/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/Common.ts index 15152594..359630f9 100644 --- a/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/Common.ts +++ b/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/Common.ts @@ -16,6 +16,7 @@ import hilog from '@ohos.hilog'; const BUNDLE_NAME = 'docs'; const DOMAIN_CODE = 0x0001; +const SLICE_PREFIX_URI = 12; const TAG = 'ExternalFileManager'; const FILE_PREFIX_NAME = 'file://'; @@ -57,6 +58,28 @@ function getPath(uri): string { return path; } +function encodePathOfUri(uri): string { + try { + let suffixUri = uri.slice(SLICE_PREFIX_URI, uri.length); + let prefixUri = uri.slice(0, SLICE_PREFIX_URI); + uri = prefixUri.concat(encodeURIComponent(suffixUri).replace(/%2F/g, '/')); + } catch (e) { + hilog.error(DOMAIN_CODE, TAG, 'The reason of encodeURIComponent: ' + e.message + ' code: ' + e.code); + uri = ''; + } + return uri; +} + +function decodeUri(uri): string { + try { + uri = decodeURIComponent(uri); + } catch (e) { + hilog.error(DOMAIN_CODE, TAG, 'The reason of decodeURIComponent: ' + e.message + ' code: ' + e.code); + uri = ''; + } + return uri; +} + interface Fileinfo { uri: string, relativePath: string, @@ -67,5 +90,7 @@ interface Fileinfo { mimeType: string } -export { getPath, checkUri, BUNDLE_NAME, DOMAIN_CODE, FILE_PREFIX_NAME, TAG }; + + +export { getPath, checkUri, encodePathOfUri, decodeUri, BUNDLE_NAME, DOMAIN_CODE, FILE_PREFIX_NAME, TAG }; export type { Fileinfo }; \ No newline at end of file diff --git a/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/FileExtensionAbility.ts b/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/FileExtensionAbility.ts index fd9fb099..fc0e0784 100644 --- a/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/FileExtensionAbility.ts +++ b/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/FileExtensionAbility.ts @@ -20,7 +20,7 @@ import fileExtensionInfo from '@ohos.file.fileExtensionInfo'; import hilog from '@ohos.hilog'; import { getFileInfos } from './ListScanFileInfo'; import type { Fileinfo } from './Common'; -import { getPath, checkUri, BUNDLE_NAME, DOMAIN_CODE, FILE_PREFIX_NAME, TAG } from './Common'; +import { getPath, checkUri, encodePathOfUri, decodeUri, BUNDLE_NAME, DOMAIN_CODE, FILE_PREFIX_NAME, TAG } from './Common'; const deviceFlag = fileExtensionInfo.DeviceFlag; const documentFlag = fileExtensionInfo.DocumentFlag; const deviceType = fileExtensionInfo.DeviceType; @@ -82,29 +82,6 @@ export default class FileExtAbility extends Extension { hilog.info(DOMAIN_CODE, TAG, 'Extension init process'); } - encode(uri): string { - try { - let suffixUri = uri.slice(12,uri.length - 1); - let prefixUri = uri.slice(0,12) - uri = encodeURIComponent(suffixUri); - uri = prefixUri.concat(uri) - } catch (e) { - hilog.error(DOMAIN_CODE, TAG, 'The reason of encodeURIComponent: ' + e.message + ' code: ' + e.code); - uri = ''; - } - return uri; - } - - decode(uri): string { - try { - uri = decodeURI(uri); - } catch (e) { - hilog.error(DOMAIN_CODE, TAG, 'The reason of decodeURI: ' + e.message + ' code: ' + e.code); - uri = ''; - } - return uri; - } - genNewFileUri(uri, displayName) { let newFileUri = uri; if (uri.charAt(uri.length - 1) === '/') { @@ -180,7 +157,7 @@ export default class FileExtAbility extends Extension { } openFile(sourceFileUri, flags): {number, number} { - sourceFileUri = this.decode(sourceFileUri); + sourceFileUri = decodeUri(sourceFileUri); if (sourceFileUri === '') { return { fd: ERR_ERROR, @@ -210,7 +187,7 @@ export default class FileExtAbility extends Extension { } createFile(parentUri, displayName): {string, number} { - parentUri = this.decode(parentUri); + parentUri = decodeUri(parentUri); if (parentUri === '') { return { uri: '', @@ -235,10 +212,7 @@ export default class FileExtAbility extends Extension { let path = getPath(newFileUri); let file = fs.openSync(path, fs.OpenMode.CREATE); fs.closeSync(file); - let suffixUri = newFileUri.slice(12,newFileUri.length - 1); - let prefixUri = newFileUri.slice(0,12) - newFileUri = encodeURIComponent(suffixUri); - newFileUri = prefixUri.concat(newFileUri) + newFileUri = encodePathOfUri(newFileUri); return { uri: newFileUri, code: ERR_OK, @@ -253,7 +227,7 @@ export default class FileExtAbility extends Extension { } mkdir(parentUri, displayName): {string, number} { - parentUri = this.decode(parentUri); + parentUri = decodeUri(parentUri); if (parentUri === '') { return { uri: '', @@ -270,10 +244,7 @@ export default class FileExtAbility extends Extension { let newFileUri = this.genNewFileUri(parentUri, displayName); let path = getPath(newFileUri); fs.mkdirSync(path); - let suffixUri = newFileUri.slice(12,newFileUri.length - 1); - let prefixUri = newFileUri.slice(0,12) - newFileUri = encodeURIComponent(suffixUri); - newFileUri = prefixUri.concat(newFileUri) + newFileUri = encodePathOfUri(newFileUri); return { uri: newFileUri, code: ERR_OK, @@ -356,7 +327,7 @@ export default class FileExtAbility extends Extension { } delete(selectFileUri): number { - selectFileUri = this.decode(selectFileUri); + selectFileUri = decodeUri(selectFileUri); if (selectFileUri === '') { return E_URIS; } @@ -386,8 +357,8 @@ export default class FileExtAbility extends Extension { } move(sourceFileUri, targetParentUri): {string, number} { - sourceFileUri = this.decode(sourceFileUri); - targetParentUri = this.decode(targetParentUri); + sourceFileUri = decodeUri(sourceFileUri); + targetParentUri = decodeUri(targetParentUri); if (sourceFileUri === '' || targetParentUri === '') { return { uri: '', @@ -404,7 +375,7 @@ export default class FileExtAbility extends Extension { let newFileUri = this.genNewFileUri(targetParentUri, displayName); let oldPath = getPath(sourceFileUri); let newPath = getPath(newFileUri); - newFileUri = this.encode(newFileUri); + newFileUri = encodePathOfUri(newFileUri); if (newFileUri === '') { return { uri: '', @@ -478,7 +449,7 @@ export default class FileExtAbility extends Extension { } rename(sourceFileUri, displayName): {string, number} { - sourceFileUri = this.decode(sourceFileUri); + sourceFileUri = decodeUri(sourceFileUri); if (sourceFileUri === '') { return { uri: '', @@ -504,10 +475,7 @@ export default class FileExtAbility extends Extension { }; } fs.renameSync(oldPath, newPath); - let suffixUri = newFileUri.slice(12,newFileUri.length - 1); - let prefixUri = newFileUri.slice(0,12) - newFileUri = encodeURIComponent(suffixUri); - newFileUri = prefixUri.concat(newFileUri) + newFileUri = encodePathOfUri(newFileUri); return { uri: newFileUri, code: ERR_OK, @@ -666,7 +634,7 @@ export default class FileExtAbility extends Extension { } access(sourceFileUri): {boolean, number} { - sourceFileUri = this.decode(sourceFileUri); + sourceFileUri = decodeUri(sourceFileUri); if (sourceFileUri === '') { return { uri: '', @@ -706,7 +674,7 @@ export default class FileExtAbility extends Extension { listFile(sourceFileUri: string, offset: number, count: number, filter: Filter) : {infos: Fileinfo[], code: number} { let infos : Fileinfo[] = []; - sourceFileUri = this.decode(sourceFileUri); + sourceFileUri = decodeUri(sourceFileUri); if (sourceFileUri === '') { return { infos: [], @@ -725,7 +693,7 @@ export default class FileExtAbility extends Extension { scanFile(sourceFileUri: string, offset: number, count: number, filter: Filter) : {infos: Fileinfo[], code: number} { let infos : Fileinfo[] = []; - sourceFileUri = this.decode(sourceFileUri); + sourceFileUri = decodeUri(sourceFileUri); if (sourceFileUri === '') { return { infos: [], @@ -743,7 +711,7 @@ export default class FileExtAbility extends Extension { } getFileInfoFromUri(selectFileUri) { - selectFileUri = this.decode(selectFileUri); + selectFileUri = decodeUri(selectFileUri); if (selectFileUri === '') { return { uri: '', @@ -768,10 +736,7 @@ export default class FileExtAbility extends Extension { } else { mode |= documentFlag.REPRESENTS_FILE; } - let suffixUri = selectFileUri.slice(12,selectFileUri.length - 1); - let prefixUri = selectFileUri.slice(0,12) - selectFileUri = encodeURIComponent(suffixUri); - selectFileUri = prefixUri.concat(selectFileUri) + selectFileUri = encodePathOfUri(selectFileUri); fileInfo = { uri: selectFileUri, relativePath: path, @@ -834,11 +799,7 @@ export default class FileExtAbility extends Extension { } else { mode |= documentFlag.REPRESENTS_FILE; } - let selectFileUri = this.relativePath2uri(selectFileRelativePath) - let suffixUri = selectFileUri.slice(12,selectFileUri.length - 1); - let prefixUri = selectFileUri.slice(0,12) - selectFileUri = encodeURIComponent(suffixUri); - selectFileUri = prefixUri.concat(selectFileUri) + let selectFileUri = encodePathOfUri(this.relativePath2uri(selectFileRelativePath)); fileInfo = { uri: selectFileUri, relativePath: selectFileRelativePath, @@ -942,7 +903,7 @@ export default class FileExtAbility extends Extension { } query(uri, columns): {[], number} { - uri = this.decode(uri); + uri = decodeUri(uri); if (uri === '') { return { uri: '', @@ -990,7 +951,7 @@ export default class FileExtAbility extends Extension { } startWatcher(uri, callback): number { - uri = this.decode(uri); + uri = decodeUri(uri); if (uri === '') { return { code: E_URIS, @@ -1012,7 +973,7 @@ export default class FileExtAbility extends Extension { } }); let targetUri = FILE_PREFIX_NAME + BUNDLE_NAME + data.fileName; - targetUri = this.encode(targetUri); + targetUri = encodePathOfUri(targetUri); if (eventCode >= 0) { callback(targetUri, eventCode); } else { @@ -1037,7 +998,7 @@ export default class FileExtAbility extends Extension { } stopWatcher(uri, isUnregisterAll): number { - uri = this.decode(uri); + uri = decodeUri(uri); if (uri === '') { return { code: E_URIS, diff --git a/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/ListScanFileInfo.ts b/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/ListScanFileInfo.ts index bee53147..63528820 100644 --- a/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/ListScanFileInfo.ts +++ b/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/ListScanFileInfo.ts @@ -17,7 +17,7 @@ import fs from '@ohos.file.fs'; import type { Filter } from '@ohos.file.fs'; import fileExtensionInfo from '@ohos.file.fileExtensionInfo'; import type { Fileinfo } from './Common'; -import { getPath, DOMAIN_CODE, TAG } from './Common'; +import { getPath, encodePathOfUri, DOMAIN_CODE, TAG } from './Common'; const documentFlag = fileExtensionInfo.DocumentFlag; const ERR_OK = 0; @@ -165,10 +165,7 @@ function getFileInfos(sourceFileUri: string, offset: number, count: number, filt mode |= documentFlag.REPRESENTS_FILE; } let newFileUri = getNewPathOrUri(sourceFileUri, fileNameList[i]); - let suffixUri = newFileUri.slice(12,newFileUri.length - 1); - let prefixUri = newFileUri.slice(0,12) - newFileUri = encodeURIComponent(suffixUri); - newFileUri = prefixUri.concat(newFileUri) + newFileUri = encodePathOfUri(newFileUri); infos.push({ uri: newFileUri, relativePath: filePath, -- Gitee