Merge pull request #25808 from CastagnaIT/fix_url_parse
[xbmc.git] / xbmc / filesystem / ImageFile.cpp
bloba5a66e9a2a9561d55c67311a9243635ce7a01279
1 /*
2 * Copyright (C) 2012-2018 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.
7 */
9 #include "ImageFile.h"
11 #include "ServiceBroker.h"
12 #include "TextureCache.h"
13 #include "URL.h"
15 using namespace XFILE;
17 CImageFile::CImageFile(void) = default;
19 CImageFile::~CImageFile(void)
21 Close();
24 bool CImageFile::Open(const CURL& url)
26 std::string file = url.Get();
27 bool needsRecaching = false;
28 std::string cachedFile =
29 CServiceBroker::GetTextureCache()->CheckCachedImage(file, needsRecaching);
30 if (cachedFile.empty())
31 { // not in the cache, so cache it
32 cachedFile = CServiceBroker::GetTextureCache()->CacheImage(file);
34 if (!cachedFile.empty())
35 { // in the cache, return what we have
36 if (m_file.Open(cachedFile))
37 return true;
39 return false;
42 bool CImageFile::Exists(const CURL& url)
44 bool needsRecaching = false;
45 std::string cachedFile =
46 CServiceBroker::GetTextureCache()->CheckCachedImage(url.Get(), needsRecaching);
47 if (!cachedFile.empty())
49 if (CFile::Exists(cachedFile, false))
50 return true;
51 else
52 // Remove from cache so it gets cached again on next Open()
53 CServiceBroker::GetTextureCache()->ClearCachedImage(url.Get());
56 // check the original file if the image is not yet cached
57 return CFile::Exists(url.GetHostName());
60 int CImageFile::Stat(const CURL& url, struct __stat64* buffer)
62 bool needsRecaching = false;
63 std::string cachedFile =
64 CServiceBroker::GetTextureCache()->CheckCachedImage(url.Get(), needsRecaching);
65 if (!cachedFile.empty())
66 return CFile::Stat(cachedFile, buffer);
69 Doesn't exist in the cache yet. We have 3 options here:
70 1. Cache the file and do the Stat() on the cached file.
71 2. Do the Stat() on the original file.
72 3. Return -1;
73 Only 1 will return valid results, at the cost of being time consuming. ATM we do 3 under
74 the theory that the only user of this is the webinterface currently, where Stat() is not
75 required.
77 return -1;
80 ssize_t CImageFile::Read(void* lpBuf, size_t uiBufSize)
82 return m_file.Read(lpBuf, uiBufSize);
85 int64_t CImageFile::Seek(int64_t iFilePosition, int iWhence /*=SEEK_SET*/)
87 return m_file.Seek(iFilePosition, iWhence);
90 void CImageFile::Close()
92 m_file.Close();
95 int64_t CImageFile::GetPosition()
97 return m_file.GetPosition();
100 int64_t CImageFile::GetLength()
102 return m_file.GetLength();