1 |
// Decentralized Media |
2 |
// |
3 |
// Douglas Thrift |
4 |
// |
5 |
// $Id$ |
6 |
|
7 |
#include <menes/c++/standard.hh> |
8 |
|
9 |
#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 |
|
19 |
#include "DecentralizedMedia.hpp" |
20 |
|
21 |
int Main(const app::Options& options) |
22 |
{ |
23 |
api::Pcre::RegEx extension(_B("^-extension=(.+)$")), local(_B("^-local=(.+)$")), player_("^-player=(.+)$"); |
24 |
ext::RedBlackSet<cse::String> extensions; |
25 |
ext::RedBlackSet<api::Path> locals; |
26 |
int player(0); |
27 |
|
28 |
_foreach (const app::ArgumentList, arg, app::GetArguments()) |
29 |
{ |
30 |
api::Pcre::RegEx::Match match; |
31 |
|
32 |
if (match = extension(*arg)) |
33 |
extensions.Insert(match[1]); |
34 |
else if (match = local(*arg)) |
35 |
locals.Insert(api::Path(match[1]).GetRealPath()); |
36 |
else if (match = player_(*arg)) |
37 |
player = lexical_cast<int>(match[1]); |
38 |
} |
39 |
|
40 |
if (extensions.IsEmpty()) |
41 |
extensions.Insert(_B("mp3")); |
42 |
|
43 |
{ |
44 |
api::Path media(_B("Media")); |
45 |
|
46 |
if (!media.Exists()) |
47 |
media.CreateDirectory(); |
48 |
|
49 |
locals.Insert(media.GetRealPath()); |
50 |
} |
51 |
|
52 |
_S<DecentralizedMedia> media(extensions, locals, player); |
53 |
|
54 |
media.Block(); |
55 |
|
56 |
return 0; |
57 |
} |
58 |
|
59 |
DecentralizedMedia::DecentralizedMedia(const ext::RedBlackSet<cse::String>& extensions, const ext::RedBlackSet<api::Path>& locals, int player) : waf::Server(_B("Web")), connection(dbi::GetDriver("dbi::PgSql::Driver")->Connect("", "douglas", "", "media")), extensions(extensions), library(connection), player(connection, player) |
60 |
{ |
61 |
{ |
62 |
_L<cse::String> args; |
63 |
|
64 |
_foreach (const ext::RedBlackSet<cse::String>, extension, extensions) |
65 |
args.InsertLast(_S<ios::String>() << "-extension=" << *extension); |
66 |
|
67 |
_foreach (const ext::RedBlackSet<api::Path>, local, locals) |
68 |
args.InsertLast(_S<ios::String>() << "-local=" << *local); |
69 |
|
70 |
_S<api::Process> media(_B("Util/media"), args); |
71 |
|
72 |
Media(*media.GetReader()); |
73 |
|
74 |
media.Join(); |
75 |
} |
76 |
|
77 |
{ |
78 |
_L<cse::String> args; |
79 |
|
80 |
args.InsertLast(_B("-s")); |
81 |
args.InsertLast(_B("object/DecentralizedMedia.hh.gch")); |
82 |
args.InsertLast(_B("LOCALS=DecentralizedMedia.hh")); |
83 |
|
84 |
_S<api::Process> make(_B(CFG_GNU_MAKE), args); |
85 |
|
86 |
make.ClearWriter(); |
87 |
|
88 |
_S<ios::String> error; |
89 |
|
90 |
ios::ReadToWrite(*make.GetReader(), error); |
91 |
|
92 |
if (make.Join() != 0) |
93 |
throw ext::StringException(error); |
94 |
} |
95 |
|
96 |
AddPort(6996); |
97 |
} |
98 |
|
99 |
void DecentralizedMedia::Process(const net::Http::Request& request, net::Http::Response& response) |
100 |
{ |
101 |
api::Cout << request.client_ << _B(" "); |
102 |
|
103 |
if (request.method_ == _B("EXTENSIONS")) |
104 |
{ |
105 |
api::Cout << "EXTENSIONS" << ios::NewLine; |
106 |
|
107 |
response.SetStatus(200); |
108 |
|
109 |
_foreach (const ext::RedBlackSet<cse::String>, extension, extensions) |
110 |
response << *extension << ios::NewLineNoFlush; |
111 |
} |
112 |
else if (request.method_ == _B("MEDIA")) |
113 |
{ |
114 |
api::Cout << "MEDIA" << ios::NewLine; |
115 |
|
116 |
if (!request.content_.IsEmpty()) |
117 |
{ |
118 |
api::InternetAddress host(request.client_); |
119 |
|
120 |
host.SetPort(6996); |
121 |
|
122 |
Media(*request.content_, host); |
123 |
|
124 |
response.SetStatus(204); |
125 |
} |
126 |
else |
127 |
response.SetStatus(411); |
128 |
} |
129 |
else |
130 |
waf::Server::Process(request, response); |
131 |
} |
132 |
|
133 |
void DecentralizedMedia::Media(ios::Reader& media, const api::Address& host) |
134 |
{ |
135 |
_R<xml::Document> document(xml::Parse(media)); |
136 |
api::Pcre::RegEx share("^[0-9a-f]+$"); |
137 |
|
138 |
_foreach (const xml::NodeSet, folder, *document/"media"/"folder") |
139 |
{ |
140 |
cse::String path(**folder/"path"); |
141 |
|
142 |
if (api::Pcre::RegEx::Match match = share(path)) |
143 |
{ |
144 |
_R<Share> share(new _H<Share>(connection, host, match[0])); |
145 |
|
146 |
path = share->path.GetPath(); |
147 |
|
148 |
player.Add(share); |
149 |
} |
150 |
|
151 |
Media(*folder, path, path); |
152 |
} |
153 |
} |
154 |
|
155 |
void DecentralizedMedia::Media(const _R<xml::Node>& folder, const api::Path& path, const api::Path& root) |
156 |
{ |
157 |
MediaFolder(connection, path, root); |
158 |
|
159 |
_foreach (const xml::NodeSet, file, *folder/"file") |
160 |
MediaFile(connection, path.GetChild(**file/"path"), **file/"artist", **file/"title", **file/"album", **file/"genre", root); |
161 |
|
162 |
_foreach (const xml::NodeSet, folder_, *folder/"folder") |
163 |
Media(*folder_, path.GetChild(**folder_/"path"), root); |
164 |
} |