1 |
douglas |
464 |
// Decentralized Media |
2 |
|
|
// |
3 |
|
|
// Douglas Thrift |
4 |
|
|
// |
5 |
|
|
// $Id$ |
6 |
|
|
|
7 |
douglas |
470 |
#include <menes/standard.hh> |
8 |
douglas |
464 |
|
9 |
douglas |
474 |
#include <menes-api/pcre/regex.hpp> |
10 |
douglas |
497 |
#include <menes-api/realpath.hpp> |
11 |
douglas |
471 |
#include <menes-app/simple.hpp> |
12 |
douglas |
484 |
#include <menes-dbi/driver.hpp> |
13 |
|
|
#include <menes-dbi/resultset.hpp> |
14 |
douglas |
471 |
#include <menes-etl/fnbind.hpp> |
15 |
douglas |
474 |
#include <menes-ios/helpers.hpp> |
16 |
douglas |
471 |
#include <menes-net/http/request.hpp> |
17 |
|
|
#include <menes-net/http/response.hpp> |
18 |
douglas |
507 |
#include <menes-xml/document.hpp> |
19 |
|
|
#include <menes-xml/parse.hpp> |
20 |
douglas |
470 |
|
21 |
douglas |
471 |
#include "DecentralizedMedia.hpp" |
22 |
douglas |
464 |
|
23 |
douglas |
471 |
int Main(const app::Options& options) |
24 |
douglas |
464 |
{ |
25 |
douglas |
491 |
ext::RedBlackSet<ext::String> extensions, locals; |
26 |
douglas |
474 |
api::Pcre::RegEx extension(_B("^-extension=(.+)$")), local(_B("^-local=(.+)$")); |
27 |
douglas |
464 |
|
28 |
douglas |
474 |
_foreach (const app::ArgumentList, arg, app::GetArguments()) |
29 |
|
|
{ |
30 |
|
|
api::Pcre::RegEx::Match match; |
31 |
|
|
|
32 |
|
|
if (match = extension(*arg)) |
33 |
douglas |
491 |
extensions.Insert(match[1]); |
34 |
douglas |
474 |
else if (match = local(*arg)) |
35 |
douglas |
497 |
locals.Insert(api::RealPath(match[1])); |
36 |
douglas |
474 |
} |
37 |
|
|
|
38 |
|
|
if (extensions.IsEmpty()) |
39 |
douglas |
491 |
extensions.Insert(_B("mp3")); |
40 |
douglas |
474 |
|
41 |
douglas |
493 |
if (!api::Path(_B("Media")).Exists()) |
42 |
|
|
api::Posix::CheckError(::mkdir("Media", 0755)); |
43 |
douglas |
491 |
|
44 |
douglas |
497 |
locals.Insert(api::RealPath(_B("Media"))); |
45 |
douglas |
493 |
|
46 |
douglas |
481 |
_S<DecentralizedMedia> media(extensions, locals); |
47 |
douglas |
474 |
|
48 |
douglas |
472 |
media.Block(); |
49 |
|
|
|
50 |
douglas |
471 |
return 0; |
51 |
|
|
} |
52 |
douglas |
469 |
|
53 |
douglas |
491 |
DecentralizedMedia::DecentralizedMedia(const ext::RedBlackSet<ext::String>& extensions, const ext::RedBlackSet<ext::String>& locals) : waf::Server(_B("Web")), process(bmp.IsRunning() ? NULL : new api::Process(_B("/usr/X11R6/bin/beep-media-player"))), connection(dbi::GetDriver("pgsql")->Connect("", "douglas", "", "media")), extensions(extensions) |
54 |
douglas |
471 |
{ |
55 |
douglas |
503 |
connection->Execute(_B("UPDATE files SET live = FALSE")); |
56 |
|
|
|
57 |
douglas |
474 |
{ |
58 |
douglas |
507 |
_L<ext::String> args; |
59 |
douglas |
474 |
|
60 |
douglas |
491 |
_foreach (const ext::RedBlackSet<ext::String>, extension, extensions) |
61 |
douglas |
507 |
args.InsertLast(_S<ios::String>() << "-extension=" << *extension); |
62 |
douglas |
474 |
|
63 |
douglas |
507 |
_foreach (const ext::RedBlackSet<ext::String>, local, locals) |
64 |
|
|
args.InsertLast(_S<ios::String>() << "-local=" << *local); |
65 |
douglas |
474 |
|
66 |
douglas |
507 |
_H<api::Process> media(new api::Process(api::RealPath(_B("Util/media")), args)); |
67 |
douglas |
474 |
|
68 |
douglas |
507 |
Media(*media->GetReader()); |
69 |
douglas |
503 |
|
70 |
douglas |
507 |
media->Join(); |
71 |
douglas |
474 |
} |
72 |
|
|
|
73 |
douglas |
471 |
AddPort(6996); |
74 |
|
|
} |
75 |
douglas |
469 |
|
76 |
douglas |
471 |
DecentralizedMedia::~DecentralizedMedia() |
77 |
douglas |
469 |
{ |
78 |
douglas |
471 |
if (!process.IsEmpty()) |
79 |
|
|
{ |
80 |
|
|
_H<api::Thread> thread(new api::Thread(etl::BindAll(&DecentralizedMedia::Destroy, this))); |
81 |
douglas |
470 |
|
82 |
douglas |
471 |
bmp.Quit(); |
83 |
douglas |
469 |
|
84 |
douglas |
471 |
thread->Join(); |
85 |
|
|
} |
86 |
douglas |
464 |
} |
87 |
douglas |
471 |
|
88 |
douglas |
491 |
_L<ext::String> DecentralizedMedia::GetArtists() const |
89 |
|
|
{ |
90 |
|
|
_H<dbi::ResultSet> artists_(connection->Execute(_B("SELECT DISTINCT artist FROM files WHERE live = TRUE"))); |
91 |
|
|
_L<ext::String> artists; |
92 |
|
|
|
93 |
|
|
while (artists_->MoveNext()) |
94 |
|
|
artists.InsertLast(artists_->GetString(_B("artist"))); |
95 |
|
|
|
96 |
|
|
return artists; |
97 |
|
|
} |
98 |
|
|
|
99 |
|
|
_L<ext::String> DecentralizedMedia::GetTitles() const |
100 |
|
|
{ |
101 |
|
|
_H<dbi::ResultSet> titles_(connection->Execute(_B("SELECT DISTINCT title FROM files WHERE live = TRUE"))); |
102 |
|
|
_L<ext::String> titles; |
103 |
|
|
|
104 |
|
|
while (titles_->MoveNext()) |
105 |
|
|
titles.InsertLast(titles_->GetString(_B("title"))); |
106 |
|
|
|
107 |
|
|
return titles; |
108 |
|
|
} |
109 |
|
|
|
110 |
|
|
_L<ext::String> DecentralizedMedia::GetAlbums() const |
111 |
|
|
{ |
112 |
|
|
_H<dbi::ResultSet> albums_(connection->Execute(_B("SELECT DISTINCT album FROM files WHERE live = TRUE"))); |
113 |
|
|
_L<ext::String> albums; |
114 |
|
|
|
115 |
|
|
while (albums_->MoveNext()) |
116 |
|
|
albums.InsertLast(albums_->GetString(_B("album"))); |
117 |
|
|
|
118 |
|
|
return albums; |
119 |
|
|
} |
120 |
|
|
|
121 |
|
|
_L<ext::String> DecentralizedMedia::GetGenres() const |
122 |
|
|
{ |
123 |
|
|
_H<dbi::ResultSet> genres_(connection->Execute(_B("SELECT DISTINCT album FROM files WHERE live = TRUE"))); |
124 |
|
|
_L<ext::String> genres; |
125 |
|
|
|
126 |
|
|
while (genres_->MoveNext()) |
127 |
|
|
genres.InsertLast(genres_->GetString(_B("genre"))); |
128 |
|
|
|
129 |
|
|
return genres; |
130 |
|
|
} |
131 |
|
|
|
132 |
|
|
_L<MediaFolder> DecentralizedMedia::GetFolders() const |
133 |
|
|
{ |
134 |
|
|
_H<dbi::ResultSet> paths(connection->Execute(_B("SELECT DISTINCT root FROM files WHERE live = TRUE"))); |
135 |
|
|
_L<MediaFolder> folders; |
136 |
|
|
|
137 |
|
|
while (paths->MoveNext()) |
138 |
|
|
folders.InsertLast(MediaFolder(connection, paths->GetString(_B("path")))); |
139 |
|
|
|
140 |
|
|
return folders; |
141 |
|
|
} |
142 |
|
|
|
143 |
douglas |
471 |
void DecentralizedMedia::Process(const net::Http::Request& request, net::Http::Response& response) |
144 |
|
|
{ |
145 |
douglas |
507 |
if (request.method_ == _B("EXTENSIONS") || request.method_ == _B("GET") && request.uri_.GetUri() == ext::EmptyString) |
146 |
douglas |
474 |
{ |
147 |
douglas |
507 |
api::Cout << "EXTENSIONS" << ios::NewLine; |
148 |
|
|
|
149 |
douglas |
474 |
response.SetStatus(200); |
150 |
|
|
|
151 |
douglas |
492 |
_foreach (const ext::RedBlackSet<ext::String>, extension, extensions) |
152 |
douglas |
476 |
response << *extension << ios::NewLineNoFlush; |
153 |
douglas |
474 |
} |
154 |
douglas |
507 |
else if (request.method_ == _B("MEDIA") || request.method_ == _B("POST") && request.uri_.GetUri() == ext::EmptyString) |
155 |
douglas |
474 |
{ |
156 |
douglas |
507 |
api::Cout << "MEDIA" << ios::NewLine; |
157 |
|
|
|
158 |
douglas |
508 |
if (!request.content_.IsEmpty()) |
159 |
douglas |
509 |
{ |
160 |
|
|
Media(*request.content_); |
161 |
douglas |
508 |
|
162 |
douglas |
509 |
response.SetStatus(204); |
163 |
|
|
} |
164 |
|
|
else |
165 |
|
|
response.SetStatus(402); |
166 |
douglas |
474 |
} |
167 |
douglas |
471 |
else |
168 |
|
|
waf::Server::Process(request, response); |
169 |
|
|
} |
170 |
douglas |
507 |
|
171 |
|
|
void DecentralizedMedia::Media(ios::Reader& media) |
172 |
|
|
{ |
173 |
|
|
_H<xml::Document> document(xml::Parse(media)); |
174 |
|
|
api::Pcre::RegEx share("^//(.+)/(.+)$"); |
175 |
|
|
|
176 |
|
|
_foreach (const xml::NodeSet, folder, *document/"media"/"folder") |
177 |
|
|
{ |
178 |
|
|
ext::String path(**folder/"path"); |
179 |
|
|
|
180 |
douglas |
514 |
if (api::Pcre::RegEx::Match match = share(path)) |
181 |
|
|
{ |
182 |
|
|
_H<Share> share(new Share(connection, match[1], match[2])); |
183 |
|
|
|
184 |
|
|
path = share->path.GetPath(); |
185 |
|
|
|
186 |
|
|
// XXX: these are probably going to need read/write locking |
187 |
|
|
sharesByPath[path] = share; |
188 |
|
|
sharesByHost[match[1]][match[2]] = share; |
189 |
|
|
|
190 |
|
|
share->Mount(); |
191 |
|
|
} |
192 |
|
|
|
193 |
|
|
Media(*folder, path, path); |
194 |
douglas |
507 |
} |
195 |
|
|
} |
196 |
|
|
|
197 |
|
|
void DecentralizedMedia::Media(const _H<xml::Node>& folder, const api::Path& path, const api::Path& root) |
198 |
|
|
{ |
199 |
|
|
MediaFolder(connection, path, root); |
200 |
|
|
|
201 |
|
|
_foreach (const xml::NodeSet, file, *folder/"file") |
202 |
|
|
MediaFile(connection, path.GetChild(**file/"path"), **file/"artist", **file/"title", **file/"album", **file/"genre", root); |
203 |
|
|
|
204 |
|
|
_foreach (const xml::NodeSet, folder_, *folder/"folder") |
205 |
|
|
Media(*folder_, path.GetChild(**folder_/"path"), root); |
206 |
|
|
} |