1 |
// Decentralized Media |
2 |
// |
3 |
// Douglas Thrift |
4 |
// |
5 |
// $Id$ |
6 |
|
7 |
#include <menes/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 |
|
83 |
_S<api::Process> make(CFG_GNU_MAKE, args); |
84 |
|
85 |
make.ClearWriter(); |
86 |
|
87 |
_S<ios::String> error; |
88 |
|
89 |
ios::ReadToWrite(*make.GetReader(), error); |
90 |
|
91 |
if (make.Join() != 0) |
92 |
throw ext::StringException(error); |
93 |
} |
94 |
|
95 |
AddPort(6996); |
96 |
} |
97 |
|
98 |
void DecentralizedMedia::Process(const net::Http::Request& request, net::Http::Response& response) |
99 |
{ |
100 |
api::Cout << request.client_ << _B(" "); |
101 |
|
102 |
if (request.method_ == _B("EXTENSIONS")) |
103 |
{ |
104 |
api::Cout << "EXTENSIONS" << ios::NewLine; |
105 |
|
106 |
response.SetStatus(200); |
107 |
|
108 |
_foreach (const ext::RedBlackSet<cse::String>, extension, extensions) |
109 |
response << *extension << ios::NewLineNoFlush; |
110 |
} |
111 |
else if (request.method_ == _B("MEDIA")) |
112 |
{ |
113 |
api::Cout << "MEDIA" << ios::NewLine; |
114 |
|
115 |
if (!request.content_.IsEmpty()) |
116 |
{ |
117 |
api::InternetAddress host(request.client_); |
118 |
|
119 |
host.SetPort(6996); |
120 |
|
121 |
Media(*request.content_, host); |
122 |
|
123 |
response.SetStatus(204); |
124 |
} |
125 |
else |
126 |
response.SetStatus(411); |
127 |
} |
128 |
else |
129 |
waf::Server::Process(request, response); |
130 |
} |
131 |
|
132 |
void DecentralizedMedia::Media(ios::Reader& media, const api::Address& host) |
133 |
{ |
134 |
_R<xml::Document> document(xml::Parse(media)); |
135 |
api::Pcre::RegEx share("^[0-9a-f]+$"); |
136 |
|
137 |
_foreach (const xml::NodeSet, folder, *document/"media"/"folder") |
138 |
{ |
139 |
cse::String path(**folder/"path"); |
140 |
|
141 |
if (api::Pcre::RegEx::Match match = share(path)) |
142 |
{ |
143 |
_R<Share> share(new _H<Share>(connection, host, match[0])); |
144 |
|
145 |
path = share->path.GetPath(); |
146 |
|
147 |
player.Add(share); |
148 |
} |
149 |
|
150 |
Media(*folder, path, path); |
151 |
} |
152 |
} |
153 |
|
154 |
void DecentralizedMedia::Media(const _R<xml::Node>& folder, const api::Path& path, const api::Path& root) |
155 |
{ |
156 |
MediaFolder(connection, path, root); |
157 |
|
158 |
_foreach (const xml::NodeSet, file, *folder/"file") |
159 |
MediaFile(connection, path.GetChild(**file/"path"), **file/"artist", **file/"title", **file/"album", **file/"genre", root); |
160 |
|
161 |
_foreach (const xml::NodeSet, folder_, *folder/"folder") |
162 |
Media(*folder_, path.GetChild(**folder_/"path"), root); |
163 |
} |