diff options
-rw-r--r-- | httpd.cpp | 2 | ||||
-rw-r--r-- | music.cpp | 54 | ||||
-rw-r--r-- | music.h | 14 |
3 files changed, 26 insertions, 44 deletions
@@ -22,7 +22,7 @@ void HTTPConnection::handle_read(const boost::system::error_code& error, size_t HTTPResponse res(socket); - MusicListing *ml = music::find(req.path); + MusicListing::p ml = music::get(req.path); if(ml) { res.code = 200; res.status = "OK"; @@ -9,40 +9,28 @@ namespace music { -MusicDirectory *root_directory = NULL; +fs::path root_directory; void init(std::string root) { // remove trailing slash if(boost::algorithm::ends_with(root, "/")) root = root.substr(0, root.size()-1); - root_directory = new MusicDirectory(root); + root_directory = root; } -MusicListing *find(std::string path, MusicDirectory& root) { - // remove trailing slash - if(boost::algorithm::ends_with(path, "/")) - path = path.substr(0, path.size()-1); - - fs::path full_path = root_directory->path / path; - if(root.path == full_path) - return &root; +MusicListing::p get(const std::string path) { + // prefix path with our root_directory + fs::path p = root_directory / path; - for(MusicDirectories::iterator it = root.directories.begin(); it != root.directories.end(); it++) { - MusicListing *ml = find(path, *it); - if(ml) return ml; - } - for(MusicTracks::iterator it = root.tracks.begin(); it != root.tracks.end(); it++) { - std::string rel_path = it->path.string().substr(root_directory->path.string().size()); - std::cout << rel_path << std::endl; - if(rel_path == path) - return &(*it); + if(fs::is_directory(p)) { + boost::shared_ptr<MusicListing> ml(new MusicDirectory(p)); + return ml; + } else if(fs::is_regular_file(p)) { + boost::shared_ptr<MusicListing> ml(new MusicTrack(p)); + return ml; } - return NULL; -} - -MusicListing *find(std::string path) { - return find(path, *root_directory); + return MusicListing::p(); } }; // namespace music @@ -50,12 +38,12 @@ MusicListing *find(std::string path) { void MusicDirectory::render(HTTPRequest& req, HTTPResponse& res) { res.add_header("content-type", "text/html"); - for(MusicDirectories::iterator it = directories.begin(); it != directories.end(); it++) { - std::string rel_path = it->path.string().substr(music::root_directory->path.string().size()); + for(PathListings::iterator it = directories.begin(); it != directories.end(); it++) { + std::string rel_path = it->string().substr(music::root_directory.string().size()); res.write(boost::str(boost::format("<a href=\"%s\">%s</a><br />") % rel_path % rel_path)); } - for(MusicTracks::iterator it = tracks.begin(); it != tracks.end(); it++) { - std::string rel_path = it->path.string().substr(music::root_directory->path.string().size()); + for(PathListings::iterator it = tracks.begin(); it != tracks.end(); it++) { + std::string rel_path = it->string().substr(music::root_directory.string().size()); res.write(boost::str(boost::format("<a href=\"%s\">%s</a><br />") % rel_path % rel_path)); } } @@ -67,16 +55,14 @@ MusicTrack::MusicTrack(const fs::path path) { MusicDirectory::MusicDirectory(const fs::path root) { this->path = root; - std::cout << root << std::endl; - - fs::path p(root); + std::cout << this->path << std::endl; fs::directory_iterator end_itr; - for(fs::directory_iterator it(p); it != end_itr; it++) { + for(fs::directory_iterator it(this->path); it != end_itr; it++) { if(fs::is_directory(it->status())) { - directories.push_back(MusicDirectory(it->path().string())); + directories.push_back(it->path()); } else if(fs::is_regular_file(it->status())) { - tracks.push_back(MusicTrack(it->path().string())); + tracks.push_back(it->path()); } } } @@ -11,6 +11,7 @@ namespace fs = boost::filesystem; class MusicListing { public: + typedef boost::shared_ptr<MusicListing> p; fs::path path; virtual void render(HTTPRequest& req, HTTPResponse& res) = 0; }; @@ -21,24 +22,19 @@ class MusicTrack : public MusicListing { virtual void render(HTTPRequest& req, HTTPResponse& res); }; -class MusicDirectory; -typedef std::vector<MusicDirectory> MusicDirectories; -typedef std::vector<MusicTrack> MusicTracks; - class MusicDirectory : public MusicListing { public: - MusicDirectories directories; - MusicTracks tracks; + typedef std::vector<fs::path> PathListings; + PathListings directories; + PathListings tracks; MusicDirectory(const fs::path root); virtual void render(HTTPRequest& req, HTTPResponse& res); }; namespace music { - extern MusicDirectory *root_directory; void init(std::string root); - MusicListing *find(std::string path, MusicDirectory& root); - MusicListing *find(std::string path); + MusicListing::p get(const std::string path); }; #endif |