• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 "ringtone_mimetype_utils.h"
17 
18 #include <fstream>
19 
20 #include "ringtone_log.h"
21 #include "ringtone_errno.h"
22 
23 namespace OHOS {
24 namespace Media {
25 using namespace std;
26 using namespace nlohmann;
27 
28 using MimeTypeMap = unordered_map<string, vector<string>>;
29 
30 MimeTypeMap RingtoneMimeTypeUtils::mediaJsonMap_;
31 const string DEFAULT_MIME_TYPE = "application/octet-stream";
32 
33 static const std::unordered_map<std::string, std::vector<std::string>> RINGTONE_MIME_TYPE_MAP = {
34     { "application/epub+zip", { "epub" } },
35     { "application/lrc", { "lrc"} },
36     { "application/pkix-cert", { "cer" } },
37     { "application/rss+xml", { "rss" } },
38     { "application/sdp", { "sdp" } },
39     { "application/smil+xml", { "smil" } },
40     { "application/ttml+xml", { "ttml", "dfxp" } },
41     { "application/vnd.ms-pki.stl", { "stl" } },
42     { "application/vnd.ms-powerpoint", { "pot", "ppt" } },
43     { "application/vnd.ms-wpl", { "wpl" } },
44     { "application/vnd.stardivision.writer", { "vor" } },
45     { "application/vnd.youtube.yt", { "yt" } },
46     { "application/x-font", { "pcf" } },
47     { "application/x-mobipocket-ebook", { "prc", "mobi" } },
48     { "application/x-pem-file", { "pem" } },
49     { "application/x-pkcs12", { "p12", "pfx" } },
50     { "application/x-subrip", { "srt" } },
51     { "application/x-webarchive", { "webarchive" } },
52     { "application/x-webarchive-xml", { "webarchivexml" } },
53     { "application/pgp-signature", { "pgp" } },
54     { "application/x-x509-ca-cert", { "crt", "der" } },
55     { "application/json", { "json" } },
56     { "application/javascript", { "js" } },
57     { "application/zip", { "zip" } },
58     { "application/rar", { "rar" } },
59     { "application/pdf", { "pdf" } },
60     { "application/msword", { "doc" } },
61     { "application/ms-excel", { "xls" } },
62     { "application/vnd.openxmlformats-officedocument.wordprocessingml.document", { "docx" } },
63     { "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", { "xlsx" } },
64     { "application/vnd.openxmlformats-officedocument.presentationml.presentation", { "pptx" } },
65     { "audio/3gpp", { "3ga" } },
66     { "audio/ac3", { "ac3", "a52"} },
67     { "audio/amr", { "amr" } },
68     { "audio/imelody", { "imy" } },
69     { "audio/midi", { "rtttl", "xmf", "rtx" } },
70     { "audio/mobile-xmf", { "mxmf"} },
71     { "audio/mp4", { "m4a", "m4b", "m4p", "f4a", "f4b", "f4p" } },
72     { "audio/mpegurl", { "m3u" } },
73     { "audio/sp-midi", { "smf" } },
74     { "audio/x-matroska", { "mka" } },
75     { "audio/x-pn-realaudio", { "ra" } },
76     { "audio/x-mpeg", { "mp3" } },
77     { "audio/ffmpeg", { "ape" } },
78     { "audio/aac", { "aac", "adts", "adt" } },
79     { "audio/basic", { "snd" } },
80     { "audio/flac", { "flac" } },
81     { "audio/mpeg", { "mp3", "mp2", "mp1", "mpa", "m4r" } },
82     { "audio/wav", { "wav" } },
83     { "audio/ogg", { "ogg" } },
84     { "image/gif", { "gif"} },
85     { "image/heic", { "heic" } },
86     { "image/heic-sequence", { "heics", "heifs" } },
87     { "image/bmp", { "bmp", "bm" } },
88     { "image/heif", { "heif", "hif" } },
89     { "image/avif", { "avif" } },
90     { "image/ico", { "cur" } },
91     { "image/webp", { "webp"} },
92     { "image/x-adobe-dng", { "dng" } },
93     { "image/x-fuji-raf", { "raf" } },
94     { "image/x-icon", { "ico" } },
95     { "image/x-nikon-nrw", { "nrw" } },
96     { "image/x-panasonic-rw2", { "rw2" } },
97     { "image/x-pentax-pef", { "pef" } },
98     { "image/x-samsung-srw", { "srw" } },
99     { "image/x-sony-arw", { "arw" } },
100     { "image/jpeg", { "jpg", "jpeg", "jpe" } },
101     { "image/png", { "png" } },
102     { "image/svg+xml", { "svg" } },
103     { "image/x-dcraw", { "raw" } },
104     { "video/mp4", { "mp4" } },
105     { "text/comma-separated-values", { "csv" } },
106     { "text/plain", { "diff", "po", "txt" } },
107     { "text/rtf", { "rtf" } },
108     { "text/text", { "phps", "m3u", "m3u8" } },
109     { "text/xml", { "xml" } },
110     { "text/x-vcard", { "vcf" } },
111     { "text/x-c++hdr", { "hpp", "h++", "hxx", "hh" } },
112     { "text/x-c++src", { "cpp", "c++", "cxx", "cc" } },
113     { "text/css", { "css" } },
114     { "text/html", { "html", "htm", "shtml"} },
115     { "text/markdown", { "md", "markdown" } },
116     { "text/x-java", { "java" } },
117     { "text/x-python", { "py" } }
118 };
119 
InitMimeTypeMap()120 int32_t RingtoneMimeTypeUtils::InitMimeTypeMap()
121 {
122     RINGTONE_INFO_LOG("InitMimeTypeMap start.");
123     mediaJsonMap_ = RINGTONE_MIME_TYPE_MAP;
124     if (mediaJsonMap_.empty()) {
125         RINGTONE_ERR_LOG("JsonMap is empty");
126         return E_FAIL;
127     }
128     return E_OK;
129 }
130 
GetMimeTypeFromExtension(const string & extension)131 string RingtoneMimeTypeUtils::GetMimeTypeFromExtension(const string &extension)
132 {
133     return GetMimeTypeFromExtension(extension, mediaJsonMap_);
134 }
135 
GetMimeTypeFromExtension(const string & extension,const MimeTypeMap & mimeTypeMap)136 string RingtoneMimeTypeUtils::GetMimeTypeFromExtension(const string &extension,
137     const MimeTypeMap &mimeTypeMap)
138 {
139     string tmp = extension;
140     transform(tmp.begin(), tmp.end(), tmp.begin(), ::tolower);
141     for (auto &item : mimeTypeMap) {
142         for (auto &ext : item.second) {
143             if (ext == tmp) {
144                 return item.first;
145             }
146         }
147     }
148     return DEFAULT_MIME_TYPE;
149 }
150 
GetMediaTypeFromMimeType(const string & mimeType)151 RingtoneMediaType RingtoneMimeTypeUtils::GetMediaTypeFromMimeType(const string &mimeType)
152 {
153     size_t pos = mimeType.find_first_of("/");
154     if (pos == string::npos) {
155         RINGTONE_ERR_LOG("Invalid mime type: %{public}s", mimeType.c_str());
156         return RINGTONE_MEDIA_TYPE_INVALID;
157     }
158     string prefix = mimeType.substr(0, pos);
159     if (prefix == "audio") {
160         return RINGTONE_MEDIA_TYPE_AUDIO;
161     } else if (prefix == "video") {
162         return RINGTONE_MEDIA_TYPE_VIDEO;
163     } else {
164         RINGTONE_WARN_LOG("Invalid mime type: %{public}s", mimeType.c_str());
165         return RINGTONE_MEDIA_TYPE_INVALID;
166     }
167 }
168 } // namespace Media
169 } // namespace OHOS
170