1 |
douglas |
464 |
// Decentralized Media |
2 |
|
|
// |
3 |
|
|
// Douglas Thrift |
4 |
|
|
// |
5 |
|
|
// $Id$ |
6 |
|
|
|
7 |
douglas |
558 |
#include <menes/cxx/standard.hh> |
8 |
douglas |
464 |
|
9 |
douglas |
549 |
#include <menes/api/pcre/regex.hpp> |
10 |
|
|
#include <menes/app/simple.hpp> |
11 |
|
|
#include <menes/dbi/driver.hpp> |
12 |
|
|
#include <menes/hop/bind.hpp> |
13 |
|
|
#include <menes/ios/helpers.hpp> |
14 |
|
|
#include <menes/net/http/request.hpp> |
15 |
|
|
#include <menes/net/http/response.hpp> |
16 |
|
|
#include <menes/xml/document.hpp> |
17 |
|
|
#include <menes/xml/parse.hpp> |
18 |
douglas |
470 |
|
19 |
douglas |
471 |
#include "DecentralizedMedia.hpp" |
20 |
douglas |
464 |
|
21 |
douglas |
471 |
int Main(const app::Options& options) |
22 |
douglas |
464 |
{ |
23 |
douglas |
555 |
api::Pcre::RegEx configuration_(_B("^-configuration=(.+)$")), extension(_B("^-extension=(.+)$")), local(_B("^-local=(.+)$")), player_("^-player=(.+)$"); |
24 |
|
|
cse::String configuration(_B("DecentralizedMedia.xml")); |
25 |
douglas |
534 |
ext::RedBlackSet<cse::String> extensions; |
26 |
douglas |
529 |
ext::RedBlackSet<api::Path> locals; |
27 |
douglas |
542 |
int player(0); |
28 |
douglas |
464 |
|
29 |
douglas |
474 |
_foreach (const app::ArgumentList, arg, app::GetArguments()) |
30 |
|
|
{ |
31 |
|
|
api::Pcre::RegEx::Match match; |
32 |
|
|
|
33 |
douglas |
555 |
if (match = configuration_(*arg)) |
34 |
|
|
configuration = match[1]; |
35 |
|
|
else if (match = extension(*arg)) |
36 |
douglas |
491 |
extensions.Insert(match[1]); |
37 |
douglas |
474 |
else if (match = local(*arg)) |
38 |
douglas |
529 |
locals.Insert(api::Path(match[1]).GetRealPath()); |
39 |
douglas |
542 |
else if (match = player_(*arg)) |
40 |
|
|
player = lexical_cast<int>(match[1]); |
41 |
douglas |
474 |
} |
42 |
|
|
|
43 |
|
|
if (extensions.IsEmpty()) |
44 |
douglas |
491 |
extensions.Insert(_B("mp3")); |
45 |
douglas |
474 |
|
46 |
douglas |
529 |
{ |
47 |
|
|
api::Path media(_B("Media")); |
48 |
|
|
|
49 |
|
|
if (!media.Exists()) |
50 |
|
|
media.CreateDirectory(); |
51 |
|
|
|
52 |
|
|
locals.Insert(media.GetRealPath()); |
53 |
|
|
} |
54 |
douglas |
491 |
|
55 |
douglas |
555 |
_S<DecentralizedMedia> media(configuration, extensions, locals, player); |
56 |
douglas |
474 |
|
57 |
douglas |
472 |
media.Block(); |
58 |
|
|
|
59 |
douglas |
471 |
return 0; |
60 |
|
|
} |
61 |
douglas |
469 |
|
62 |
douglas |
555 |
DecentralizedMedia::DecentralizedMedia(const cse::String& configuration, const ext::RedBlackSet<cse::String>& extensions, const ext::RedBlackSet<api::Path>& locals, int player) : waf::Server(_B("Web")), connector(new _H<Connector>(configuration)), extensions(extensions), library(connector), player(connector, player) |
63 |
douglas |
471 |
{ |
64 |
douglas |
517 |
{ |
65 |
douglas |
534 |
_L<cse::String> args; |
66 |
douglas |
474 |
|
67 |
douglas |
534 |
_foreach (const ext::RedBlackSet<cse::String>, extension, extensions) |
68 |
douglas |
507 |
args.InsertLast(_S<ios::String>() << "-extension=" << *extension); |
69 |
douglas |
474 |
|
70 |
douglas |
529 |
_foreach (const ext::RedBlackSet<api::Path>, local, locals) |
71 |
douglas |
507 |
args.InsertLast(_S<ios::String>() << "-local=" << *local); |
72 |
douglas |
474 |
|
73 |
douglas |
517 |
_S<api::Process> media(_B("Util/media"), args); |
74 |
douglas |
474 |
|
75 |
douglas |
517 |
Media(*media.GetReader()); |
76 |
douglas |
503 |
|
77 |
douglas |
517 |
media.Join(); |
78 |
douglas |
474 |
} |
79 |
|
|
|
80 |
douglas |
471 |
AddPort(6996); |
81 |
|
|
} |
82 |
douglas |
469 |
|
83 |
douglas |
471 |
void DecentralizedMedia::Process(const net::Http::Request& request, net::Http::Response& response) |
84 |
|
|
{ |
85 |
douglas |
547 |
api::Cout << request.client_ << _B(" "); |
86 |
|
|
|
87 |
douglas |
531 |
if (request.method_ == _B("EXTENSIONS")) |
88 |
douglas |
474 |
{ |
89 |
douglas |
507 |
api::Cout << "EXTENSIONS" << ios::NewLine; |
90 |
|
|
|
91 |
douglas |
474 |
response.SetStatus(200); |
92 |
|
|
|
93 |
douglas |
534 |
_foreach (const ext::RedBlackSet<cse::String>, extension, extensions) |
94 |
douglas |
476 |
response << *extension << ios::NewLineNoFlush; |
95 |
douglas |
474 |
} |
96 |
douglas |
531 |
else if (request.method_ == _B("MEDIA")) |
97 |
douglas |
474 |
{ |
98 |
douglas |
507 |
api::Cout << "MEDIA" << ios::NewLine; |
99 |
|
|
|
100 |
douglas |
508 |
if (!request.content_.IsEmpty()) |
101 |
douglas |
509 |
{ |
102 |
douglas |
542 |
api::InternetAddress host(request.client_); |
103 |
douglas |
508 |
|
104 |
douglas |
542 |
host.SetPort(6996); |
105 |
|
|
|
106 |
|
|
Media(*request.content_, host); |
107 |
|
|
|
108 |
douglas |
509 |
response.SetStatus(204); |
109 |
|
|
} |
110 |
|
|
else |
111 |
douglas |
531 |
response.SetStatus(411); |
112 |
douglas |
474 |
} |
113 |
douglas |
471 |
else |
114 |
|
|
waf::Server::Process(request, response); |
115 |
|
|
} |
116 |
douglas |
507 |
|
117 |
douglas |
547 |
void DecentralizedMedia::Media(ios::Reader& media, const api::Address& host) |
118 |
douglas |
507 |
{ |
119 |
douglas |
541 |
_R<xml::Document> document(xml::Parse(media)); |
120 |
douglas |
531 |
api::Pcre::RegEx share("^[0-9a-f]+$"); |
121 |
douglas |
507 |
|
122 |
|
|
_foreach (const xml::NodeSet, folder, *document/"media"/"folder") |
123 |
|
|
{ |
124 |
douglas |
534 |
cse::String path(**folder/"path"); |
125 |
douglas |
507 |
|
126 |
douglas |
514 |
if (api::Pcre::RegEx::Match match = share(path)) |
127 |
|
|
{ |
128 |
douglas |
555 |
_R<Share> share(new _H<Share>(connector, host, match[0])); |
129 |
douglas |
514 |
|
130 |
|
|
path = share->path.GetPath(); |
131 |
|
|
|
132 |
douglas |
542 |
player.Add(share); |
133 |
douglas |
514 |
} |
134 |
|
|
|
135 |
|
|
Media(*folder, path, path); |
136 |
douglas |
507 |
} |
137 |
|
|
} |
138 |
|
|
|
139 |
douglas |
541 |
void DecentralizedMedia::Media(const _R<xml::Node>& folder, const api::Path& path, const api::Path& root) |
140 |
douglas |
507 |
{ |
141 |
douglas |
555 |
MediaFolder(connector, path, root); |
142 |
douglas |
507 |
|
143 |
|
|
_foreach (const xml::NodeSet, file, *folder/"file") |
144 |
douglas |
555 |
MediaFile(connector, path.GetChild(**file/"path"), **file/"artist", **file/"title", **file/"album", **file/"genre", root); |
145 |
douglas |
507 |
|
146 |
|
|
_foreach (const xml::NodeSet, folder_, *folder/"folder") |
147 |
|
|
Media(*folder_, path.GetChild(**folder_/"path"), root); |
148 |
|
|
} |