• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2025 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 URI_HELPER_H
17 #define URI_HELPER_H
18 
19 #include <cstdint>
20 #include <string>
21 #include <string_view>
22 #include <map>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include "nocopyable.h"
26 
27 namespace OHOS {
28 namespace Media {
29 enum FdLocation : int32_t {
30     INVALID = 0,
31     LOCAL,
32     CLOUD,
33 };
34 
35 /**
36  * The simple utility is designed to facilitate the uri processing.
37  */
38 class __attribute__((visibility("default"))) UriHelper : public NoCopyable {
39 public:
40     enum UriType : uint8_t {
41         URI_TYPE_FILE,
42         URI_TYPE_FD,
43         URI_TYPE_HTTP,
44         URI_TYPE_UNKNOWN,
45     };
46 
47     enum UriAccessMode : uint8_t {
48         URI_READ = 1 << 0,
49         URI_WRITE = 1 << 1,
50     };
51 
52     explicit UriHelper(const std::string_view &uri);
53     UriHelper(int32_t fd, int64_t offset, int64_t size);
54     ~UriHelper();
55 
56     uint8_t UriType() const;
57     std::string FormattedUri() const;
58     bool AccessCheck(uint8_t flag) const;
59 
60     FdLocation GetFdLocation();
61 
62 private:
63     void FormatMeForUri(const std::string_view &uri) noexcept;
64     void FormatMeForFd() noexcept;
65     bool ParseFdUri(std::string_view uri);
66     bool CorrectFdParam();
67     void DetermineFdLocation();
68 
69     std::string formattedUri_ = "";
70     std::string_view rawFileUri_ = "";
71     uint8_t type_ = 0;
72     int32_t fd_ = -1;
73     int64_t offset_ = 0;
74     int64_t size_ = 0;
75     FdLocation fdLocation_ = FdLocation::INVALID;
76 };
77 } // namespace Media
78 } // namespace OHOS
79 
80 #endif