988c1a32444c12eadb0a6edd54a14484a2afcb8b
[framework/web/wrt-commons.git] / modules / utils / src / mime_type_utils.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 #include <set>
17 #include <dpl/assert.h>
18 #include <vector>
19
20 #include <map>
21
22 #include <mime_type_utils.h>
23
24 const std::set<DPL::String>& MimeTypeUtils::getMimeTypesSupportedForIcon()
25 {
26     static std::set<DPL::String> set;
27     DPL::String (*s)(const std::string&) = DPL::FromASCIIString;
28     if (set.empty()) {
29         set.insert(s("image/gif"));
30         set.insert(s("image/png"));
31         set.insert(s("image/vnd.microsoft.icon"));
32         set.insert(s("image/svg+xml"));
33         set.insert(s("image/jpeg"));
34     }
35     return set;
36 }
37
38 const std::set<DPL::String>& MimeTypeUtils::getMimeTypesSupportedForStartFile()
39 {
40     static std::set<DPL::String> set;
41     DPL::String (*s)(const std::string&) = DPL::FromASCIIString;
42     if (set.empty()) {
43         set.insert(s("text/html"));
44         set.insert(s("application/xhtml+xml"));
45     }
46     return set;
47 }
48
49 bool MimeTypeUtils::isMimeTypeSupportedForStartFile(const DPL::String& mimeType)
50 {
51     return getMimeTypesSupportedForStartFile().count(stripMimeParameters(
52                                                          mimeType)) > 0;
53 }
54
55 const MimeTypeUtils::FileIdentificationMap& MimeTypeUtils::
56     getFileIdentificationMap()
57 {
58     static FileIdentificationMap map;
59     DPL::String (*s)(const std::string&) = DPL::FromASCIIString;
60     if (map.empty()) {
61         map[s(".html")] = s("text/html");
62         map[s(".htm")] = s("text/html");
63         map[s(".css")] = s("text/css");
64         map[s(".js")] = s("application/javascript");
65         map[s(".xml")] = s("application/xml");
66         map[s(".txt")] = s("text/plain");
67         map[s(".wav")] = s("audio/x-wav");
68         map[s(".xhtml")] = s("application/xhtml+xml");
69         map[s(".xht")] = s("application/xhtml+xml");
70         map[s(".gif")] = s("image/gif");
71         map[s(".png")] = s("image/png");
72         map[s(".ico")] = s("image/vnd.microsoft.icon");
73         map[s(".svg")] = s("image/svg+xml");
74         map[s(".jpg")] = s("image/jpeg");
75     }
76     return map;
77 }
78
79 bool MimeTypeUtils::isMimeTypeSupportedForIcon(const DPL::String& mimeType)
80 {
81     return getMimeTypesSupportedForIcon().count(stripMimeParameters(mimeType))
82            > 0;
83 }
84
85 DPL::String MimeTypeUtils::stripMimeParameters(const DPL::String& mimeType)
86 {
87     size_t parametersStart = mimeType.find_first_of(L';');
88     if (parametersStart != DPL::String::npos) {
89         return mimeType.substr(0, parametersStart);
90     } else {
91         return mimeType;
92     }
93 }
94
95 MimeTypeUtils::MimeAttributes MimeTypeUtils::getMimeAttributes(
96         const DPL::String& mimeType)
97 {
98     MimeAttributes attributes;
99     std::vector<DPL::String> tokens;
100     DPL::Tokenize(mimeType, L";=", std::back_inserter(tokens));
101     for (unsigned int i = 1; i < tokens.size(); i += 2) {
102         attributes[tokens[i]] = tokens[i + 1];
103     }
104     return attributes;
105 }
106
107 bool MimeTypeUtils::isValidIcon(const DPL::String& path)
108 {
109     return getMimeTypesSupportedForIcon().count(identifyFileMimeType(path)) > 0;
110 }
111
112 bool MimeTypeUtils::isValidStartFile(const DPL::String& path,
113         const DPL::OptionalString& providedMimeType)
114 {
115     DPL::String mimeType = (!!providedMimeType) ? stripMimeParameters(
116             *providedMimeType) : identifyFileMimeType(path);
117     return getMimeTypesSupportedForStartFile().count(mimeType) > 0;
118 }
119
120 DPL::String MimeTypeUtils::getFileNameFromPath(const DPL::String& path)
121 {
122     size_t lastSlashPos = path.find_last_of(L'/');
123     return path.substr(lastSlashPos + 1);
124 }
125
126 DPL::String MimeTypeUtils::identifyFileMimeType(const DPL::String& path)
127 {
128     DPL::String name = getFileNameFromPath(path); //step 4
129
130     if (name.size() == 0) {
131         ThrowMsg(Exception::InvalidFileName, "Path should contain a file name.");
132     }
133
134     size_t lastFullStop = name.find_last_of(L'.');
135     if (lastFullStop != 0 && lastFullStop != DPL::String::npos) { //step 5
136         DPL::String extension = name.substr(lastFullStop); //step 6 & 7
137         if (extension.size() > 0) { //step 8
138             //step 9
139             std::transform(extension.begin(), extension.end(),
140                            extension.begin(), ::towlower);
141             FileIdentificationMap::const_iterator it =
142                 getFileIdentificationMap().find(extension);
143             if (it != getFileIdentificationMap().end()) {
144                 return it->second;
145             }
146         }
147     }
148     //TODO step 10 - sniff
149     return DPL::FromASCIIString("application/sniff");
150 }