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-api/realpath.hpp> |
11 |
#include <menes-app/simple.hpp> |
12 |
#include <menes-dbi/driver.hpp> |
13 |
#include <menes-dbi/resultset.hpp> |
14 |
#include <menes-etl/fnbind.hpp> |
15 |
#include <menes-ios/helpers.hpp> |
16 |
#include <menes-net/http/request.hpp> |
17 |
#include <menes-net/http/response.hpp> |
18 |
|
19 |
#include "DecentralizedMedia.hpp" |
20 |
|
21 |
int Main(const app::Options& options) |
22 |
{ |
23 |
ext::RedBlackSet<ext::String> extensions, locals; |
24 |
api::Pcre::RegEx extension(_B("^-extension=(.+)$")), local(_B("^-local=(.+)$")); |
25 |
|
26 |
_foreach (const app::ArgumentList, arg, app::GetArguments()) |
27 |
{ |
28 |
api::Pcre::RegEx::Match match; |
29 |
|
30 |
if (match = extension(*arg)) |
31 |
extensions.Insert(match[1]); |
32 |
else if (match = local(*arg)) |
33 |
locals.Insert(api::RealPath(match[1])); |
34 |
} |
35 |
|
36 |
if (extensions.IsEmpty()) |
37 |
extensions.Insert(_B("mp3")); |
38 |
|
39 |
if (!api::Path(_B("Media")).Exists()) |
40 |
api::Posix::CheckError(::mkdir("Media", 0755)); |
41 |
|
42 |
locals.Insert(api::RealPath(_B("Media"))); |
43 |
|
44 |
_S<DecentralizedMedia> media(extensions, locals); |
45 |
|
46 |
media.Block(); |
47 |
|
48 |
return 0; |
49 |
} |
50 |
|
51 |
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 |
{ |
53 |
_foreach (const ext::RedBlackSet<ext::String>, local, locals) |
54 |
MediaFolder(connection, *local, *local); |
55 |
|
56 |
{ |
57 |
_L<ext::String> args(locals.Begin(), locals.End()); |
58 |
|
59 |
args.InsertLast(_B("-type")); |
60 |
args.InsertLast(_B("d")); |
61 |
|
62 |
_foreach (const ext::RedBlackSet<ext::String>, extension, extensions) |
63 |
{ |
64 |
args.InsertLast(_B("-or")); |
65 |
args.InsertLast(_B("-iname")); |
66 |
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 |
while (ios::ReadLine(*find->GetReader(), file)) if (!locals.Contains(file)) |
73 |
{ |
74 |
} |
75 |
|
76 |
find->Join(); |
77 |
} |
78 |
|
79 |
AddPort(6996); |
80 |
} |
81 |
|
82 |
DecentralizedMedia::~DecentralizedMedia() |
83 |
{ |
84 |
if (!process.IsEmpty()) |
85 |
{ |
86 |
_H<api::Thread> thread(new api::Thread(etl::BindAll(&DecentralizedMedia::Destroy, this))); |
87 |
|
88 |
bmp.Quit(); |
89 |
|
90 |
thread->Join(); |
91 |
} |
92 |
} |
93 |
|
94 |
_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 |
void DecentralizedMedia::Process(const net::Http::Request& request, net::Http::Response& response) |
150 |
{ |
151 |
if (request.method_ == _B("EXTENSIONS")) |
152 |
{ |
153 |
response.SetStatus(200); |
154 |
|
155 |
_foreach (const ext::RedBlackSet<ext::String>, extension, extensions) |
156 |
response << *extension << ios::NewLineNoFlush; |
157 |
} |
158 |
else if (request.method_ == _B("MEDIA")) |
159 |
{ |
160 |
// XXX: implement |
161 |
} |
162 |
else |
163 |
waf::Server::Process(request, response); |
164 |
} |