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 struct FdDesInfo { 30 int32_t fd = 0; 31 int64_t offset = 0; 32 int64_t size = 0; 33 }; 34 35 enum FdLocation : int32_t { 36 INVALID = 0, 37 LOCAL, 38 CLOUD, 39 }; 40 41 /** 42 * The simple utility is designed to facilitate the uri processing. 43 */ 44 class __attribute__((visibility("default"))) UriHelper : public NoCopyable { 45 public: 46 enum UriType : uint8_t { 47 URI_TYPE_FILE, 48 URI_TYPE_FD, 49 URI_TYPE_HTTP, 50 URI_TYPE_UNKNOWN, 51 }; 52 53 enum UriAccessMode : uint8_t { 54 URI_READ = 1 << 0, 55 URI_WRITE = 1 << 1, 56 }; 57 58 explicit UriHelper(const std::string_view &uri); 59 UriHelper(int32_t fd, int64_t offset, int64_t size); 60 ~UriHelper(); 61 62 uint8_t UriType() const; 63 std::string FormattedUri() const; 64 bool AccessCheck(uint8_t flag) const; 65 GetFdInfo()66 FdDesInfo GetFdInfo() 67 { 68 return { .fd = fd_, .offset = offset_, .size = size_ }; 69 } 70 71 FdLocation GetFdLocation(); 72 73 private: 74 void FormatMeForUri(const std::string_view &uri) noexcept; 75 void FormatMeForFd() noexcept; 76 bool ParseFdUri(std::string_view uri); 77 bool CorrectFdParam(); 78 void DetermineFdLocation(); 79 80 std::string formattedUri_ = ""; 81 std::string_view rawFileUri_ = ""; 82 uint8_t type_ = 0; 83 int32_t fd_ = -1; 84 int64_t offset_ = 0; 85 int64_t size_ = 0; 86 FdLocation fdLocation_ = FdLocation::INVALID; 87 }; 88 } // namespace Media 89 } // namespace OHOS 90 91 #endif