Welcome to mirror list, hosted at ThFree Co, Russian Federation.

remote_file.cpp « platform - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 05263f1947d61e0196828fc358b40c6d4a8dde8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "platform/remote_file.hpp"

#include "platform/http_client.hpp"
#include "platform/platform.hpp"

#include "coding/file_writer.hpp"

#include "base/logging.hpp"

namespace platform
{
namespace
{
double constexpr kRequestTimeoutInSec = 5.0;
}  // namespace

RemoteFile::RemoteFile(std::string url, std::string accessToken /* = {} */,
                       HttpClient::Headers const & defaultHeaders /* = {} */,
                       bool allowRedirection /* = true */)
  : m_url(std::move(url))
  , m_accessToken(std::move(accessToken))
  , m_defaultHeaders(defaultHeaders)
  , m_allowRedirection(allowRedirection)
{}

RemoteFile::Result RemoteFile::Download(std::string const & filePath) const
{
  if (m_url.empty())
    return {m_url, Status::NetworkError, "Empty URL"};

  platform::HttpClient request(m_url);
  request.SetRawHeaders(m_defaultHeaders);
  request.SetTimeout(kRequestTimeoutInSec);
  if (!m_accessToken.empty())
    request.SetRawHeader("Authorization", "Bearer " + m_accessToken);
  if (request.RunHttpRequest())
  {
    if (!m_allowRedirection && request.WasRedirected())
      return {m_url, Status::NetworkError, "Unexpected redirection"};

    auto const & response = request.ServerResponse();
    int const resultCode = request.ErrorCode();
    if (resultCode == 403)
    {
      LOG(LWARNING, ("Access denied for", m_url, "response:", response));
      return {m_url, Status::Forbidden, resultCode, response};
    }
    else if (resultCode == 404)
    {
      LOG(LWARNING, ("File not found at", m_url, "response:", response));
      return {m_url, Status::NotFound, resultCode, response};
    }

    if (resultCode >= 200 && resultCode < 300)
    {
      try
      {
        FileWriter w(filePath);
        w.Write(response.data(), response.length());
      }
      catch (FileWriter::Exception const & exception)
      {
        LOG(LWARNING, ("Exception while writing file:", filePath, "reason:", exception.what()));
        return {m_url, Status::DiskError, resultCode, exception.what()};
      }
      return {m_url, Status::Ok, resultCode, ""};
    }
    return {m_url, Status::NetworkError, resultCode, response};
  }
  return {m_url, Status::NetworkError, "Unspecified network error"};
}

void RemoteFile::DownloadAsync(std::string const & filePath,
                               StartDownloadingHandler && startDownloadingHandler,
                               ResultHandler && resultHandler) const
{
  RemoteFile remoteFile = *this;
  GetPlatform().RunTask(Platform::Thread::Network,
                        [filePath, remoteFile = std::move(remoteFile),
                         startDownloadingHandler = std::move(startDownloadingHandler),
                         resultHandler = std::move(resultHandler)]
  {
    if (startDownloadingHandler)
      startDownloadingHandler(filePath);
    auto result = remoteFile.Download(filePath);
    if (resultHandler)
      resultHandler(std::move(result), filePath);
  });
}
}  // namespace platform