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