From 56c165d95e55e8dc799b5effbb815eb9831046ea Mon Sep 17 00:00:00 2001 From: yang-jingbo1985 Date: Wed, 22 Nov 2023 15:22:57 +0800 Subject: [PATCH 01/12] =?UTF-8?q?=E5=AE=9E=E7=8E=B0list=20file=20=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E6=94=AF=E6=8C=81=20mimetype=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yang-jingbo1985 --- .../js/src/mod_fs/properties/listfile.cpp | 137 +++++++++++++++ .../kits/js/src/mod_fs/properties/listfile.h | 3 + .../src/mod_fs/properties/prop_n_exporter.cpp | 2 + utils/common/include/mimetype.h | 164 ++++++++++++++++++ 4 files changed, 306 insertions(+) create mode 100644 utils/common/include/mimetype.h diff --git a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp index e046e1745..97b2de03f 100755 --- a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp @@ -26,6 +26,8 @@ #include "file_utils.h" #include "filemgmt_libhilog.h" +#include "mimetype.h" + namespace OHOS::FileManagement::ModuleFileIO { using namespace std; using namespace OHOS::FileManagement::LibN; @@ -79,6 +81,21 @@ static bool GetFileFilterParam(const NVal &argv, FileFilter *filter) } filter->SetDisplayName(displayNames); } + + if (argv.HasProp("mimeType") && !argv.GetProp("mimeType").TypeIs(napi_undefined)) { + vector mimeTypes; + tie(ret, mimeTypes, ignore) = argv.GetProp("mimeType").ToStringArray(); + if (!ret) { + HILOGE("Failed to get mimeType prop."); + return false; + } + if (mimeTypes.size() == 0) { + HILOGE("Invalid mimeType."); + return false; + } + filter->SetMimeType(mimeTypes); + } + if (argv.HasProp("fileSizeOver") && !argv.GetProp("fileSizeOver").TypeIs(napi_undefined)) { int64_t fileSizeOver = 0; tie(ret, fileSizeOver) = argv.GetProp("fileSizeOver").ToInt64(); @@ -178,6 +195,30 @@ static bool FilterDisplayname(const vector &displaynames, const struct d return false; } +static bool FilterMimetype(const vector &mimetypes, const struct dirent &filename) +{ + if (filename.d_type == DT_DIR) { + return true; + } + + size_t found = string(filename.d_name).rfind('.'); + if (found == std::string::npos) { + return false; + } + HILOGE("filename.d_name: %{public}s", filename.d_name); + + string extension = string(filename.d_name).substr(found + 1); + string fileMimetype = MimeTypeUtil::GetMimeTypeFromExt(extension); + HILOGE("Default extension: %{public}s", extension.c_str()); + HILOGE("Default fileMimetype: %{public}s", fileMimetype.c_str()); + for (const auto& mimetypeItem : mimetypes) { + if (mimetypeItem == fileMimetype) { + return true; + } + } + return false; +} + static bool FilterFilesizeOver(const int64_t fFileSizeOver, const struct dirent &filename) { if (fFileSizeOver < 0) { @@ -224,6 +265,10 @@ static bool FilterResult(const struct dirent &filename) if (!FilterDisplayname(fDisplaynames, filename) && fDisplaynames.size() > 0) { return false; } + vector fMimetype = g_optionArgs.filter.GetMimeType(); + if (!FilterMimetype(fMimetype, filename) && fMimetype.size() > 0) { + return false; + } int64_t fFileSizeOver = g_optionArgs.filter.GetFileSizeOver(); if (!FilterFilesizeOver(fFileSizeOver, filename)) { return false; @@ -403,4 +448,96 @@ napi_value ListFile::Async(napi_env env, napi_callback_info info) return NAsyncWorkCallback(env, thisVar, cb).Schedule(LIST_FILE_PRODUCE_NAME, cbExec, cbCompl).val_; } } +napi_value ListFile::GetMimeType(napi_env env, napi_callback_info info) +{ + #if 0 + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + bool succ = false; + std::unique_ptr fileName; + std::tie(succ, fileName, std::ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!succ) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + std::string fileNameSuffix = fileName.get(); + size_t slashIndex = fileNameSuffix.rfind("."); + if (slashIndex != string::npos) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + std::string extension = fileNameSuffix.substr(slashIndex); + + #else + size_t argc = 1; + napi_value argv[1]; + napi_value thisVar; + void* data; + napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); + if (argc < 1) + { + napi_throw_type_error(env, nullptr, "Wrong number of arguments"); + return nullptr; + } + + size_t extensionLength = 0; + napi_get_value_string_utf8(env, argv[0], nullptr, 0, &extensionLength); + std::string extension(extensionLength, '\0'); + napi_get_value_string_utf8(env, argv[0], &extension[0], extensionLength + 1, nullptr); + extension = "." + extension; +#endif + string mimeType = MimeTypeUtil::GetMimeTypeFromExt(extension); + + napi_value result; + napi_create_string_utf8(env, mimeType.c_str(), mimeType.length(), &result); + + return result; +} + +napi_value ListFile::GetExtension(napi_env env, napi_callback_info info) +{ + napi_status status; + size_t argc = 1; + napi_value argv[1]; + status = napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr); + if (status != napi_ok || argc != 1) { + return nullptr; + } + + char extension[MAX_SUFFIX_LENGTH] = {0}; + size_t extensionSize = 0; + status = napi_get_value_string_utf8(env, argv[0], extension, sizeof(extension), &extensionSize); + if (status != napi_ok || extensionSize == 0) { + return nullptr; + } + + string extensionStr(extension, extensionSize); + vector mimeTypeVector = MimeTypeUtil::GetExtFromMimeType(extensionStr); + + string mimeType; + if (!mimeTypeVector.empty()) { + mimeType = ""; + for (const string& str : mimeTypeVector) { + mimeType += str; + } + } + string result = mimeType.substr(mimeType.find('/') + 1); + + napi_value extensionValue; + status = napi_create_string_utf8(env, result.c_str(), result.length(), &extensionValue); + if (status != napi_ok) { + return nullptr; + } + + return extensionValue; + +} + + + } // namespace OHOS::FileManagement::ModuleFileIO \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/listfile.h b/interfaces/kits/js/src/mod_fs/properties/listfile.h index d4dd936a0..bc772d784 100755 --- a/interfaces/kits/js/src/mod_fs/properties/listfile.h +++ b/interfaces/kits/js/src/mod_fs/properties/listfile.h @@ -27,6 +27,9 @@ class ListFile { public: static napi_value Sync(napi_env env, napi_callback_info info); static napi_value Async(napi_env env, napi_callback_info info); + static napi_value GetMimeType(napi_env env, napi_callback_info info); + static napi_value GetExtension(napi_env env, napi_callback_info info); + }; class ListFileArgs { diff --git a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp index 89e612164..30191872e 100644 --- a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp @@ -648,6 +648,8 @@ bool PropNExporter::Export() NVal::DeclareNapiFunction("fdopenStreamSync", FdopenStream::Sync), NVal::DeclareNapiFunction("listFile", ListFile::Async), NVal::DeclareNapiFunction("listFileSync", ListFile::Sync), + NVal::DeclareNapiFunction("getMimeType", ListFile::GetMimeType), + NVal::DeclareNapiFunction("getExtension", ListFile::GetExtension), NVal::DeclareNapiFunction("lseek", Lseek::Sync), NVal::DeclareNapiFunction("moveDir", MoveDir::Async), NVal::DeclareNapiFunction("moveDirSync", MoveDir::Sync), diff --git a/utils/common/include/mimetype.h b/utils/common/include/mimetype.h new file mode 100644 index 000000000..58f73cb6d --- /dev/null +++ b/utils/common/include/mimetype.h @@ -0,0 +1,164 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FILEMGMT_COMMON_MIMETYPE_H +#define FILEMGMT_COMMON_MIMETYPE_H + +#include +#include +#include + +namespace OHOS::FileManagement { +using namespace std; +static map> mimeTypeMap = { + {"application/epub+zip", {"epub"}}, + {"application/lrc", {"lrc"}}, + {"application/pkix-cert", {"cer"}}, + {"application/rss+xml", {"rss"}}, + {"application/sdp", {"sdp"}}, + {"application/smil+xml", {"smil"}}, + {"application/ttml+xml", {"ttml", "dfxp"}}, + {"application/vnd.ms-pki.stl", {"stl"}}, + {"application/vnd.ms-powerpoint", {"pot", "ppt"}}, + {"application/vnd.ms-wpl", {"wpl"}}, + {"application/vnd.stardivision.writer", {"vor"}}, + {"application/vnd.youtube.yt", {"yt"}}, + {"application/x-font", {"pcf"}}, + {"application/x-mobipocket-ebook", {"prc", "mobi"}}, + {"application/x-pem-file", {"pem"}}, + {"application/x-pkcs12", {"p12", "pfx"}}, + {"application/x-subrip", {"srt"}}, + {"application/x-webarchive", {"webarchive"}}, + {"application/x-webarchive-xml", {"webarchivexml"}}, + {"application/pgp-signature", {"pgp"}}, + {"application/x-x509-ca-cert", {"crt", "der"}}, + {"application/json", {"json"}}, + {"application/javascript", {"js"}}, + {"application/zip", {"zip"}}, + {"application/rar", {"rar"}}, + {"application/pdf", {"pdf"}}, + {"application/msword", {"doc"}}, + {"application/ms-excel", {"xls"}}, + {"application/vnd.openxmlformats-officedocument.wordprocessingml.document", {"docx"}}, + {"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", {"xlsx"}}, + {"application/vnd.openxmlformats-officedocument.presentationml.presentation", {"pptx"}}, + + {"audio/3gpp", {"3ga"}}, + {"audio/ac3", {"ac3", "a52"}}, + {"audio/amr", {"amr"}}, + {"audio/imelody", {"imy"}}, + {"audio/midi", {"rtttl", "xmf", "rtx"}}, + {"audio/mobile-xmf", {"mxmf"}}, + {"audio/mp4", {"m4a", "m4b", "m4p", "f4a", "f4b", "f4p"}}, + {"audio/mpegurl", {"m3u"}}, + {"audio/sp-midil", {"smf"}}, + {"audio/x-matroska", {"mka"}}, + {"audio/x-pn-realaudio", {"ra"}}, + {"audio/x-mpeg", {"mp3"}}, + {"audio/aac", {"aac", "adts", "adt"}}, + {"audio/basic", {"snd"}}, + {"audio/basic", {"flac"}}, + {"audio/mp4", {"mp3", "mp2", "mp1", "mpa", "m4r"}}, + {"audio/wav", {"wav"}}, + {"audio/ogg", {"ogg"}}, + + {"image/gif", {"gif"}}, + {"image/heic", {"heic"}}, + {"image/heic-sequence", {"heics", "heifs"}}, + {"image/bmp", {"bmp", "bm"}}, + {"image/heif", {"heif", "hif"}}, + {"image/avif", {"avif"}}, + {"image/ico", {"cur"}}, + {"image/webp", {"webp"}}, + {"image/x-adobe-dng", {"dng"}}, + {"image/x-fuji-raf", {"raf"}}, + {"image/x-icon", {"icon"}}, + {"image/x-nikon-nrw", {"nrw"}}, + {"image/x-panasonic-rw2", {"rw2"}}, + {"image/x-pentax-pef", {"pef"}}, + {"image/x-samsung-srw", {"srw"}}, + {"image/x-sony-arw", {"arw"}}, + {"image/jpeg", {"jpg", "jpeg", "jpe"}}, + {"image/png", {"png"}}, + {"image/svg+xml", {"svg"}}, + {"image/x-dcraw", {"raw"}}, + + {"video/3gpp2", {"3gpp2", "3gp2", "3g2"}}, + {"video/3gpp", {"3gpp", "3gp"}}, + {"video/avi", {"avi"}}, + {"video/mp4", {"mp4", "f4v", "mp4v", "mpeg4", "mp4"}}, + {"video/mp2t", {"m2ts", "mts"}}, + {"video/mp2ts", {"ts"}}, + {"video/vnd.youtube.yt", {"vt"}}, + {"video/x-webex", {"wrf"}}, + {"video/mpeg", {"mpeg", "mpeg2", "mpv2", "mp2v", "m2v", "m2t", "mpeg1", "mpv1", "mp1v", "m1v", "mpg"}}, + {"video/quicktime", {"mov"}}, + {"video/x-matroska", {"mkv"}}, + {"video/webm", {"webm"}}, + {"video/H264", {"h264"}}, + + {"text/comma-separated-values", {"csv"}}, + {"text/plain", {"diff", "po", "txt"}}, + {"text/rtf", {"rtf"}}, + {"text/text", {"phps", "m3u", "m3u8"}}, + {"text/xml", {"xml"}}, + {"text/x-vcard", {"vcf"}}, + {"text/x-c++hdr", {"hpp", "h++", "hxx","hh"}}, + {"text/x-c++src", {"cpp", "c++", "cxx","cc"}}, + {"text/css", {"css"}}, + {"text/html", {"html", "htm","shtml"}}, + {"text/markdown", {"md", "markdown"}}, + {"text/x-java", {"java"}}, + {"text/x-python", {"py"}} + +}; + +class MimeTypeUtil +{ +public: + MimeTypeUtil(){}; + ~MimeTypeUtil() = default; + + static string GetMimeTypeFromExt(const string &ext) + { + HILOGI("GetMimeTypeFromExt ext:%{public}s", ext.c_str()); + + //transform(ext.begin(), ext.end(), ext.begin(), std::tolower); + for (auto it = mimeTypeMap.begin(); it != mimeTypeMap.end(); it++) + { + auto vec = it->second; + for (auto &item : vec) + { + if (item == ext) + { + HILOGI("return mimetype %{public}s", it->first.c_str()); + return it->first; + } + } + } + return "application/octet-stream"; + } + + static vector GetExtFromMimeType(const string &mimeType) + { + auto it = mimeTypeMap.find(mimeType); + if (it != mimeTypeMap.end()) { + return it->second; + } + return {}; + } +}; +} // namespace OHOS::FileManagement +#endif // FILEMGMT_COMMON_MIMETYPE_H \ No newline at end of file -- Gitee From 4817a8f69e234144a566461f736ddb9c8c589786 Mon Sep 17 00:00:00 2001 From: yang-jingbo1985 Date: Wed, 22 Nov 2023 19:43:31 +0800 Subject: [PATCH 02/12] =?UTF-8?q?=E5=AE=9E=E7=8E=B0list=20file=20=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E6=94=AF=E6=8C=81=20mimetype=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yang-jingbo1985 --- .../js/src/mod_fs/properties/listfile.cpp | 86 +++---------------- 1 file changed, 14 insertions(+), 72 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp index 97b2de03f..66fa64c78 100755 --- a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp @@ -34,6 +34,16 @@ using namespace OHOS::FileManagement::LibN; thread_local OptionArgs g_optionArgs; +static napi_value CreateStringUtf8(napi_env env, const std::string &str) +{ + napi_value value = nullptr; + if (napi_create_string_utf8(env, str.c_str(), str.length(), &value) != napi_ok) { + HILOGE("CreateStringUtf8, value is not napi_ok"); + return nullptr; + } + return value; +} + static bool CheckSuffix(const vector &suffixs) { for (string suffix : suffixs) { @@ -450,7 +460,6 @@ napi_value ListFile::Async(napi_env env, napi_callback_info info) } napi_value ListFile::GetMimeType(napi_env env, napi_callback_info info) { - #if 0 NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ONE)) { HILOGE("Number of arguments unmatched"); @@ -458,86 +467,19 @@ napi_value ListFile::GetMimeType(napi_env env, napi_callback_info info) return nullptr; } - bool succ = false; - std::unique_ptr fileName; - std::tie(succ, fileName, std::ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); - if (!succ) { + auto [succGetFileName, fileName, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!succGetFileName) { NError(EINVAL).ThrowErr(env); return nullptr; } + std::string fileNameSuffix = fileName.get(); size_t slashIndex = fileNameSuffix.rfind("."); if (slashIndex != string::npos) { NError(EINVAL).ThrowErr(env); return nullptr; } - std::string extension = fileNameSuffix.substr(slashIndex); - - #else - size_t argc = 1; - napi_value argv[1]; - napi_value thisVar; - void* data; - napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); - if (argc < 1) - { - napi_throw_type_error(env, nullptr, "Wrong number of arguments"); - return nullptr; - } - - size_t extensionLength = 0; - napi_get_value_string_utf8(env, argv[0], nullptr, 0, &extensionLength); - std::string extension(extensionLength, '\0'); - napi_get_value_string_utf8(env, argv[0], &extension[0], extensionLength + 1, nullptr); - extension = "." + extension; -#endif - string mimeType = MimeTypeUtil::GetMimeTypeFromExt(extension); - - napi_value result; - napi_create_string_utf8(env, mimeType.c_str(), mimeType.length(), &result); - - return result; + return CreateStringUtf8(env, MimeTypeUtil::GetMimeTypeFromExt(fileNameSuffix.substr(slashIndex))); } -napi_value ListFile::GetExtension(napi_env env, napi_callback_info info) -{ - napi_status status; - size_t argc = 1; - napi_value argv[1]; - status = napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr); - if (status != napi_ok || argc != 1) { - return nullptr; - } - - char extension[MAX_SUFFIX_LENGTH] = {0}; - size_t extensionSize = 0; - status = napi_get_value_string_utf8(env, argv[0], extension, sizeof(extension), &extensionSize); - if (status != napi_ok || extensionSize == 0) { - return nullptr; - } - - string extensionStr(extension, extensionSize); - vector mimeTypeVector = MimeTypeUtil::GetExtFromMimeType(extensionStr); - - string mimeType; - if (!mimeTypeVector.empty()) { - mimeType = ""; - for (const string& str : mimeTypeVector) { - mimeType += str; - } - } - string result = mimeType.substr(mimeType.find('/') + 1); - - napi_value extensionValue; - status = napi_create_string_utf8(env, result.c_str(), result.length(), &extensionValue); - if (status != napi_ok) { - return nullptr; - } - - return extensionValue; - -} - - - } // namespace OHOS::FileManagement::ModuleFileIO \ No newline at end of file -- Gitee From 4340c864d1a113ab6b5890064c536d84d75e977b Mon Sep 17 00:00:00 2001 From: yang-jingbo1985 Date: Tue, 28 Nov 2023 01:06:58 +0800 Subject: [PATCH 03/12] =?UTF-8?q?=E5=AE=9E=E7=8E=B0list=20file=20=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E6=94=AF=E6=8C=81=20mimetype=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../js/src/mod_fs/properties/listfile.cpp | 33 +++++-------------- utils/common/include/mimetype.h | 3 -- 2 files changed, 9 insertions(+), 27 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp index 66fa64c78..a4896dc42 100755 --- a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include "file_utils.h" #include "filemgmt_libhilog.h" @@ -34,16 +35,6 @@ using namespace OHOS::FileManagement::LibN; thread_local OptionArgs g_optionArgs; -static napi_value CreateStringUtf8(napi_env env, const std::string &str) -{ - napi_value value = nullptr; - if (napi_create_string_utf8(env, str.c_str(), str.length(), &value) != napi_ok) { - HILOGE("CreateStringUtf8, value is not napi_ok"); - return nullptr; - } - return value; -} - static bool CheckSuffix(const vector &suffixs) { for (string suffix : suffixs) { @@ -211,16 +202,7 @@ static bool FilterMimetype(const vector &mimetypes, const struct dirent return true; } - size_t found = string(filename.d_name).rfind('.'); - if (found == std::string::npos) { - return false; - } - HILOGE("filename.d_name: %{public}s", filename.d_name); - - string extension = string(filename.d_name).substr(found + 1); - string fileMimetype = MimeTypeUtil::GetMimeTypeFromExt(extension); - HILOGE("Default extension: %{public}s", extension.c_str()); - HILOGE("Default fileMimetype: %{public}s", fileMimetype.c_str()); + string fileMimetype = MimeTypeUtil::GetMimeTypeFromExt(ExtractFileExt(filename.d_name)); for (const auto& mimetypeItem : mimetypes) { if (mimetypeItem == fileMimetype) { return true; @@ -472,14 +454,17 @@ napi_value ListFile::GetMimeType(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } - std::string fileNameSuffix = fileName.get(); - size_t slashIndex = fileNameSuffix.rfind("."); - if (slashIndex != string::npos) { + if (fileNameSuffix.empty()) { NError(EINVAL).ThrowErr(env); return nullptr; } - return CreateStringUtf8(env, MimeTypeUtil::GetMimeTypeFromExt(fileNameSuffix.substr(slashIndex))); + std::string tmpSuffix = ""; + size_t slashIndex = fileNameSuffix.rfind("."); + if (slashIndex != string::npos) { + tmpSuffix = fileNameSuffix.substr(slashIndex); + } + return NVal::CreateUTF8String(env, MimeTypeUtil::GetMimeTypeFromExt(tmpSuffix)).val_; } } // namespace OHOS::FileManagement::ModuleFileIO \ No newline at end of file diff --git a/utils/common/include/mimetype.h b/utils/common/include/mimetype.h index 58f73cb6d..a4024087e 100644 --- a/utils/common/include/mimetype.h +++ b/utils/common/include/mimetype.h @@ -133,9 +133,6 @@ public: static string GetMimeTypeFromExt(const string &ext) { - HILOGI("GetMimeTypeFromExt ext:%{public}s", ext.c_str()); - - //transform(ext.begin(), ext.end(), ext.begin(), std::tolower); for (auto it = mimeTypeMap.begin(); it != mimeTypeMap.end(); it++) { auto vec = it->second; -- Gitee From f514af8fc11d130396a001d5927d10ef4e36f17f Mon Sep 17 00:00:00 2001 From: yang-jingbo1985 Date: Tue, 28 Nov 2023 09:58:37 +0800 Subject: [PATCH 04/12] =?UTF-8?q?=E5=AE=9E=E7=8E=B0list=20file=20=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E6=94=AF=E6=8C=81=20mimetype=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yang-jingbo1985 --- interfaces/kits/js/src/mod_fs/properties/listfile.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/properties/listfile.h b/interfaces/kits/js/src/mod_fs/properties/listfile.h index bc772d784..ae9684f4e 100755 --- a/interfaces/kits/js/src/mod_fs/properties/listfile.h +++ b/interfaces/kits/js/src/mod_fs/properties/listfile.h @@ -28,8 +28,6 @@ public: static napi_value Sync(napi_env env, napi_callback_info info); static napi_value Async(napi_env env, napi_callback_info info); static napi_value GetMimeType(napi_env env, napi_callback_info info); - static napi_value GetExtension(napi_env env, napi_callback_info info); - }; class ListFileArgs { -- Gitee From 68d06b62e1487b643208c78f30d3c7167c9321b9 Mon Sep 17 00:00:00 2001 From: yang-jingbo1985 Date: Tue, 28 Nov 2023 10:18:45 +0800 Subject: [PATCH 05/12] =?UTF-8?q?=E5=AE=9E=E7=8E=B0list=20file=20=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E6=94=AF=E6=8C=81=20mimetype=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yang-jingbo1985 --- interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp index 30191872e..fc127e295 100644 --- a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp @@ -649,7 +649,6 @@ bool PropNExporter::Export() NVal::DeclareNapiFunction("listFile", ListFile::Async), NVal::DeclareNapiFunction("listFileSync", ListFile::Sync), NVal::DeclareNapiFunction("getMimeType", ListFile::GetMimeType), - NVal::DeclareNapiFunction("getExtension", ListFile::GetExtension), NVal::DeclareNapiFunction("lseek", Lseek::Sync), NVal::DeclareNapiFunction("moveDir", MoveDir::Async), NVal::DeclareNapiFunction("moveDirSync", MoveDir::Sync), -- Gitee From 4d59a33f6b7fdc62a09055cc3cabd07488b97e63 Mon Sep 17 00:00:00 2001 From: yang-jingbo1985 Date: Tue, 28 Nov 2023 15:51:09 +0800 Subject: [PATCH 06/12] =?UTF-8?q?=E5=AE=9E=E7=8E=B0list=20file=20=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E6=94=AF=E6=8C=81=20mimetype=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yang-jingbo1985 --- .../js/src/mod_fs/properties/listfile.cpp | 24 +++++++------- .../kits/js/src/mod_fs/properties/listfile.h | 10 +++--- utils/common/include/mimetype.h | 32 ++++++++----------- 3 files changed, 30 insertions(+), 36 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp index a4896dc42..192a55112 100755 --- a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp @@ -15,6 +15,7 @@ #include "listfile.h" +#include #include #include #include @@ -22,7 +23,6 @@ #include #include #include -#include #include "file_utils.h" #include "filemgmt_libhilog.h" @@ -203,7 +203,7 @@ static bool FilterMimetype(const vector &mimetypes, const struct dirent } string fileMimetype = MimeTypeUtil::GetMimeTypeFromExt(ExtractFileExt(filename.d_name)); - for (const auto& mimetypeItem : mimetypes) { + for (const auto &mimetypeItem : mimetypes) { if (mimetypeItem == fileMimetype) { return true; } @@ -298,7 +298,7 @@ static void Deleter(struct NameListArg *arg) static int FilterFileRes(const string &path, vector &dirents) { - unique_ptr pNameList = { new (nothrow) struct NameListArg, Deleter }; + unique_ptr pNameList = {new (nothrow) struct NameListArg, Deleter}; if (!pNameList) { HILOGE("Failed to request heap memory."); return ENOMEM; @@ -317,7 +317,7 @@ static int FilterFileRes(const string &path, vector &dirents) static int RecursiveFunc(const string &path, vector &dirents) { - unique_ptr pNameList = { new (nothrow) struct NameListArg, Deleter }; + unique_ptr pNameList = {new (nothrow) struct NameListArg, Deleter}; if (!pNameList) { HILOGE("Failed to request heap memory."); return ENOMEM; @@ -417,23 +417,23 @@ napi_value ListFile::Async(napi_env env, napi_callback_info info) auto cbExec = [arg, optionArgsTmp]() -> NError { g_optionArgs = optionArgsTmp; int ret = 0; - ret = g_optionArgs.recursion ? RecursiveFunc(g_optionArgs.path, arg->dirents) : - FilterFileRes(g_optionArgs.path, arg->dirents); + ret = g_optionArgs.recursion ? RecursiveFunc(g_optionArgs.path, arg->dirents) + : FilterFileRes(g_optionArgs.path, arg->dirents); g_optionArgs.Clear(); return ret ? NError(ret) : NError(ERRNO_NOERR); }; auto cbCompl = [arg, optionArgsTmp, path = string(path.get())](napi_env env, NError err) -> NVal { if (err) { - return { env, err.GetNapiErr(env) }; + return {env, err.GetNapiErr(env)}; } - return { env, DoListFileVector2NV(env, path, arg->dirents, optionArgsTmp.recursion) }; + return {env, DoListFileVector2NV(env, path, arg->dirents, optionArgsTmp.recursion)}; }; NVal thisVar(env, funcArg.GetThisVar()); - if (funcArg.GetArgc() == NARG_CNT::ONE || (funcArg.GetArgc() == NARG_CNT::TWO && - !NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_function))) { + if (funcArg.GetArgc() == NARG_CNT::ONE || + (funcArg.GetArgc() == NARG_CNT::TWO && !NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_function))) { return NAsyncWorkPromise(env, thisVar).Schedule(LIST_FILE_PRODUCE_NAME, cbExec, cbCompl).val_; } else { NVal cb(env, funcArg[((funcArg.GetArgc() == NARG_CNT::TWO) ? NARG_POS::SECOND : NARG_POS::THIRD)]); @@ -461,8 +461,8 @@ napi_value ListFile::GetMimeType(napi_env env, napi_callback_info info) } std::string tmpSuffix = ""; size_t slashIndex = fileNameSuffix.rfind("."); - if (slashIndex != string::npos) { - tmpSuffix = fileNameSuffix.substr(slashIndex); + if (slashIndex != string::npos) { + tmpSuffix = fileNameSuffix.substr(slashIndex + 1); } return NVal::CreateUTF8String(env, MimeTypeUtil::GetMimeTypeFromExt(tmpSuffix)).val_; } diff --git a/interfaces/kits/js/src/mod_fs/properties/listfile.h b/interfaces/kits/js/src/mod_fs/properties/listfile.h index ae9684f4e..65439551c 100755 --- a/interfaces/kits/js/src/mod_fs/properties/listfile.h +++ b/interfaces/kits/js/src/mod_fs/properties/listfile.h @@ -16,8 +16,8 @@ #ifndef INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_LISTFILE_H #define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_LISTFILE_H -#include "filemgmt_libn.h" #include "file_filter.h" +#include "filemgmt_libn.h" #include @@ -36,17 +36,15 @@ public: }; struct NameListArg { - struct dirent** namelist = { nullptr }; + struct dirent **namelist = {nullptr}; int direntNum = 0; }; constexpr int DEFAULT_SIZE = -1; constexpr int DEFAULT_MODIFY_AFTER = -1; struct OptionArgs { - FileFilter filter = FileFilterBuilder() - .SetFileSizeOver(DEFAULT_SIZE) - .SetLastModifiedAfter(DEFAULT_MODIFY_AFTER) - .Build(); + FileFilter filter = + FileFilterBuilder().SetFileSizeOver(DEFAULT_SIZE).SetLastModifiedAfter(DEFAULT_MODIFY_AFTER).Build(); int listNum = 0; int countNum = 0; bool recursion = false; diff --git a/utils/common/include/mimetype.h b/utils/common/include/mimetype.h index a4024087e..d794b07cf 100644 --- a/utils/common/include/mimetype.h +++ b/utils/common/include/mimetype.h @@ -16,9 +16,9 @@ #ifndef FILEMGMT_COMMON_MIMETYPE_H #define FILEMGMT_COMMON_MIMETYPE_H -#include -#include #include +#include +#include namespace OHOS::FileManagement { using namespace std; @@ -115,32 +115,28 @@ static map> mimeTypeMap = { {"text/text", {"phps", "m3u", "m3u8"}}, {"text/xml", {"xml"}}, {"text/x-vcard", {"vcf"}}, - {"text/x-c++hdr", {"hpp", "h++", "hxx","hh"}}, - {"text/x-c++src", {"cpp", "c++", "cxx","cc"}}, + {"text/x-c++hdr", {"hpp", "h++", "hxx", "hh"}}, + {"text/x-c++src", {"cpp", "c++", "cxx", "cc"}}, {"text/css", {"css"}}, - {"text/html", {"html", "htm","shtml"}}, + {"text/html", {"html", "htm", "shtml"}}, {"text/markdown", {"md", "markdown"}}, {"text/x-java", {"java"}}, {"text/x-python", {"py"}} }; -class MimeTypeUtil -{ +class MimeTypeUtil { public: - MimeTypeUtil(){}; + MimeTypeUtil() {}; ~MimeTypeUtil() = default; static string GetMimeTypeFromExt(const string &ext) { - for (auto it = mimeTypeMap.begin(); it != mimeTypeMap.end(); it++) - { + + for (auto it = mimeTypeMap.begin(); it != mimeTypeMap.end(); it++) { auto vec = it->second; - for (auto &item : vec) - { - if (item == ext) - { - HILOGI("return mimetype %{public}s", it->first.c_str()); + for (auto &item : vec) { + if (item == ext) { return it->first; } } @@ -148,14 +144,14 @@ public: return "application/octet-stream"; } - static vector GetExtFromMimeType(const string &mimeType) - { + static vector GetExtFromMimeType(const string &mimeType) + { auto it = mimeTypeMap.find(mimeType); if (it != mimeTypeMap.end()) { return it->second; } return {}; - } + } }; } // namespace OHOS::FileManagement #endif // FILEMGMT_COMMON_MIMETYPE_H \ No newline at end of file -- Gitee From 877cf9018543f0ff12a5d696296acca942b09582 Mon Sep 17 00:00:00 2001 From: yang-jingbo1985 Date: Wed, 29 Nov 2023 00:34:22 +0800 Subject: [PATCH 07/12] =?UTF-8?q?=E5=AE=9E=E7=8E=B0list=20file=20=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E6=94=AF=E6=8C=81=20mimetype=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yang-jingbo1985 --- .../js/src/mod_fs/properties/listfile.cpp | 37 +++++++++++-------- utils/common/include/mimetype.h | 1 - 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp index 192a55112..a6d4dcfae 100755 --- a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp @@ -53,7 +53,26 @@ static bool CheckSuffix(const vector &suffixs) return true; } -static bool GetFileFilterParam(const NVal &argv, FileFilter *filter) +static bool GetFileFilterParamByMimeType(const NVal &argv, FileFilter *filter) +{ + bool ret = false; + if (argv.HasProp("mimeType") && !argv.GetProp("mimeType").TypeIs(napi_undefined)) { + vector mimeTypes; + tie(ret, mimeTypes, ignore) = argv.GetProp("mimeType").ToStringArray(); + if (!ret) { + HILOGE("Failed to get mimeType prop."); + return false; + } + if (mimeTypes.size() == 0) { + HILOGE("Invalid mimeType."); + return false; + } + filter->SetMimeType(mimeTypes); + } + return true; +} + +static bool GetFileFilterParam(const NVal &argv, FileFilter &filter) { bool ret = false; if (argv.HasProp("suffix") && !argv.GetProp("suffix").TypeIs(napi_undefined)) { @@ -82,21 +101,9 @@ static bool GetFileFilterParam(const NVal &argv, FileFilter *filter) } filter->SetDisplayName(displayNames); } - - if (argv.HasProp("mimeType") && !argv.GetProp("mimeType").TypeIs(napi_undefined)) { - vector mimeTypes; - tie(ret, mimeTypes, ignore) = argv.GetProp("mimeType").ToStringArray(); - if (!ret) { - HILOGE("Failed to get mimeType prop."); - return false; - } - if (mimeTypes.size() == 0) { - HILOGE("Invalid mimeType."); - return false; - } - filter->SetMimeType(mimeTypes); + if (!GetFileFilterParamByMimeType(argv, filter)) { + return false; } - if (argv.HasProp("fileSizeOver") && !argv.GetProp("fileSizeOver").TypeIs(napi_undefined)) { int64_t fileSizeOver = 0; tie(ret, fileSizeOver) = argv.GetProp("fileSizeOver").ToInt64(); diff --git a/utils/common/include/mimetype.h b/utils/common/include/mimetype.h index d794b07cf..1b636a723 100644 --- a/utils/common/include/mimetype.h +++ b/utils/common/include/mimetype.h @@ -132,7 +132,6 @@ public: static string GetMimeTypeFromExt(const string &ext) { - for (auto it = mimeTypeMap.begin(); it != mimeTypeMap.end(); it++) { auto vec = it->second; for (auto &item : vec) { -- Gitee From ab454070642388ba888378e87a266ae1d109e506 Mon Sep 17 00:00:00 2001 From: yang-jingbo1985 Date: Wed, 29 Nov 2023 09:54:30 +0800 Subject: [PATCH 08/12] =?UTF-8?q?=E5=AE=9E=E7=8E=B0list=20file=20=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E6=94=AF=E6=8C=81=20mimetype=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yang-jingbo1985 --- interfaces/kits/js/src/mod_fs/properties/listfile.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp index a6d4dcfae..ce1b04c33 100755 --- a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp @@ -72,7 +72,7 @@ static bool GetFileFilterParamByMimeType(const NVal &argv, FileFilter *filter) return true; } -static bool GetFileFilterParam(const NVal &argv, FileFilter &filter) +static bool GetFileFilterParam(const NVal &argv, FileFilter *filter) { bool ret = false; if (argv.HasProp("suffix") && !argv.GetProp("suffix").TypeIs(napi_undefined)) { -- Gitee From 397a6d581a01ebf9a3c230fe68ca6ca06c86d72f Mon Sep 17 00:00:00 2001 From: yang-jingbo1985 Date: Wed, 29 Nov 2023 15:41:13 +0800 Subject: [PATCH 09/12] =?UTF-8?q?=E5=AE=9E=E7=8E=B0list=20file=20=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E6=94=AF=E6=8C=81=20mimetype=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yang-jingbo1985 --- .../js/src/mod_fs/properties/listfile.cpp | 51 +++++++------------ 1 file changed, 18 insertions(+), 33 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp index ce1b04c33..55e35c5bf 100755 --- a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp @@ -53,21 +53,17 @@ static bool CheckSuffix(const vector &suffixs) return true; } -static bool GetFileFilterParamByMimeType(const NVal &argv, FileFilter *filter) +static bool GetArray(const NVal &argv, string condition, vector &conditions) { bool ret = false; - if (argv.HasProp("mimeType") && !argv.GetProp("mimeType").TypeIs(napi_undefined)) { - vector mimeTypes; - tie(ret, mimeTypes, ignore) = argv.GetProp("mimeType").ToStringArray(); - if (!ret) { - HILOGE("Failed to get mimeType prop."); - return false; - } - if (mimeTypes.size() == 0) { - HILOGE("Invalid mimeType."); - return false; - } - filter->SetMimeType(mimeTypes); + tie(ret, conditions, ignore) = argv.GetProp(condition).ToStringArray(); + if (!ret) { + HILOGE("Failed to get %{public}s prop.", condition.c_str()); + return false; + } + if (0 == conditions.size()) { + HILOGE("Invalid %{public}s.", condition.c_str()); + return false; } return true; } @@ -77,12 +73,9 @@ static bool GetFileFilterParam(const NVal &argv, FileFilter *filter) bool ret = false; if (argv.HasProp("suffix") && !argv.GetProp("suffix").TypeIs(napi_undefined)) { vector suffixs; - tie(ret, suffixs, ignore) = argv.GetProp("suffix").ToStringArray(); - if (!ret) { - HILOGE("Failed to get suffix prop."); + if (!GetArray(argv, "suffix", suffixs)) return false; - } - if (!CheckSuffix(suffixs) || suffixs.size() == 0) { + if (!CheckSuffix(suffixs)) { HILOGE("Invalid suffix."); return false; } @@ -90,19 +83,17 @@ static bool GetFileFilterParam(const NVal &argv, FileFilter *filter) } if (argv.HasProp("displayName") && !argv.GetProp("displayName").TypeIs(napi_undefined)) { vector displayNames; - tie(ret, displayNames, ignore) = argv.GetProp("displayName").ToStringArray(); - if (!ret) { - HILOGE("Failed to get displayname prop."); - return false; - } - if (displayNames.size() == 0) { - HILOGE("Invalid displayName."); + if (!GetArray(argv, "displayName", displayNames)) { return false; } filter->SetDisplayName(displayNames); } - if (!GetFileFilterParamByMimeType(argv, filter)) { - return false; + if (argv.HasProp("mimeType") && !argv.GetProp("mimeType").TypeIs(napi_undefined)) { + vector mimeTypes; + if (!GetArray(argv, "mimeType", mimeTypes)) { + return false; + } + filter->SetMimeType(mimeTypes); } if (argv.HasProp("fileSizeOver") && !argv.GetProp("fileSizeOver").TypeIs(napi_undefined)) { int64_t fileSizeOver = 0; @@ -135,7 +126,6 @@ static bool GetOptionParam(const NVal &argv, OptionArgs *optionArgs) return false; } } - if (argv.HasProp("recursion")) { tie(succ, optionArgs->recursion) = argv.GetProp("recursion").ToBool(false); if (!succ) { @@ -143,7 +133,6 @@ static bool GetOptionParam(const NVal &argv, OptionArgs *optionArgs) return false; } } - if (argv.HasProp("filter")) { NVal filterProp = argv.GetProp("filter"); if (!filterProp.TypeIs(napi_undefined)) { @@ -208,7 +197,6 @@ static bool FilterMimetype(const vector &mimetypes, const struct dirent if (filename.d_type == DT_DIR) { return true; } - string fileMimetype = MimeTypeUtil::GetMimeTypeFromExt(ExtractFileExt(filename.d_name)); for (const auto &mimetypeItem : mimetypes) { if (mimetypeItem == fileMimetype) { @@ -285,7 +273,6 @@ static int32_t FilterFunc(const struct dirent *filename) if (string_view(filename->d_name) == "." || string_view(filename->d_name) == "..") { return FILTER_DISMATCH; } - if (g_optionArgs.countNum < g_optionArgs.listNum || g_optionArgs.listNum == 0) { if ((filename->d_type == DT_DIR && g_optionArgs.recursion) || FilterResult(*filename)) { return FILTER_MATCH; @@ -455,7 +442,6 @@ napi_value ListFile::GetMimeType(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } - auto [succGetFileName, fileName, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); if (!succGetFileName) { NError(EINVAL).ThrowErr(env); @@ -473,5 +459,4 @@ napi_value ListFile::GetMimeType(napi_env env, napi_callback_info info) } return NVal::CreateUTF8String(env, MimeTypeUtil::GetMimeTypeFromExt(tmpSuffix)).val_; } - } // namespace OHOS::FileManagement::ModuleFileIO \ No newline at end of file -- Gitee From 21cca8d962f8b55c3802728d2584de85dfa150e3 Mon Sep 17 00:00:00 2001 From: yang-jingbo1985 Date: Wed, 29 Nov 2023 17:32:07 +0800 Subject: [PATCH 10/12] =?UTF-8?q?=E5=AE=9E=E7=8E=B0list=20file=20=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E6=94=AF=E6=8C=81=20mimetype=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yang-jingbo1985 --- interfaces/kits/js/src/mod_fs/properties/listfile.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp index 55e35c5bf..34a773168 100755 --- a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp @@ -73,8 +73,9 @@ static bool GetFileFilterParam(const NVal &argv, FileFilter *filter) bool ret = false; if (argv.HasProp("suffix") && !argv.GetProp("suffix").TypeIs(napi_undefined)) { vector suffixs; - if (!GetArray(argv, "suffix", suffixs)) + if (!GetArray(argv, "suffix", suffixs)) { return false; + } if (!CheckSuffix(suffixs)) { HILOGE("Invalid suffix."); return false; -- Gitee From f5283b259af9f9c1b91161700606c41b73c519ad Mon Sep 17 00:00:00 2001 From: yang-jingbo1985 Date: Thu, 30 Nov 2023 17:54:30 +0800 Subject: [PATCH 11/12] =?UTF-8?q?=E5=AE=9E=E7=8E=B0list=20file=20=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E6=94=AF=E6=8C=81=20mimetype=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yang-jingbo1985 --- utils/common/include/mimetype.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/common/include/mimetype.h b/utils/common/include/mimetype.h index 1b636a723..d9917eac6 100644 --- a/utils/common/include/mimetype.h +++ b/utils/common/include/mimetype.h @@ -140,7 +140,7 @@ public: } } } - return "application/octet-stream"; + return ""; } static vector GetExtFromMimeType(const string &mimeType) -- Gitee From 29391e48d24d8e05151288ebed55f738f3ad940b Mon Sep 17 00:00:00 2001 From: yang-jingbo1985 Date: Tue, 5 Dec 2023 16:34:50 +0800 Subject: [PATCH 12/12] =?UTF-8?q?=E5=AE=9E=E7=8E=B0list=20file=20=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E6=94=AF=E6=8C=81=20mimetype=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yang-jingbo1985 --- .../js/src/mod_fs/properties/listfile.cpp | 21 +++++++------------ utils/common/include/mimetype.h | 18 +++++++--------- 2 files changed, 16 insertions(+), 23 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp index 34a773168..2656e9701 100755 --- a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp @@ -53,7 +53,7 @@ static bool CheckSuffix(const vector &suffixs) return true; } -static bool GetArray(const NVal &argv, string condition, vector &conditions) +static bool GetArray(const NVal &argv, const string condition, vector &conditions) { bool ret = false; tie(ret, conditions, ignore) = argv.GetProp(condition).ToStringArray(); @@ -61,7 +61,7 @@ static bool GetArray(const NVal &argv, string condition, vector &conditi HILOGE("Failed to get %{public}s prop.", condition.c_str()); return false; } - if (0 == conditions.size()) { + if (conditions.empty()) { HILOGE("Invalid %{public}s.", condition.c_str()); return false; } @@ -246,15 +246,15 @@ static bool FilterLastModifyTime(const double lastModifiedAfter, const struct di static bool FilterResult(const struct dirent &filename) { vector fSuffixs = g_optionArgs.filter.GetSuffix(); - if (!FilterSuffix(fSuffixs, filename) && fSuffixs.size() > 0) { + if (!FilterSuffix(fSuffixs, filename) && !fSuffixs.empty()) { return false; } vector fDisplaynames = g_optionArgs.filter.GetDisplayName(); - if (!FilterDisplayname(fDisplaynames, filename) && fDisplaynames.size() > 0) { + if (!FilterDisplayname(fDisplaynames, filename) && !fDisplaynames.empty()) { return false; } vector fMimetype = g_optionArgs.filter.GetMimeType(); - if (!FilterMimetype(fMimetype, filename) && fMimetype.size() > 0) { + if (!FilterMimetype(fMimetype, filename) && !fMimetype.empty()) { return false; } int64_t fFileSizeOver = g_optionArgs.filter.GetFileSizeOver(); @@ -444,20 +444,15 @@ napi_value ListFile::GetMimeType(napi_env env, napi_callback_info info) return nullptr; } auto [succGetFileName, fileName, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); - if (!succGetFileName) { - NError(EINVAL).ThrowErr(env); - return nullptr; - } std::string fileNameSuffix = fileName.get(); - if (fileNameSuffix.empty()) { + if (!succGetFileName && fileNameSuffix.empty() ) { NError(EINVAL).ThrowErr(env); return nullptr; } - std::string tmpSuffix = ""; size_t slashIndex = fileNameSuffix.rfind("."); if (slashIndex != string::npos) { - tmpSuffix = fileNameSuffix.substr(slashIndex + 1); + fileNameSuffix = fileNameSuffix.substr(slashIndex + 1); } - return NVal::CreateUTF8String(env, MimeTypeUtil::GetMimeTypeFromExt(tmpSuffix)).val_; + return NVal::CreateUTF8String(env, MimeTypeUtil::GetMimeTypeFromExt(fileNameSuffix)).val_; } } // namespace OHOS::FileManagement::ModuleFileIO \ No newline at end of file diff --git a/utils/common/include/mimetype.h b/utils/common/include/mimetype.h index d9917eac6..21413b741 100644 --- a/utils/common/include/mimetype.h +++ b/utils/common/include/mimetype.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -16,13 +16,12 @@ #ifndef FILEMGMT_COMMON_MIMETYPE_H #define FILEMGMT_COMMON_MIMETYPE_H -#include #include #include +#include namespace OHOS::FileManagement { -using namespace std; -static map> mimeTypeMap = { +static inline std::unordered_map > mimeTypeMap = { {"application/epub+zip", {"epub"}}, {"application/lrc", {"lrc"}}, {"application/pkix-cert", {"cer"}}, @@ -122,7 +121,6 @@ static map> mimeTypeMap = { {"text/markdown", {"md", "markdown"}}, {"text/x-java", {"java"}}, {"text/x-python", {"py"}} - }; class MimeTypeUtil { @@ -130,20 +128,20 @@ public: MimeTypeUtil() {}; ~MimeTypeUtil() = default; - static string GetMimeTypeFromExt(const string &ext) + static std::string GetMimeTypeFromExt(const std::string &ext) { - for (auto it = mimeTypeMap.begin(); it != mimeTypeMap.end(); it++) { - auto vec = it->second; + for (auto &iter : mimeTypeMap) { + auto vec = iter.second; for (auto &item : vec) { if (item == ext) { - return it->first; + return iter.first; } } } return ""; } - static vector GetExtFromMimeType(const string &mimeType) + static std::vector GetExtFromMimeType(const std::string &mimeType) { auto it = mimeTypeMap.find(mimeType); if (it != mimeTypeMap.end()) { -- Gitee