• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "mimetype_utils.h"
17 
18 #include <algorithm>
19 #include <fstream>
20 
21 #include "media_log.h"
22 #include "medialibrary_errno.h"
23 
24 using std::string;
25 using std::vector;
26 using std::unordered_map;
27 using namespace nlohmann;
28 
29 namespace OHOS {
30 namespace Media {
31 using MimeTypeMap = unordered_map<string, vector<string>>;
32 
33 MimeTypeMap MimeTypeUtils::mediaJsonMap_;
34 const string MIMETYPE_JSON_PATH = "/system/etc/userfilemanager/userfilemanager_mimetypes.json";
35 const string DEFAULT_MIME_TYPE = "application/octet-stream";
36 
37 /**
38  * The format of the target json file:
39  * First floor: Media type string, such as image, video, audio, etc.
40  * Second floor: Mime type string
41  * Third floor: Extension array.
42 */
CreateMapFromJson()43 void MimeTypeUtils::CreateMapFromJson()
44 {
45     std::ifstream jFile(MIMETYPE_JSON_PATH);
46     if (!jFile.is_open()) {
47         MEDIA_ERR_LOG("Failed to open: %{private}s", MIMETYPE_JSON_PATH.c_str());
48         return;
49     }
50     json firstFloorObjs;
51     jFile >> firstFloorObjs;
52     jFile.close();
53     for (auto& firstFloorObj : firstFloorObjs.items()) {
54         json secondFloorJsons = json::parse(firstFloorObj.value().dump(), nullptr, false);
55         if (secondFloorJsons.is_discarded() || secondFloorJsons.empty()) {
56             MEDIA_ERR_LOG("parse secondFloorJsons failed");
57             return;
58         }
59         for (auto& secondFloorJson : secondFloorJsons.items()) {
60             json thirdFloorJsons = json::parse(secondFloorJson.value().dump(), nullptr, false);
61             if (thirdFloorJsons.is_discarded() || thirdFloorJsons.empty()) {
62                 MEDIA_ERR_LOG("parse thirdFloorJsons failed");
63                 return;
64             }
65             // Key: MimeType, Value: Extension array.
66             mediaJsonMap_.insert(std::pair<string, vector<string>>(secondFloorJson.key(), thirdFloorJsons));
67         }
68     }
69 }
70 
InitMimeTypeMap()71 int32_t MimeTypeUtils::InitMimeTypeMap()
72 {
73     CreateMapFromJson();
74     if (mediaJsonMap_.empty()) {
75         return E_FAIL;
76     }
77     return E_OK;
78 }
79 
GetMimeTypeFromExtension(const string & extension)80 string MimeTypeUtils::GetMimeTypeFromExtension(const string &extension)
81 {
82     return GetMimeTypeFromExtension(extension, mediaJsonMap_);
83 }
84 
GetMimeTypeFromExtension(const string & extension,const MimeTypeMap & mimeTypeMap)85 string MimeTypeUtils::GetMimeTypeFromExtension(const string &extension,
86     const MimeTypeMap &mimeTypeMap)
87 {
88     std::string tmp = std::move(extension);
89     std::transform(tmp.begin(), tmp.end(), tmp.begin(), ::tolower);
90     for (auto &item : mimeTypeMap) {
91         for (auto &ext : item.second) {
92             if (ext == tmp) {
93                 return item.first;
94             }
95         }
96     }
97     return DEFAULT_MIME_TYPE;
98 }
99 
GetMediaTypeFromMimeType(const string & mimeType)100 MediaType MimeTypeUtils::GetMediaTypeFromMimeType(const string &mimeType)
101 {
102     size_t pos = mimeType.find_first_of("/");
103     if (pos == string::npos) {
104         MEDIA_ERR_LOG("Invalid mime type: %{private}s", mimeType.c_str());
105         return MEDIA_TYPE_FILE;
106     }
107     string prefix = mimeType.substr(0, pos);
108     if (prefix == "audio") {
109         return MEDIA_TYPE_AUDIO;
110     } else if (prefix == "video") {
111         return MEDIA_TYPE_VIDEO;
112     } else if (prefix == "image") {
113         return MEDIA_TYPE_IMAGE;
114     } else {
115         return MEDIA_TYPE_FILE;
116     }
117 }
118 }
119 }
120