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 |
api::Cerr << file << ios::NewLine; |
74 |
|
75 |
find->Join(); |
76 |
} |
77 |
|
78 |
AddPort(6996); |
79 |
} |
80 |
|
81 |
DecentralizedMedia::~DecentralizedMedia() |
82 |
{ |
83 |
if (!process.IsEmpty()) |
84 |
{ |
85 |
_H<api::Thread> thread(new api::Thread(etl::BindAll(&DecentralizedMedia::Destroy, this))); |
86 |
|
87 |
bmp.Quit(); |
88 |
|
89 |
thread->Join(); |
90 |
} |
91 |
} |
92 |
|
93 |
_L<ext::String> DecentralizedMedia::GetArtists() const |
94 |
{ |
95 |
_H<dbi::ResultSet> artists_(connection->Execute(_B("SELECT DISTINCT artist FROM files WHERE live = TRUE"))); |
96 |
_L<ext::String> artists; |
97 |
|
98 |
while (artists_->MoveNext()) |
99 |
artists.InsertLast(artists_->GetString(_B("artist"))); |
100 |
|
101 |
return artists; |
102 |
} |
103 |
|
104 |
_L<ext::String> DecentralizedMedia::GetTitles() const |
105 |
{ |
106 |
_H<dbi::ResultSet> titles_(connection->Execute(_B("SELECT DISTINCT title FROM files WHERE live = TRUE"))); |
107 |
_L<ext::String> titles; |
108 |
|
109 |
while (titles_->MoveNext()) |
110 |
titles.InsertLast(titles_->GetString(_B("title"))); |
111 |
|
112 |
return titles; |
113 |
} |
114 |
|
115 |
_L<ext::String> DecentralizedMedia::GetAlbums() const |
116 |
{ |
117 |
_H<dbi::ResultSet> albums_(connection->Execute(_B("SELECT DISTINCT album FROM files WHERE live = TRUE"))); |
118 |
_L<ext::String> albums; |
119 |
|
120 |
while (albums_->MoveNext()) |
121 |
albums.InsertLast(albums_->GetString(_B("album"))); |
122 |
|
123 |
return albums; |
124 |
} |
125 |
|
126 |
_L<ext::String> DecentralizedMedia::GetGenres() const |
127 |
{ |
128 |
_H<dbi::ResultSet> genres_(connection->Execute(_B("SELECT DISTINCT album FROM files WHERE live = TRUE"))); |
129 |
_L<ext::String> genres; |
130 |
|
131 |
while (genres_->MoveNext()) |
132 |
genres.InsertLast(genres_->GetString(_B("genre"))); |
133 |
|
134 |
return genres; |
135 |
} |
136 |
|
137 |
_L<MediaFolder> DecentralizedMedia::GetFolders() const |
138 |
{ |
139 |
_H<dbi::ResultSet> paths(connection->Execute(_B("SELECT DISTINCT root FROM files WHERE live = TRUE"))); |
140 |
_L<MediaFolder> folders; |
141 |
|
142 |
while (paths->MoveNext()) |
143 |
folders.InsertLast(MediaFolder(connection, paths->GetString(_B("path")))); |
144 |
|
145 |
return folders; |
146 |
} |
147 |
|
148 |
void DecentralizedMedia::Process(const net::Http::Request& request, net::Http::Response& response) |
149 |
{ |
150 |
if (request.method_ == _B("EXTENSIONS")) |
151 |
{ |
152 |
response.SetStatus(200); |
153 |
|
154 |
_foreach (const ext::RedBlackSet<ext::String>, extension, extensions) |
155 |
response << *extension << ios::NewLineNoFlush; |
156 |
} |
157 |
else if (request.method_ == _B("MEDIA")) |
158 |
{ |
159 |
// XXX: implement |
160 |
} |
161 |
else |
162 |
waf::Server::Process(request, response); |
163 |
} |