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