1 /*
2  * Copyright (c) 2024 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 #include "file_uri.h"
17 
18 #include <unistd.h>
19 
20 #include "uri.h"
21 
22 #include "common_func.h"
23 #include "log.h"
24 #include "sandbox_helper.h"
25 
26 using namespace std;
27 
28 namespace OHOS {
29 namespace CJSystemapi {
30 namespace FileUri {
31 
32 const std::string FILE_SCHEME_PREFIX = "file://";
33 const std::string MEDIA_AUTHORITY = "media";
GetName()34 string FileUriImpl::GetName()
35 {
36     string sandboxPath = AppFileService::SandboxHelper::Decode(uri_.GetPath());
37     size_t posLast = sandboxPath.find_last_of("/");
38     if (posLast == string::npos) {
39         return "";
40     }
41 
42     if (posLast == sandboxPath.size()) {
43         return "";
44     }
45 
46     return sandboxPath.substr(posLast + 1);
47 }
48 
GetPath()49 string FileUriImpl::GetPath()
50 {
51     string sandboxPath = AppFileService::SandboxHelper::Decode(uri_.GetPath());
52     string bundleName = uri_.GetAuthority();
53     if (bundleName == MEDIA_AUTHORITY && sandboxPath.find(".") != string::npos) {
54         size_t pos = sandboxPath.rfind("/");
55         return sandboxPath.substr(0, pos);
56     }
57 
58     return sandboxPath;
59 }
60 
ToString()61 string FileUriImpl::ToString()
62 {
63     return uri_.ToString();
64 }
65 
FileUriImpl(const string & uriOrPath)66 FileUriImpl::FileUriImpl(const string &uriOrPath): uri_(
67     (uriOrPath.find(FILE_SCHEME_PREFIX) == 0) ? uriOrPath : AppFileService::CommonFunc::GetUriFromPath(uriOrPath)
68 )
69 {}
70 
71 }
72 }  // namespace CJSystemapi
73 }  // namespace OHOS