Skip to content

Commit

Permalink
Merge pull request xbmc#1687 from jmarshallnz/fix_image_urls
Browse files Browse the repository at this point in the history
Fix embedded image urls
  • Loading branch information
jmarshallnz committed Oct 27, 2012
2 parents 1677554 + 8521e5e commit ecf53e8
Show file tree
Hide file tree
Showing 14 changed files with 119 additions and 32 deletions.
6 changes: 6 additions & 0 deletions project/VS2010Express/XBMC.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,12 @@
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release (DirectX)|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release (OpenGL)|Win32'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\xbmc\test\TestTextureCache.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug (DirectX)|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug (OpenGL)|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release (DirectX)|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release (OpenGL)|Win32'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\xbmc\test\TestUtils.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug (DirectX)|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug (OpenGL)|Win32'">true</ExcludedFromBuild>
Expand Down
3 changes: 3 additions & 0 deletions project/VS2010Express/XBMC.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -2933,6 +2933,9 @@
<ClCompile Include="..\..\xbmc\test\TestFileItem.cpp">
<Filter>test</Filter>
</ClCompile>
<ClCompile Include="..\..\xbmc\test\TestTextureCache.cpp">
<Filter>test</Filter>
</ClCompile>
<ClCompile Include="..\..\xbmc\interfaces\json-rpc\PVROperations.cpp">
<Filter>interfaces\json-rpc</Filter>
</ClCompile>
Expand Down
28 changes: 15 additions & 13 deletions xbmc/TextureCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,16 @@ CStdString CTextureCache::GetWrappedImageURL(const CStdString &image, const CStd
if (image.compare(0, 8, "image://") == 0)
return image; // already wrapped

CStdString encoded(image);
CURL::Encode(encoded);
CStdString url = "image://";
if (!type.IsEmpty())
url += type + "@";
url += encoded;
CURL url;
url.SetProtocol("image");
url.SetUserName(type);
url.SetHostName(image);
if (!options.IsEmpty())
url += "/transform?" + options;
return url;
{
url.SetFileName("transform");
url.SetOptions("?" + options);
}
return url.Get();
}

CStdString CTextureCache::GetWrappedThumbURL(const CStdString &image)
Expand All @@ -123,15 +124,16 @@ CStdString CTextureCache::UnwrapImageURL(const CStdString &image)
{
CURL url(image);
if (url.GetUserName().IsEmpty() && url.GetOptions().IsEmpty())
{
CStdString file(url.GetHostName());
CURL::Decode(file);
return file;
}
return url.GetHostName();
}
return image;
}

bool CTextureCache::CanCacheImageURL(const CURL &url)
{
return (url.GetUserName().empty() || url.GetUserName() == "music");
}

CStdString CTextureCache::CheckCachedImage(const CStdString &url, bool returnDDS, bool &needsRecaching)
{
CStdString cachedHash;
Expand Down
7 changes: 7 additions & 0 deletions xbmc/TextureCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "TextureDatabase.h"
#include "threads/Event.h"

class CURL;
class CBaseTexture;

/*!
Expand Down Expand Up @@ -134,6 +135,12 @@ class CTextureCache : public CJobQueue
*/
static CStdString UnwrapImageURL(const CStdString &image);

/*! \brief check whether an image:// URL may be cached
\param url the URL to the image
\return true if the given URL may be cached, false otherwise
*/
static bool CanCacheImageURL(const CURL &url);

/*! \brief Add this image to the database
Thread-safe wrapper of CTextureDatabase::AddCachedTexture
\param image url of the original image
Expand Down
12 changes: 4 additions & 8 deletions xbmc/TextureCacheJob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,12 @@ CStdString CTextureCacheJob::DecodeImageURL(const CStdString &url, unsigned int
// format is image://[type@]<url_encoded_path>?options
CURL thumbURL(url);

if (!thumbURL.GetUserName().IsEmpty())
{
if (thumbURL.GetUserName() == "music")
additional_info = "music";
else
return ""; // we don't re-cache special images (eg picturefolder/video embedded thumbs)
}
if (!CTextureCache::CanCacheImageURL(thumbURL))
return "";
if (thumbURL.GetUserName() == "music")
additional_info = "music";

image = thumbURL.GetHostName();
CURL::Decode(image);

CStdString optionString = thumbURL.GetOptions().Mid(1);
optionString.TrimRight('/'); // in case XBMC adds a slash
Expand Down
8 changes: 3 additions & 5 deletions xbmc/filesystem/ImageFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,10 @@ bool CImageFile::Exists(const CURL& url)
return CFile::Exists(cachedFile);

// need to check if the original can be cached on demand and that the file exists
if (!url.GetUserName().IsEmpty())
return false; // not in the cache, and can't be cached on demand
if (!CTextureCache::CanCacheImageURL(url))
return false;

CStdString image = url.GetHostName();
CURL::Decode(image);
return CFile::Exists(image);
return CFile::Exists(url.GetHostName());
}

int CImageFile::Stat(const CURL& url, struct __stat64* buffer)
Expand Down
13 changes: 13 additions & 0 deletions xbmc/music/MusicDatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3636,6 +3636,19 @@ bool CMusicDatabase::UpdateOldVersion(int version)
g_settings.Save();
}

if (version < 29)
{ // update old art URLs
m_pDS->query("select art_id,url from art where url like 'image://%%'");
vector< pair<int, string> > art;
while (!m_pDS->eof())
{
art.push_back(make_pair(m_pDS->fv(0).get_asInt(), CURL(m_pDS->fv(1).get_asString()).Get()));
m_pDS->next();
}
m_pDS->close();
for (vector< pair<int, string> >::iterator i = art.begin(); i != art.end(); ++i)
m_pDS->exec(PrepareSQL("update art set url='%s' where art_id=%d", i->second.c_str(), i->first));
}
// always recreate the views after any table change
CreateViews();

Expand Down
2 changes: 1 addition & 1 deletion xbmc/music/MusicDatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ class CMusicDatabase : public CDatabase
std::map<CStdString, CAlbum> m_albumCache;

virtual bool CreateTables();
virtual int GetMinVersion() const { return 28; };
virtual int GetMinVersion() const { return 29; };
const char *GetBaseDBName() const { return "MyMusic"; };

int AddSong(const CSong& song, bool bCheck = true, int idAlbum = -1);
Expand Down
4 changes: 1 addition & 3 deletions xbmc/network/httprequesthandler/HTTPImageHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ int CHTTPImageHandler::HandleHTTPRequest(const HTTPRequest &request)
m_path = request.url.substr(7);

XFILE::CImageFile imageFile;
if (imageFile.Exists(m_path) ||
// temporary workaround for music images until they are integrated into CTextureCache and therefore CImageFile
(m_path.Left(10) == "special://" && m_path.Right(4) == ".tbn" && XFILE::CFile::Exists(m_path)))
if (imageFile.Exists(m_path))
{
m_responseCode = MHD_HTTP_OK;
m_responseType = HTTPFileDownload;
Expand Down
1 change: 1 addition & 0 deletions xbmc/test/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
SRCS= \
TestBasicEnvironment.cpp \
TestFileItem.cpp \
TestTextureCache.cpp \
TestUtils.cpp \
xbmc-test.cpp

Expand Down
49 changes: 49 additions & 0 deletions xbmc/test/TestTextureCache.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (C) 2012 Team XBMC
* http://www.xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/

#include "URL.h"
#include "TextureCache.h"

#include "gtest/gtest.h"

TEST(TestTextureCache, GetWrappedImageURL)
{
typedef struct
{
const char *in;
const char *type;
const char *options;
const char *out;
} testfiles;

const testfiles test_files[] = {{ "/path/to/image/file.jpg", "", "", "image://%2fpath%2fto%2fimage%2ffile.jpg/" },
{ "/path/to/image/file.jpg", "", "size=thumb", "image://%2fpath%2fto%2fimage%2ffile.jpg/transform?size=thumb" },
{ "/path/to/video/file.mkv", "video", "", "image://video@%2fpath%2fto%2fvideo%2ffile.mkv/" },
{ "/path/to/music/file.mp3", "music", "", "image://music@%2fpath%2fto%2fmusic%2ffile.mp3/" },
{ "image://%2fpath%2fto%2fimage%2ffile.jpg/", "", "", "image://%2fpath%2fto%2fimage%2ffile.jpg/" },
{ "image://%2fpath%2fto%2fimage%2ffile.jpg/transform?size=thumb", "", "size=thumb", "image://%2fpath%2fto%2fimage%2ffile.jpg/transform?size=thumb" }};

for (unsigned int i = 0; i < sizeof(test_files) / sizeof(testfiles); i++)
{
std::string expected = test_files[i].out;
std::string out = CTextureCache::GetWrappedImageURL(test_files[i].in, test_files[i].type, test_files[i].options);
EXPECT_EQ(out, expected);
}
}
3 changes: 2 additions & 1 deletion xbmc/utils/URIUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@ bool URIUtils::ProtocolHasParentInHostname(const CStdString& prot)
bool URIUtils::ProtocolHasEncodedHostname(const CStdString& prot)
{
return ProtocolHasParentInHostname(prot)
|| prot.Equals("musicsearch");
|| prot.Equals("musicsearch")
|| prot.Equals("image");
}

bool URIUtils::ProtocolHasEncodedFilename(const CStdString& prot)
Expand Down
13 changes: 13 additions & 0 deletions xbmc/video/VideoDatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4111,6 +4111,19 @@ bool CVideoDatabase::UpdateOldVersion(int iVersion)
}
m_pDS->exec("DROP TABLE IF EXISTS setlinkmovie");
}
if (iVersion < 70)
{ // update old art URLs
m_pDS->query("select art_id,url from art where url like 'image://%%'");
vector< pair<int, string> > art;
while (!m_pDS->eof())
{
art.push_back(make_pair(m_pDS->fv(0).get_asInt(), CURL(m_pDS->fv(1).get_asString()).Get()));
m_pDS->next();
}
m_pDS->close();
for (vector< pair<int, string> >::iterator i = art.begin(); i != art.end(); ++i)
m_pDS->exec(PrepareSQL("update art set url='%s' where art_id=%d", i->second.c_str(), i->first));
}
// always recreate the view after any table change
CreateViews();
return true;
Expand Down
2 changes: 1 addition & 1 deletion xbmc/video/VideoDatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,7 @@ class CVideoDatabase : public CDatabase
*/
bool LookupByFolders(const CStdString &path, bool shows = false);

virtual int GetMinVersion() const { return 69; };
virtual int GetMinVersion() const { return 70; };
virtual int GetExportVersion() const { return 1; };
const char *GetBaseDBName() const { return "MyVideos"; };

Expand Down

0 comments on commit ecf53e8

Please sign in to comment.