Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add ResourceRetriever::getFilePath() #970

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions dart/common/LocalResourceRetriever.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,7 @@ namespace common {
//==============================================================================
bool LocalResourceRetriever::exists(const Uri& _uri)
{
// Open and close the file to check if it exists. It would be more efficient
// to stat() it, but that is not portable.
if(_uri.mScheme.get_value_or("file") != "file")
return false;
else if (!_uri.mPath)
return false;

return std::ifstream(_uri.getFilesystemPath(), std::ios::binary).good();
return !getFilePath(_uri).empty();
}

//==============================================================================
Expand All @@ -70,5 +63,23 @@ common::ResourcePtr LocalResourceRetriever::retrieve(const Uri& _uri)
return nullptr;
}

//==============================================================================
std::string LocalResourceRetriever::getFilePath(const Uri& uri)
{
// Open and close the file to check if it exists. It would be more efficient
// to stat() it, but that is not portable.
if(uri.mScheme.get_value_or("file") != "file")
return "";
else if (!uri.mPath)
return "";

const auto path = uri.getFilesystemPath();

if (std::ifstream(path, std::ios::binary).good())
return path;
else
return "";
}

} // namespace common
} // namespace dart
3 changes: 3 additions & 0 deletions dart/common/LocalResourceRetriever.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ class LocalResourceRetriever : public virtual ResourceRetriever

// Documentation inherited.
ResourcePtr retrieve(const Uri& _uri) override;

// Documentation inherited.
std::string getFilePath(const Uri& uri) override;
};

using LocalResourceRetrieverPtr = std::shared_ptr<LocalResourceRetriever>;
Expand Down
6 changes: 6 additions & 0 deletions dart/common/ResourceRetriever.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,11 @@ std::string ResourceRetriever::readAll(const Uri& uri)
return resource->readAll();
}

//==============================================================================
std::string ResourceRetriever::getFilePath(const Uri& /*uri*/)
{
return "";
}

} // namespace common
} // namespace dart
19 changes: 12 additions & 7 deletions dart/common/ResourceRetriever.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ class ResourceRetriever
public:
virtual ~ResourceRetriever() = default;

/// \brief Return whether the resource specified by a URI exists.
/// Returns whether the resource specified by a URI exists.
virtual bool exists(const Uri& _uri) = 0;

/// \brief Return the resource specified by a URI or nullptr on failure.
/// Returns the resource specified by a URI or nullptr on failure.
virtual ResourcePtr retrieve(const Uri& _uri) = 0;

/// Reads all data from the resource of uri, and returns it as a string.
Expand All @@ -61,11 +61,16 @@ class ResourceRetriever
/// \throw std::runtime_error when failed to read sucessfully.
virtual std::string readAll(const Uri& uri);

// We don't const-qualify for exists, retrieve, and readAll here. Derived
// classes of ResourceRetriever will be interacting with external resources
// that you don't necessarily have control over so we cannot guarantee that
// you get the same result every time with the same input Uri. Indeed,
// const-qualification for those functions is pointless.
/// Returns absolute file path to \c uri; an empty string if unavailable.
///
/// This base class returns an empty string by default.
virtual std::string getFilePath(const Uri& uri);

// We don't const-qualify for exists, retrieve, readAll, and getFilePath here.
// Derived classes of ResourceRetriever will be interacting with external
// resources that you don't necessarily have control over so we cannot
// guarantee that you get the same result every time with the same input Uri.
// Indeed, const-qualification for those functions is pointless.
};

using ResourceRetrieverPtr = std::shared_ptr<ResourceRetriever>;
Expand Down
13 changes: 13 additions & 0 deletions dart/io/CompositeResourceRetriever.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,19 @@ common::ResourcePtr CompositeResourceRetriever::retrieve(
return nullptr;
}

//==============================================================================
std::string CompositeResourceRetriever::getFilePath(const common::Uri& uri)
{
for (const auto& resourceRetriever : getRetrievers(uri))
{
const auto path = resourceRetriever->getFilePath(uri);
if (!path.empty())
return path;
}

return "";
}

//==============================================================================
std::vector<common::ResourceRetrieverPtr>
CompositeResourceRetriever::getRetrievers(const common::Uri& _uri) const
Expand Down
3 changes: 3 additions & 0 deletions dart/io/CompositeResourceRetriever.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ class CompositeResourceRetriever : public virtual common::ResourceRetriever
// Documentation inherited.
common::ResourcePtr retrieve(const common::Uri& _uri) override;

// Documentation inherited.
std::string getFilePath(const common::Uri& uri) override;

private:
std::vector<common::ResourceRetrieverPtr> getRetrievers(
const common::Uri& _uri) const;
Expand Down
33 changes: 33 additions & 0 deletions dart/io/DartResourceRetriever.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,39 @@ common::ResourcePtr DartResourceRetriever::retrieve(const common::Uri& uri)
return nullptr;
}

//==============================================================================
std::string DartResourceRetriever::getFilePath(const common::Uri& uri)
{
std::string relativePath;
if (!resolveDataUri(uri, relativePath))
return "";

if (uri.mAuthority.get() == "sample")
{
for (const auto& dataPath : mDataDirectories)
{
common::Uri fileUri;
fileUri.fromPath(dataPath + relativePath);

const auto path = mLocalRetriever->getFilePath(fileUri);

// path is empty if the file specified by fileUri doesn't exist.
if (!path.empty())
return path;
}
}
else
{
const auto path = mLocalRetriever->getFilePath(uri);

// path is empty if the file specified by fileUri doesn't exist.
if (!path.empty())
return path;
}

return "";
}

//==============================================================================
void DartResourceRetriever::addDataDirectory(
const std::string& dataDirectory)
Expand Down
3 changes: 3 additions & 0 deletions dart/io/DartResourceRetriever.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ class DartResourceRetriever : public common::ResourceRetriever
// Documentation inherited.
common::ResourcePtr retrieve(const common::Uri& uri) override;

// Documentation inherited.
std::string getFilePath(const common::Uri& uri) override;

private:

void addDataDirectory(const std::string& packageDirectory);
Expand Down
30 changes: 20 additions & 10 deletions dart/io/PackageResourceRetriever.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,38 +68,48 @@ void PackageResourceRetriever::addPackageDirectory(

//==============================================================================
bool PackageResourceRetriever::exists(const common::Uri& _uri)
{
return !getFilePath(_uri).empty();
}

//==============================================================================
common::ResourcePtr PackageResourceRetriever::retrieve(const common::Uri& _uri)
{
std::string packageName, relativePath;
if (!resolvePackageUri(_uri, packageName, relativePath))
return false;
return nullptr;

for(const std::string& packagePath : getPackagePaths(packageName))
{
common::Uri fileUri;
fileUri.fromPath(packagePath + relativePath);

if (mLocalRetriever->exists(fileUri))
return true;
if(const auto resource = mLocalRetriever->retrieve(fileUri))
return resource;
}
return false;
return nullptr;
}

//==============================================================================
common::ResourcePtr PackageResourceRetriever::retrieve(const common::Uri& _uri)
std::string PackageResourceRetriever::getFilePath(const common::Uri& uri)
{
std::string packageName, relativePath;
if (!resolvePackageUri(_uri, packageName, relativePath))
return nullptr;
if (!resolvePackageUri(uri, packageName, relativePath))
return "";

for(const std::string& packagePath : getPackagePaths(packageName))
{
common::Uri fileUri;
fileUri.fromPath(packagePath + relativePath);

if(const auto resource = mLocalRetriever->retrieve(fileUri))
return resource;
const auto path = mLocalRetriever->getFilePath(fileUri);

// path is empty if the file specified by fileUri doesn't exist.
if (!path.empty())
return path;
}
return nullptr;

return "";
}

//==============================================================================
Expand Down
3 changes: 3 additions & 0 deletions dart/io/PackageResourceRetriever.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ class PackageResourceRetriever : public virtual common::ResourceRetriever
// Documentation inherited.
common::ResourcePtr retrieve(const common::Uri& _uri) override;

// Documentation inherited.
std::string getFilePath(const common::Uri& uri) override;

private:
common::ResourceRetrieverPtr mLocalRetriever;
std::unordered_map<std::string, std::vector<std::string> > mPackageMap;
Expand Down