• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 #ifndef SCANNER_UTILS_H
17 #define SCANNER_UTILS_H
18 
19 #include <string>
20 #include <unordered_map>
21 #include <unordered_set>
22 #include <vector>
23 #include <sys/stat.h>
24 
25 #include "medialibrary_type_const.h"
26 #include "media_scanner_const.h"
27 
28 namespace OHOS {
29 namespace Media {
30 enum ErrorCodes {
31     ERR_FAIL = -1,
32     ERR_SUCCESS,
33     ERR_EMPTY_ARGS,
34     ERR_NOT_ACCESSIBLE,
35     ERR_INCORRECT_PATH,
36     ERR_MEM_ALLOC_FAIL,
37     ERR_MIMETYPE_NOTSUPPORT,
38     ERR_SCAN_NOT_INIT
39 };
40 
41 const int32_t MAX_BATCH_SIZE = 5;
42 
43 constexpr int32_t UNKNOWN_ID = -1;
44 
45 // Const for File Metadata defaults
46 const std::string FILE_PATH_DEFAULT = "";
47 const std::string FILE_NAME_DEFAULT = "";
48 const int64_t FILE_SIZE_DEFAULT = 0;
49 const std::string URI_DEFAULT = "";
50 const int64_t FILE_DATE_ADDED_DEFAULT = 0;
51 const int64_t FILE_DATE_MODIFIED_DEFAULT = 0;
52 const MediaType FILE_MEDIA_TYPE_DEFAULT = MEDIA_TYPE_FILE;
53 const int32_t FILE_ID_DEFAULT = 0;
54 const std::string FILE_EXTENSION_DEFAULT = "";
55 
56 const int32_t FILE_DURATION_DEFAULT = 0;
57 const std::string FILE_TITLE_DEFAULT = "";
58 const std::string FILE_ARTIST_DEFAULT = "";
59 const int32_t FILE_HEIGHT_DEFAULT = 0;
60 const int32_t FILE_WIDTH_DEFAULT = 0;
61 const int32_t FILE_ALBUM_ID_DEFAULT = 0;
62 const std::string FILE_ALBUM_NAME_DEFAULT = "";
63 const int32_t FILE_ORIENTATION_DEFAULT = 0;
64 const std::string FILE_RELATIVE_PATH_DEFAULT = "";
65 const std::string FILE_RECYCLE_PATH_DEFAULT = "";
66 const int64_t FILE_DATE_TAKEN_DEFAULT = 0;
67 const double FILE_LONGITUDE_DEFAULT = 0;
68 const double FILE_LATITUDE_DEFAULT = 0;
69 
70 const std::string DEFAULT_AUDIO_MIME_TYPE = "audio/*";
71 const std::string DEFAULT_VIDEO_MIME_TYPE = "video/*";
72 const std::string DEFAULT_IMAGE_MIME_TYPE = "image/*";
73 const std::string DEFAULT_FILE_MIME_TYPE = "file/*";
74 
75 static std::vector<std::string> EXTRACTOR_SUPPORTED_MIME = {
76     DEFAULT_AUDIO_MIME_TYPE,
77     DEFAULT_VIDEO_MIME_TYPE,
78     DEFAULT_IMAGE_MIME_TYPE
79 };
80 
81 static const std::unordered_map<std::string, std::string> SUPPORTED_EXTN_MAP = {
82     /** Supported image types */
83     {IMAGE_CONTAINER_TYPE_BMP, DEFAULT_IMAGE_MIME_TYPE},
84     {IMAGE_CONTAINER_TYPE_BM, DEFAULT_IMAGE_MIME_TYPE},
85     {IMAGE_CONTAINER_TYPE_GIF, DEFAULT_IMAGE_MIME_TYPE},
86     {IMAGE_CONTAINER_TYPE_JPG, DEFAULT_IMAGE_MIME_TYPE},
87     {IMAGE_CONTAINER_TYPE_JPEG, DEFAULT_IMAGE_MIME_TYPE},
88     {IMAGE_CONTAINER_TYPE_JPE, DEFAULT_IMAGE_MIME_TYPE},
89     {IMAGE_CONTAINER_TYPE_PNG, DEFAULT_IMAGE_MIME_TYPE},
90     {IMAGE_CONTAINER_TYPE_WEBP, DEFAULT_IMAGE_MIME_TYPE},
91     {IMAGE_CONTAINER_TYPE_RAW, DEFAULT_IMAGE_MIME_TYPE},
92     {IMAGE_CONTAINER_TYPE_SVG, DEFAULT_IMAGE_MIME_TYPE},
93     {IMAGE_CONTAINER_TYPE_HEIF, DEFAULT_IMAGE_MIME_TYPE},
94     /** Supported video container types */
95     {VIDEO_CONTAINER_TYPE_MP4, DEFAULT_VIDEO_MIME_TYPE},
96     {VIDEO_CONTAINER_TYPE_3GP, DEFAULT_VIDEO_MIME_TYPE},
97     {VIDEO_CONTAINER_TYPE_MPG, DEFAULT_VIDEO_MIME_TYPE},
98     {VIDEO_CONTAINER_TYPE_MOV, DEFAULT_VIDEO_MIME_TYPE},
99     {VIDEO_CONTAINER_TYPE_WEBM, DEFAULT_VIDEO_MIME_TYPE},
100     {VIDEO_CONTAINER_TYPE_MKV, DEFAULT_VIDEO_MIME_TYPE},
101     {AUDIO_CONTAINER_TYPE_AAC, DEFAULT_AUDIO_MIME_TYPE},
102     {AUDIO_CONTAINER_TYPE_MP3, DEFAULT_AUDIO_MIME_TYPE},
103     {AUDIO_CONTAINER_TYPE_FLAC, DEFAULT_AUDIO_MIME_TYPE},
104     {AUDIO_CONTAINER_TYPE_WAV, DEFAULT_AUDIO_MIME_TYPE},
105     {AUDIO_CONTAINER_TYPE_OGG, DEFAULT_AUDIO_MIME_TYPE},
106     {"7z", "application/x-7z-compressed"},
107     {"bz", "application/x-bzip"},
108     {"bz2", "application/x-bzip2"},
109     {"cer", "application/pkix-cert"},
110     {"clp", "application/x-msclip"},
111     {"crl", "application/pkix-crl"},
112     {"css", "text/css"},
113     {"csv", "text/csv"},
114     {"der", "application/x-x509-ca-cert"},
115     {"doc", "application/msword"},
116     {"docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
117     {"exe", "application/x-msdownload"},
118     {"gtar", "application/x-gtar"},
119     {"gz", "application/gzip"},
120     {"html", "text/html"},
121     {"ics", "text/calendar"},
122     {"ipfix", "application/ipfix"},
123     {"jar", "application/java-archive"},
124     {"json", "application/json"},
125     {"mdb", "application/x-msaccess"},
126     {"nsf", "application/vnd.lotus-notes"},
127     {"odb", "application/vnd.oasis.opendocument.database"},
128     {"odc", "application/vnd.oasis.opendocument.chart"},
129     {"odp", "application/vnd.oasis.opendocument.presentation"},
130     {"ods", "application/vnd.oasis.opendocument.spreadsheet"},
131     {"odt", "application/vnd.oasis.opendocument.text"},
132     {"p10", "application/pkcs10"},
133     {"p12", "application/x-pkcs12"},
134     {"p7b", "application/x-pkcs7-certificates"},
135     {"p7m", "application/pkcs7-mime"},
136     {"p7r", "application/x-pkcs7-certreqresp"},
137     {"p7s", "application/pkcs7-signature"},
138     {"p8", "application/pkcs8"},
139     {"pdf", "application/pdf"},
140     {"pki", "application/pkixcmp"},
141     {"pkipath", "application/pkix-pkipath"},
142     {"ppt", "application/vnd.ms-powerpoint"},
143     {"pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"},
144     {"rar", "application/x-rar-compressed"},
145     {"rss", "application/rss+xml"},
146     {"rtf", "application/rtf"},
147     {"rtx", "text/richtext"},
148     {"sxc", "application/vnd.sun.xml.calc"},
149     {"sxg", "application/vnd.sun.xml.writer.global"},
150     {"sxi", "application/vnd.sun.xml.impress"},
151     {"sxw", "application/vnd.sun.xml.writer"},
152     {"tar", "application/x-tar"},
153     {"texinfo", "application/x-texinfo"},
154     {"torrent", "application/x-bittorrent"},
155     {"txt", "text/plain"},
156     {"vcf", "text/x-vcard"},
157     {"vcs", "text/x-vcalendar"},
158     {"vsd", "application/vnd.visio"},
159     {"vsdx", "application/vnd.visio2013"},
160     {"vxml", "application/voicexml+xml"},
161     {"wml", "text/vnd.wap.wml"},
162     {"wmls", "text/vnd.wap.wmlscript"},
163     {"wps", "application/vnd.ms-works"},
164     {"wri", "application/x-mswrite"},
165     {"xdf", "application/xcap-diff+xml"},
166     {"xhtml", "application/xhtml+xml"},
167     {"xls", "application/vnd.ms-excel"},
168     {"xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
169     {"xml", "application/xml"},
170     {"xps", "application/vnd.ms-xpsdocument"},
171     {"yaml", "text/yaml"},
172     {"zip", "application/zip"},
173 };
174 
175 class ScannerUtils {
176 public:
177     ScannerUtils();
178     ~ScannerUtils();
179 
180     static bool IsExists(const std::string &path);
181     static std::string GetFileNameFromUri(const std::string &path);
182     static std::string GetFileExtensionFromFileUri(const std::string &path);
183     static std::string GetMimeTypeFromExtension(const std::string &extension);
184     static std::string GetParentPath(const std::string &path);
185     static bool IsFileHidden(const std::string &path);
186     static bool IsDirectory(const std::string &path);
187     static MediaType GetMediatypeFromMimetype(const std::string &mimetype);
188     static void GetRootMediaDir(std::string &dir);
189     static std::string GetFileTitle(const std::string &displayName);
190     static bool IsDirHiddenRecursive(const std::string &path);
191     static bool IsDirHidden(const std::string &path);
192     static void InitSkipList();
193     static bool CheckSkipScanList(const std::string &path);
194 
195 private:
196     static std::vector<size_t> skipList_;
197 };
198 } // namespace Media
199 } // namespace OHOS
200 
201 #endif // SCANNER_UTILS_H
202