// Decentralized Media // // Douglas Thrift // // $Id$ #include #include #include #include #include #include #include #include #include #include #include "DecentralizedMedia.hpp" int Main(const app::Options& options) { api::Pcre::RegEx extension(_B("^-extension=(.+)$")), local(_B("^-local=(.+)$")), player_("^-player=(.+)$"); ext::RedBlackSet extensions; ext::RedBlackSet locals; int player(0); _foreach (const app::ArgumentList, arg, app::GetArguments()) { api::Pcre::RegEx::Match match; if (match = extension(*arg)) extensions.Insert(match[1]); else if (match = local(*arg)) locals.Insert(api::Path(match[1]).GetRealPath()); else if (match = player_(*arg)) player = lexical_cast(match[1]); } if (extensions.IsEmpty()) extensions.Insert(_B("mp3")); { api::Path media(_B("Media")); if (!media.Exists()) media.CreateDirectory(); locals.Insert(media.GetRealPath()); } _S media(extensions, locals, player); media.Block(); return 0; } DecentralizedMedia::DecentralizedMedia(const ext::RedBlackSet& extensions, const ext::RedBlackSet& locals, int player) : waf::Server(_B("Web")), connection(dbi::GetDriver("dbi::PgSql::Driver")->Connect("", "douglas", "", "media")), extensions(extensions), library(connection), player(connection, player) { { _L args; _foreach (const ext::RedBlackSet, extension, extensions) args.InsertLast(_S() << "-extension=" << *extension); _foreach (const ext::RedBlackSet, local, locals) args.InsertLast(_S() << "-local=" << *local); _S media(_B("Util/media"), args); Media(*media.GetReader()); media.Join(); } { _L args; args.InsertLast(_B("-s")); args.InsertLast(_B("object/DecentralizedMedia.hh.gch")); _S make(CFG_GNU_MAKE, args); make.ClearWriter(); _S error; ios::ReadToWrite(*make.GetReader(), error); if (make.Join() != 0) throw ext::StringException(error); } AddPort(6996); } void DecentralizedMedia::Process(const net::Http::Request& request, net::Http::Response& response) { if (request.method_ == _B("EXTENSIONS")) { api::Cout << "EXTENSIONS" << ios::NewLine; response.SetStatus(200); _foreach (const ext::RedBlackSet, extension, extensions) response << *extension << ios::NewLineNoFlush; } else if (request.method_ == _B("MEDIA")) { api::Cout << "MEDIA" << ios::NewLine; if (!request.content_.IsEmpty()) { api::InternetAddress host(request.client_); host.SetPort(6996); Media(*request.content_, host); response.SetStatus(204); } else response.SetStatus(411); } else waf::Server::Process(request, response); } void DecentralizedMedia::Media(ios::Reader& media, const api::InternetAddress& host) { _R document(xml::Parse(media)); api::Pcre::RegEx share("^[0-9a-f]+$"); _foreach (const xml::NodeSet, folder, *document/"media"/"folder") { cse::String path(**folder/"path"); if (api::Pcre::RegEx::Match match = share(path)) { _R share(new _H(connection, host, match[0])); path = share->path.GetPath(); player.Add(share); } Media(*folder, path, path); } } void DecentralizedMedia::Media(const _R& folder, const api::Path& path, const api::Path& root) { MediaFolder(connection, path, root); _foreach (const xml::NodeSet, file, *folder/"file") MediaFile(connection, path.GetChild(**file/"path"), **file/"artist", **file/"title", **file/"album", **file/"genre", root); _foreach (const xml::NodeSet, folder_, *folder/"folder") Media(*folder_, path.GetChild(**folder_/"path"), root); }