• 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 const int64_t FILE_TIME_PENDING_DEFAULT = 0;
70 const std::string FILE_All_EXIF_DEFAULT = "";
71 const std::string FILE_USER_COMMENT_DEFAULT = "";
72 
73 const std::string DEFAULT_AUDIO_MIME_TYPE = "audio/*";
74 const std::string DEFAULT_VIDEO_MIME_TYPE = "video/*";
75 const std::string DEFAULT_IMAGE_MIME_TYPE = "image/*";
76 const std::string DEFAULT_FILE_MIME_TYPE = "file/*";
77 
78 static std::vector<std::string> EXTRACTOR_SUPPORTED_MIME = {
79     DEFAULT_AUDIO_MIME_TYPE,
80     DEFAULT_VIDEO_MIME_TYPE,
81     DEFAULT_IMAGE_MIME_TYPE
82 };
83 
84 static const std::unordered_map<std::string, std::string> SUPPORTED_EXTN_MAP = {
85     /** Supported image types */
86     {IMAGE_CONTAINER_TYPE_BMP, DEFAULT_IMAGE_MIME_TYPE},
87     {IMAGE_CONTAINER_TYPE_BM, DEFAULT_IMAGE_MIME_TYPE},
88     {IMAGE_CONTAINER_TYPE_GIF, DEFAULT_IMAGE_MIME_TYPE},
89     {IMAGE_CONTAINER_TYPE_JPG, DEFAULT_IMAGE_MIME_TYPE},
90     {IMAGE_CONTAINER_TYPE_JPEG, DEFAULT_IMAGE_MIME_TYPE},
91     {IMAGE_CONTAINER_TYPE_JPE, DEFAULT_IMAGE_MIME_TYPE},
92     {IMAGE_CONTAINER_TYPE_PNG, DEFAULT_IMAGE_MIME_TYPE},
93     {IMAGE_CONTAINER_TYPE_WEBP, DEFAULT_IMAGE_MIME_TYPE},
94     {IMAGE_CONTAINER_TYPE_RAW, DEFAULT_IMAGE_MIME_TYPE},
95     {IMAGE_CONTAINER_TYPE_SVG, DEFAULT_IMAGE_MIME_TYPE},
96     {IMAGE_CONTAINER_TYPE_HEIF, DEFAULT_IMAGE_MIME_TYPE},
97     /** Supported video container types */
98     {VIDEO_CONTAINER_TYPE_MP4, DEFAULT_VIDEO_MIME_TYPE},
99     {VIDEO_CONTAINER_TYPE_3GP, DEFAULT_VIDEO_MIME_TYPE},
100     {VIDEO_CONTAINER_TYPE_MPG, DEFAULT_VIDEO_MIME_TYPE},
101     {VIDEO_CONTAINER_TYPE_MOV, DEFAULT_VIDEO_MIME_TYPE},
102     {VIDEO_CONTAINER_TYPE_WEBM, DEFAULT_VIDEO_MIME_TYPE},
103     {VIDEO_CONTAINER_TYPE_MKV, DEFAULT_VIDEO_MIME_TYPE},
104     {AUDIO_CONTAINER_TYPE_AAC, DEFAULT_AUDIO_MIME_TYPE},
105     {AUDIO_CONTAINER_TYPE_MP3, DEFAULT_AUDIO_MIME_TYPE},
106     {AUDIO_CONTAINER_TYPE_FLAC, DEFAULT_AUDIO_MIME_TYPE},
107     {AUDIO_CONTAINER_TYPE_WAV, DEFAULT_AUDIO_MIME_TYPE},
108     {AUDIO_CONTAINER_TYPE_OGG, DEFAULT_AUDIO_MIME_TYPE},
109     {"7z", "application/x-7z-compressed"},
110     {"bz", "application/x-bzip"},
111     {"bz2", "application/x-bzip2"},
112     {"cer", "application/pkix-cert"},
113     {"clp", "application/x-msclip"},
114     {"crl", "application/pkix-crl"},
115     {"css", "text/css"},
116     {"csv", "text/csv"},
117     {"der", "application/x-x509-ca-cert"},
118     {"doc", "application/msword"},
119     {"docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
120     {"exe", "application/x-msdownload"},
121     {"gtar", "application/x-gtar"},
122     {"gz", "application/gzip"},
123     {"html", "text/html"},
124     {"ics", "text/calendar"},
125     {"ipfix", "application/ipfix"},
126     {"jar", "application/java-archive"},
127     {"json", "application/json"},
128     {"mdb", "application/x-msaccess"},
129     {"nsf", "application/vnd.lotus-notes"},
130     {"odb", "application/vnd.oasis.opendocument.database"},
131     {"odc", "application/vnd.oasis.opendocument.chart"},
132     {"odp", "application/vnd.oasis.opendocument.presentation"},
133     {"ods", "application/vnd.oasis.opendocument.spreadsheet"},
134     {"odt", "application/vnd.oasis.opendocument.text"},
135     {"p10", "application/pkcs10"},
136     {"p12", "application/x-pkcs12"},
137     {"p7b", "application/x-pkcs7-certificates"},
138     {"p7m", "application/pkcs7-mime"},
139     {"p7r", "application/x-pkcs7-certreqresp"},
140     {"p7s", "application/pkcs7-signature"},
141     {"p8", "application/pkcs8"},
142     {"pdf", "application/pdf"},
143     {"pki", "application/pkixcmp"},
144     {"pkipath", "application/pkix-pkipath"},
145     {"ppt", "application/vnd.ms-powerpoint"},
146     {"pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"},
147     {"rar", "application/x-rar-compressed"},
148     {"rss", "application/rss+xml"},
149     {"rtf", "application/rtf"},
150     {"rtx", "text/richtext"},
151     {"sxc", "application/vnd.sun.xml.calc"},
152     {"sxg", "application/vnd.sun.xml.writer.global"},
153     {"sxi", "application/vnd.sun.xml.impress"},
154     {"sxw", "application/vnd.sun.xml.writer"},
155     {"tar", "application/x-tar"},
156     {"texinfo", "application/x-texinfo"},
157     {"torrent", "application/x-bittorrent"},
158     {"txt", "text/plain"},
159     {"vcf", "text/x-vcard"},
160     {"vcs", "text/x-vcalendar"},
161     {"vsd", "application/vnd.visio"},
162     {"vsdx", "application/vnd.visio2013"},
163     {"vxml", "application/voicexml+xml"},
164     {"wml", "text/vnd.wap.wml"},
165     {"wmls", "text/vnd.wap.wmlscript"},
166     {"wps", "application/vnd.ms-works"},
167     {"wri", "application/x-mswrite"},
168     {"xdf", "application/xcap-diff+xml"},
169     {"xhtml", "application/xhtml+xml"},
170     {"xls", "application/vnd.ms-excel"},
171     {"xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
172     {"xml", "application/xml"},
173     {"xps", "application/vnd.ms-xpsdocument"},
174     {"yaml", "text/yaml"},
175     {"zip", "application/zip"},
176 };
177 
178 class ScannerUtils {
179 public:
180     ScannerUtils();
181     ~ScannerUtils();
182 
183     static bool IsExists(const std::string &path);
184     static std::string GetFileNameFromUri(const std::string &path);
185     static std::string GetFileExtension(const std::string &path);
186     static std::string GetParentPath(const std::string &path);
187     static bool IsFileHidden(const std::string &path);
188     static bool IsDirectory(const std::string &path);
189     static bool IsRegularFile(const std::string &path);
190     static void GetRootMediaDir(std::string &dir);
191     static std::string GetFileTitle(const std::string &displayName);
192     static bool IsDirHiddenRecursive(const std::string &path);
193     static bool IsDirHidden(const std::string &path);
194     static void InitSkipList();
195     static bool CheckSkipScanList(const std::string &path);
196 
197 private:
198     static std::vector<size_t> skipList_;
199 };
200 } // namespace Media
201 } // namespace OHOS
202 
203 #endif // SCANNER_UTILS_H
204