summaryrefslogtreecommitdiffstats
path: root/xbmc/pvr/PVRCachedImages.cpp
blob: ea17fd769fca99f78006d580380943f287ba7c36 (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
91
92
93
94
/*
 *  Copyright (C) 2005-2021 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#include "PVRCachedImages.h"

#include "ServiceBroker.h"
#include "TextureCache.h"
#include "TextureDatabase.h"
#include "URL.h"
#include "utils/StringUtils.h"
#include "utils/Variant.h"
#include "utils/log.h"

#include <algorithm>

using namespace PVR;

int CPVRCachedImages::Cleanup(const std::vector<PVRImagePattern>& urlPatterns,
                              const std::vector<std::string>& urlsToCheck,
                              bool clearTextureForPath /* = false */)
{
  int iCleanedImages = 0;

  if (urlPatterns.empty())
  {
    CLog::LogFC(LOGERROR, LOGPVR, "No URL patterns given");
    return iCleanedImages;
  }

  CTextureDatabase db;
  if (!db.Open())
  {
    CLog::LogFC(LOGERROR, LOGPVR, "Failed to open texture database");
    return iCleanedImages;
  }

  CDatabase::Filter filter;

  for (const auto& pattern : urlPatterns)
  {
    const std::string encodedPattern =
        StringUtils::Format("{}@{}", pattern.owner, CURL::Encode(pattern.path));

    std::string escapedPattern;
    for (size_t i = 0; i < encodedPattern.size(); ++i)
    {
      if (encodedPattern[i] == '%' || encodedPattern[i] == '^')
        escapedPattern += '^';

      escapedPattern += encodedPattern[i];
    }

    const std::string where =
        StringUtils::Format("url LIKE 'image://{}%' ESCAPE '^'", escapedPattern);
    filter.AppendWhere(where, false); // logical OR
  }

  CVariant items;
  if (!db.GetTextures(items, filter))
  {
    CLog::LogFC(LOGERROR, LOGPVR, "Failed to get items from texture database");
    return iCleanedImages;
  }

  for (unsigned int i = 0; i < items.size(); ++i)
  {
    // Unwrap the image:// URL returned from texture db.
    const std::string textureURL = UnwrapImageURL(items[i]["url"].asString());

    if (std::none_of(urlsToCheck.cbegin(), urlsToCheck.cend(),
                     [&textureURL](const std::string& url) { return url == textureURL; }))
    {
      CLog::LogFC(LOGDEBUG, LOGPVR, "Removing stale cached image: '{}'", textureURL);
      CServiceBroker::GetTextureCache()->ClearCachedImage(items[i]["textureid"].asInteger());

      if (clearTextureForPath)
        db.ClearTextureForPath(textureURL, "thumb");

      iCleanedImages++;
    }
  }

  return iCleanedImages;
}

std::string CPVRCachedImages::UnwrapImageURL(const std::string& url)
{
  return StringUtils::StartsWith(url, "image://") ? CURL(url).GetHostName() : url;
}