ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
download.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <cstdint>
11 #include <filesystem>
12 #include <functional>
13 #include <optional>
14 #include <string>
15 #include <string_view>
16 
17 namespace colmap {
18 
19 // Detect if given string is a URI
20 // (i.e., starts with http://, https://, file://).
21 bool IsURI(const std::string& uri);
22 
23 #ifdef COLMAP_DOWNLOAD_ENABLED
24 
25 // Progress callback function type: (downloaded_bytes, total_bytes) -> void
26 // If total_bytes is 0, the total size is unknown.
27 // Using int64_t instead of curl_off_t to avoid including curl headers in the
28 // public interface
29 using DownloadProgressCallback =
30  std::function<void(int64_t downloaded, int64_t total)>;
31 
32 // Download file from server. Supports any protocol supported by Curl.
33 // Automatically follows redirects. Returns null in case of failure. Notice that
34 // this function is not suitable for large files that don't fit easily into
35 // memory. If such a use case emerges in the future, we want to instead stream
36 // the downloaded data chunks to disk instead of accumulating them in memory.
37 // progress_callback: Optional callback function to report download progress.
38 std::optional<std::string> DownloadFile(
39  const std::string& url,
40  DownloadProgressCallback progress_callback = nullptr);
41 
42 // Computes SHA256 digest for given string.
43 std::string ComputeSHA256(const std::string_view& str);
44 
45 // Downloads and caches file from given URI. The URI must take the format
46 // "<url>;<name>;<sha256>". The file will be cached under
47 // $HOME/.cache/colmap/<sha256>-<name>. File integrity is checked against the
48 // provided SHA256 digest. Throws exception if the digest does not match.
49 // Returns the path to the cached file.
50 // progress_callback: Optional callback function to report download progress.
51 std::string DownloadAndCacheFile(
52  const std::string& uri,
53  DownloadProgressCallback progress_callback = nullptr);
54 
55 // Overwrites the default download cache directory at $HOME/.cache/colmap/.
56 void OverwriteDownloadCacheDir(std::filesystem::path path);
57 
58 #endif // COLMAP_DOWNLOAD_ENABLED
59 
60 // Check if a URI would be cached (i.e., if the cached file already exists).
61 // Returns the cache path if the file exists, empty path otherwise.
62 // Only works for URI format: "<url>;<name>;<sha256>"
63 std::filesystem::path GetCachedFilePath(const std::string& uri);
64 
65 // If the given URI is a local filesystem path, returns the input path. If the
66 // URI matches the "<url>;<name>;<sha256>" format, calls DownloadAndCacheFile().
67 // Throws runtime exception if download is not supported.
69 
70 } // namespace colmap
static const std::string path
Definition: PointCloud.cpp:59
bool IsURI(const std::string &uri)
std::filesystem::path GetCachedFilePath(const std::string &uri)
std::filesystem::path MaybeDownloadAndCacheFile(const std::string &uri)