• 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     json firstFloorObjs;
47     jFile >> firstFloorObjs;
48     for (auto& firstFloorObj : firstFloorObjs.items()) {
49         json secondFloorJsons = json::parse(firstFloorObj.value().dump(), nullptr, false);
50         for (auto& secondFloorJson : secondFloorJsons.items()) {
51             json thirdFloorJsons = json::parse(secondFloorJson.value().dump(), nullptr, false);
52             // Key: MimeType, Value: Extension array.
53             mediaJsonMap_.insert(std::pair<string, vector<string>>(secondFloorJson.key(), thirdFloorJsons));
54         }
55     }
56 }
57 
InitMimeTypeMap()58 int32_t MimeTypeUtils::InitMimeTypeMap()
59 {
60     CreateMapFromJson();
61     if (mediaJsonMap_.empty()) {
62         return E_FAIL;
63     }
64     return E_OK;
65 }
66 
GetMimeTypeFromExtension(const string & extension)67 string MimeTypeUtils::GetMimeTypeFromExtension(const string &extension)
68 {
69     return GetMimeTypeFromExtension(extension, mediaJsonMap_);
70 }
71 
GetMimeTypeFromExtension(const string & extension,const MimeTypeMap & mimeTypeMap)72 string MimeTypeUtils::GetMimeTypeFromExtension(const string &extension,
73     const MimeTypeMap &mimeTypeMap)
74 {
75     std::string tmp = std::move(extension);
76     std::transform(tmp.begin(), tmp.end(), tmp.begin(), ::tolower);
77     for (auto &item : mimeTypeMap) {
78         for (auto &ext : item.second) {
79             if (ext == tmp) {
80                 return item.first;
81             }
82         }
83     }
84     return DEFAULT_MIME_TYPE;
85 }
86 
GetMediaTypeFromMimeType(const string & mimeType)87 MediaType MimeTypeUtils::GetMediaTypeFromMimeType(const string &mimeType)
88 {
89     size_t pos = mimeType.find_first_of("/");
90     if (pos == string::npos) {
91         MEDIA_ERR_LOG("Invalid mime type: %{public}s", mimeType.c_str());
92         return MEDIA_TYPE_FILE;
93     }
94     string prefix = mimeType.substr(0, pos);
95     if (prefix == "audio") {
96         return MEDIA_TYPE_AUDIO;
97     } else if (prefix == "video") {
98         return MEDIA_TYPE_VIDEO;
99     } else if (prefix == "image") {
100         return MEDIA_TYPE_IMAGE;
101     } else {
102         return MEDIA_TYPE_FILE;
103     }
104 }
105 }
106 }
107