Skip to content

Commit

Permalink
filesystem: add support for resource:// paths
Browse files Browse the repository at this point in the history
  • Loading branch information
Montellese committed Mar 6, 2015
1 parent d96e69c commit afe4c91
Show file tree
Hide file tree
Showing 7 changed files with 230 additions and 0 deletions.
2 changes: 2 additions & 0 deletions xbmc/filesystem/DirectoryFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
#if defined(TARGET_ANDROID)
#include "AndroidAppDirectory.h"
#endif
#include "ResourceDirectory.h"

using namespace XFILE;

Expand Down Expand Up @@ -242,6 +243,7 @@ IDirectory* CDirectoryFactory::Create(const CURL& url)
#ifdef HAVE_LIBBLURAY
if (url.IsProtocol("bluray")) return new CBlurayDirectory();
#endif
if (url.IsProtocol("resource")) return new CResourceDirectory();
}

CLog::Log(LOGWARNING, "%s - %sunsupported protocol(%s) in %s", __FUNCTION__, networkAvailable ? "" : "Network down or ", url.GetProtocol().c_str(), url.GetRedacted().c_str() );
Expand Down
2 changes: 2 additions & 0 deletions xbmc/filesystem/FileFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
#include "HDHomeRunFile.h"
#include "SlingboxFile.h"
#include "ImageFile.h"
#include "ResourceFile.h"
#include "Application.h"
#include "URL.h"
#include "utils/log.h"
Expand Down Expand Up @@ -213,6 +214,7 @@ IFile* CFileFactory::CreateLoader(const CURL& url)
#ifdef HAVE_LIBBLURAY
else if (url.IsProtocol("bluray")) return new CBlurayFile();
#endif
else if (url.IsProtocol("resource")) return new CResourceFile();
}

CLog::Log(LOGWARNING, "%s - %sunsupported protocol(%s) in %s", __FUNCTION__, networkAvailable ? "" : "Network down or ", url.GetProtocol().c_str(), url.GetRedacted().c_str());
Expand Down
2 changes: 2 additions & 0 deletions xbmc/filesystem/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ SRCS += posix/PosixDirectory.cpp
SRCS += posix/PosixFile.cpp
SRCS += PVRFile.cpp
SRCS += PVRDirectory.cpp
SRCS += ResourceDirectory.cpp
SRCS += ResourceFile.cpp
SRCS += RSSDirectory.cpp
SRCS += RTVDirectory.cpp
SRCS += RTVFile.cpp
Expand Down
67 changes: 67 additions & 0 deletions xbmc/filesystem/ResourceDirectory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (C) 2014 Team XBMC
* http://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 "ResourceDirectory.h"
#include "FileItem.h"
#include "URL.h"
#include "filesystem/Directory.h"
#include "filesystem/ResourceFile.h"
#include "utils/StringUtils.h"
#include "utils/URIUtils.h"

using namespace XFILE;

CResourceDirectory::CResourceDirectory()
{ }

CResourceDirectory::~CResourceDirectory()
{ }

bool CResourceDirectory::GetDirectory(const CURL& url, CFileItemList &items)
{
const std::string pathToUrl(url.Get());
std::string translatedPath;
if (!CResourceFile::TranslatePath(url, translatedPath))
return false;

if (CDirectory::GetDirectory(translatedPath, items, m_strFileMask, m_flags | DIR_FLAG_GET_HIDDEN))
{ // replace our paths as necessary
items.SetURL(url);
for (int i = 0; i < items.Size(); i++)
{
CFileItemPtr item = items[i];
if (StringUtils::StartsWith(item->GetPath(), translatedPath))
item->SetPath(URIUtils::AddFileToFolder(pathToUrl, item->GetPath().substr(translatedPath.size())));
}

return true;
}

return false;
}

std::string CResourceDirectory::TranslatePath(const CURL &url)
{
std::string translatedPath;
if (!CResourceFile::TranslatePath(url, translatedPath))
return "";

return translatedPath;
}
37 changes: 37 additions & 0 deletions xbmc/filesystem/ResourceDirectory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#pragma once
/*
* Copyright (C) 2014 Team XBMC
* http://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 "filesystem/OverrideDirectory.h"

namespace XFILE
{
class CResourceDirectory : public COverrideDirectory
{
public:
CResourceDirectory();
virtual ~CResourceDirectory();

virtual bool GetDirectory(const CURL& url, CFileItemList &items);

protected:
virtual std::string TranslatePath(const CURL &url);
};
}
82 changes: 82 additions & 0 deletions xbmc/filesystem/ResourceFile.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright (C) 2014 Team XBMC
* http://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 "ResourceFile.h"
#include "URL.h"
#include "Util.h"
#include "addons/AddonManager.h"
#include "addons/Resource.h"
#include "utils/StringUtils.h"
#include "utils/URIUtils.h"

#include <sys/stat.h>

using namespace ADDON;
using namespace XFILE;

CResourceFile::CResourceFile()
: COverrideFile(false)
{ }

CResourceFile::~CResourceFile()
{ }

bool CResourceFile::TranslatePath(const std::string &path, std::string &translatedPath)
{
return TranslatePath(CURL(path), translatedPath);
}

bool CResourceFile::TranslatePath(const CURL &url, std::string &translatedPath)
{
translatedPath = url.Get();

// only handle resource:// paths
if (!url.IsProtocol("resource"))
return false;

// the share name represents an identifier that can be mapped to an addon ID
std::string addonId = url.GetHostName();
if (addonId.empty())
return false;

AddonPtr addon;
if (!CAddonMgr::Get().GetAddon(addonId, addon, ADDON_UNKNOWN, true) || addon == NULL)
return false;

std::shared_ptr<CResource> resource = std::dynamic_pointer_cast<ADDON::CResource>(addon);
if (resource == NULL)
return false;

std::string filePath = url.GetFileName();
if (!resource->IsAllowed(filePath))
return false;

translatedPath = CUtil::ValidatePath(URIUtils::AddFileToFolder(addon->Path(), "resources/" + filePath));
return true;
}

std::string CResourceFile::TranslatePath(const CURL &url)
{
std::string translatedPath;
if (!TranslatePath(url, translatedPath))
return "";

return translatedPath;
}
38 changes: 38 additions & 0 deletions xbmc/filesystem/ResourceFile.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#pragma once
/*
* Copyright (C) 2014 Team XBMC
* http://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 "filesystem/OverrideFile.h"

namespace XFILE
{
class CResourceFile : public COverrideFile
{
public:
CResourceFile();
virtual ~CResourceFile();

static bool TranslatePath(const std::string &path, std::string &translatedPath);
static bool TranslatePath(const CURL &url, std::string &translatedPath);

protected:
virtual std::string TranslatePath(const CURL &url);
};
}

0 comments on commit afe4c91

Please sign in to comment.