2 * Copyright (C) 2005-2021 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
9 #include "PVRCachedImages.h"
11 #include "ServiceBroker.h"
12 #include "TextureCache.h"
13 #include "TextureDatabase.h"
15 #include "utils/StringUtils.h"
16 #include "utils/Variant.h"
17 #include "utils/log.h"
23 int CPVRCachedImages::Cleanup(const std::vector
<PVRImagePattern
>& urlPatterns
,
24 const std::vector
<std::string
>& urlsToCheck
,
25 bool clearTextureForPath
/* = false */)
27 int iCleanedImages
= 0;
29 if (urlPatterns
.empty())
31 CLog::LogFC(LOGERROR
, LOGPVR
, "No URL patterns given");
32 return iCleanedImages
;
38 CLog::LogFC(LOGERROR
, LOGPVR
, "Failed to open texture database");
39 return iCleanedImages
;
42 CDatabase::Filter filter
;
44 for (const auto& pattern
: urlPatterns
)
46 const std::string encodedPattern
=
47 StringUtils::Format("{}@{}", pattern
.owner
, CURL::Encode(pattern
.path
));
49 std::string escapedPattern
;
50 for (size_t i
= 0; i
< encodedPattern
.size(); ++i
)
52 if (encodedPattern
[i
] == '%' || encodedPattern
[i
] == '^')
53 escapedPattern
+= '^';
55 escapedPattern
+= encodedPattern
[i
];
58 const std::string where
=
59 StringUtils::Format("url LIKE 'image://{}%' ESCAPE '^'", escapedPattern
);
60 filter
.AppendWhere(where
, false); // logical OR
64 if (!db
.GetTextures(items
, filter
))
66 CLog::LogFC(LOGERROR
, LOGPVR
, "Failed to get items from texture database");
67 return iCleanedImages
;
70 for (unsigned int i
= 0; i
< items
.size(); ++i
)
72 // Unwrap the image:// URL returned from texture db.
73 const std::string textureURL
= UnwrapImageURL(items
[i
]["url"].asString());
75 if (std::none_of(urlsToCheck
.cbegin(), urlsToCheck
.cend(),
76 [&textureURL
](const std::string
& url
) { return url
== textureURL
; }))
78 CLog::LogFC(LOGDEBUG
, LOGPVR
, "Removing stale cached image: '{}'", textureURL
);
79 CServiceBroker::GetTextureCache()->ClearCachedImage(items
[i
]["textureid"].asInteger());
81 if (clearTextureForPath
)
82 db
.ClearTextureForPath(textureURL
, "thumb");
88 return iCleanedImages
;
91 std::string
CPVRCachedImages::UnwrapImageURL(const std::string
& url
)
93 return StringUtils::StartsWith(url
, "image://") ? CURL(url
).GetHostName() : url
;