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 |
471 |
#include <menes-app/simple.hpp> |
11 |
douglas |
484 |
#include <menes-dbi/driver.hpp> |
12 |
|
|
#include <menes-dbi/resultset.hpp> |
13 |
douglas |
471 |
#include <menes-etl/fnbind.hpp> |
14 |
douglas |
474 |
#include <menes-ios/helpers.hpp> |
15 |
douglas |
471 |
#include <menes-net/http/request.hpp> |
16 |
|
|
#include <menes-net/http/response.hpp> |
17 |
douglas |
470 |
|
18 |
douglas |
471 |
#include "DecentralizedMedia.hpp" |
19 |
douglas |
464 |
|
20 |
douglas |
471 |
int Main(const app::Options& options) |
21 |
douglas |
464 |
{ |
22 |
douglas |
491 |
ext::RedBlackSet<ext::String> extensions, locals; |
23 |
douglas |
474 |
api::Pcre::RegEx extension(_B("^-extension=(.+)$")), local(_B("^-local=(.+)$")); |
24 |
douglas |
464 |
|
25 |
douglas |
474 |
_foreach (const app::ArgumentList, arg, app::GetArguments()) |
26 |
|
|
{ |
27 |
|
|
api::Pcre::RegEx::Match match; |
28 |
|
|
|
29 |
|
|
if (match = extension(*arg)) |
30 |
douglas |
491 |
extensions.Insert(match[1]); |
31 |
douglas |
474 |
else if (match = local(*arg)) |
32 |
douglas |
491 |
locals.Insert(match[1]); |
33 |
douglas |
474 |
} |
34 |
|
|
|
35 |
|
|
if (extensions.IsEmpty()) |
36 |
douglas |
491 |
extensions.Insert(_B("mp3")); |
37 |
douglas |
474 |
|
38 |
douglas |
491 |
locals.Insert("Media"); |
39 |
|
|
|
40 |
douglas |
481 |
_S<DecentralizedMedia> media(extensions, locals); |
41 |
douglas |
474 |
|
42 |
douglas |
472 |
media.Block(); |
43 |
|
|
|
44 |
douglas |
471 |
return 0; |
45 |
|
|
} |
46 |
douglas |
469 |
|
47 |
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) |
48 |
douglas |
471 |
{ |
49 |
douglas |
474 |
if (!locals.IsEmpty()) |
50 |
|
|
{ |
51 |
douglas |
491 |
_L<ext::String> args(locals.Begin(), locals.End()); |
52 |
douglas |
474 |
|
53 |
douglas |
491 |
_foreach (const ext::RedBlackSet<ext::String>, extension, extensions) |
54 |
douglas |
474 |
{ |
55 |
douglas |
476 |
if (extension != _set.Begin()) |
56 |
|
|
args.InsertLast(_B("-or")); |
57 |
|
|
|
58 |
douglas |
492 |
args.InsertLast(_B("-iname")); |
59 |
douglas |
474 |
args.InsertLast(_S<ios::String>() << _B("*.") << *extension); |
60 |
|
|
} |
61 |
|
|
|
62 |
|
|
_H<api::Process> find(new api::Process(_B("/usr/bin/find"), args)); |
63 |
|
|
ext::String file; |
64 |
|
|
|
65 |
|
|
while (ios::ReadLine(*find->GetReader(), file)) |
66 |
douglas |
476 |
/*media.Insert(file)*/; |
67 |
douglas |
474 |
|
68 |
|
|
find->Join(); |
69 |
|
|
} |
70 |
|
|
|
71 |
douglas |
471 |
AddPort(6996); |
72 |
|
|
} |
73 |
douglas |
469 |
|
74 |
douglas |
471 |
DecentralizedMedia::~DecentralizedMedia() |
75 |
douglas |
469 |
{ |
76 |
douglas |
471 |
if (!process.IsEmpty()) |
77 |
|
|
{ |
78 |
|
|
_H<api::Thread> thread(new api::Thread(etl::BindAll(&DecentralizedMedia::Destroy, this))); |
79 |
douglas |
470 |
|
80 |
douglas |
471 |
bmp.Quit(); |
81 |
douglas |
469 |
|
82 |
douglas |
471 |
thread->Join(); |
83 |
|
|
} |
84 |
douglas |
464 |
} |
85 |
douglas |
471 |
|
86 |
douglas |
491 |
_L<ext::String> DecentralizedMedia::GetArtists() const |
87 |
|
|
{ |
88 |
|
|
_H<dbi::ResultSet> artists_(connection->Execute(_B("SELECT DISTINCT artist FROM files WHERE live = TRUE"))); |
89 |
|
|
_L<ext::String> artists; |
90 |
|
|
|
91 |
|
|
while (artists_->MoveNext()) |
92 |
|
|
artists.InsertLast(artists_->GetString(_B("artist"))); |
93 |
|
|
|
94 |
|
|
return artists; |
95 |
|
|
} |
96 |
|
|
|
97 |
|
|
_L<ext::String> DecentralizedMedia::GetTitles() const |
98 |
|
|
{ |
99 |
|
|
_H<dbi::ResultSet> titles_(connection->Execute(_B("SELECT DISTINCT title FROM files WHERE live = TRUE"))); |
100 |
|
|
_L<ext::String> titles; |
101 |
|
|
|
102 |
|
|
while (titles_->MoveNext()) |
103 |
|
|
titles.InsertLast(titles_->GetString(_B("title"))); |
104 |
|
|
|
105 |
|
|
return titles; |
106 |
|
|
} |
107 |
|
|
|
108 |
|
|
_L<ext::String> DecentralizedMedia::GetAlbums() const |
109 |
|
|
{ |
110 |
|
|
_H<dbi::ResultSet> albums_(connection->Execute(_B("SELECT DISTINCT album FROM files WHERE live = TRUE"))); |
111 |
|
|
_L<ext::String> albums; |
112 |
|
|
|
113 |
|
|
while (albums_->MoveNext()) |
114 |
|
|
albums.InsertLast(albums_->GetString(_B("album"))); |
115 |
|
|
|
116 |
|
|
return albums; |
117 |
|
|
} |
118 |
|
|
|
119 |
|
|
_L<ext::String> DecentralizedMedia::GetGenres() const |
120 |
|
|
{ |
121 |
|
|
_H<dbi::ResultSet> genres_(connection->Execute(_B("SELECT DISTINCT album FROM files WHERE live = TRUE"))); |
122 |
|
|
_L<ext::String> genres; |
123 |
|
|
|
124 |
|
|
while (genres_->MoveNext()) |
125 |
|
|
genres.InsertLast(genres_->GetString(_B("genre"))); |
126 |
|
|
|
127 |
|
|
return genres; |
128 |
|
|
} |
129 |
|
|
|
130 |
|
|
_L<MediaFolder> DecentralizedMedia::GetFolders() const |
131 |
|
|
{ |
132 |
|
|
_H<dbi::ResultSet> paths(connection->Execute(_B("SELECT DISTINCT root FROM files WHERE live = TRUE"))); |
133 |
|
|
_L<MediaFolder> folders; |
134 |
|
|
|
135 |
|
|
while (paths->MoveNext()) |
136 |
|
|
folders.InsertLast(MediaFolder(connection, paths->GetString(_B("path")))); |
137 |
|
|
|
138 |
|
|
return folders; |
139 |
|
|
} |
140 |
|
|
|
141 |
douglas |
471 |
void DecentralizedMedia::Process(const net::Http::Request& request, net::Http::Response& response) |
142 |
|
|
{ |
143 |
douglas |
474 |
if (request.method_ == _B("EXTENSIONS")) |
144 |
|
|
{ |
145 |
|
|
response.SetStatus(200); |
146 |
|
|
|
147 |
douglas |
492 |
_foreach (const ext::RedBlackSet<ext::String>, extension, extensions) |
148 |
douglas |
476 |
response << *extension << ios::NewLineNoFlush; |
149 |
douglas |
474 |
} |
150 |
|
|
else if (request.method_ == _B("MEDIA")) |
151 |
|
|
{ |
152 |
|
|
// XXX: implement |
153 |
|
|
} |
154 |
douglas |
471 |
else |
155 |
|
|
waf::Server::Process(request, response); |
156 |
|
|
} |