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 |
495 |
locals.Insert(api::ReadLink(match[1])); |
33 |
douglas |
474 |
} |
34 |
|
|
|
35 |
|
|
if (extensions.IsEmpty()) |
36 |
douglas |
491 |
extensions.Insert(_B("mp3")); |
37 |
douglas |
474 |
|
38 |
douglas |
493 |
if (!api::Path(_B("Media")).Exists()) |
39 |
|
|
api::Posix::CheckError(::mkdir("Media", 0755)); |
40 |
douglas |
491 |
|
41 |
douglas |
495 |
locals.Insert(api::ReadLink(_B("Media"))); |
42 |
douglas |
493 |
|
43 |
douglas |
481 |
_S<DecentralizedMedia> media(extensions, locals); |
44 |
douglas |
474 |
|
45 |
douglas |
472 |
media.Block(); |
46 |
|
|
|
47 |
douglas |
471 |
return 0; |
48 |
|
|
} |
49 |
douglas |
469 |
|
50 |
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) |
51 |
douglas |
471 |
{ |
52 |
douglas |
495 |
_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 |
douglas |
493 |
|
56 |
douglas |
495 |
if (!path->MoveNext()) |
57 |
|
|
connection->Parse(_B("INSERT INTO folders (path) VALUES (?)"))->Execute(*local); |
58 |
|
|
} |
59 |
|
|
|
60 |
douglas |
474 |
{ |
61 |
douglas |
491 |
_L<ext::String> args(locals.Begin(), locals.End()); |
62 |
douglas |
474 |
|
63 |
douglas |
493 |
args.InsertLast(_B("-type")); |
64 |
|
|
args.InsertLast(_B("d")); |
65 |
|
|
|
66 |
douglas |
491 |
_foreach (const ext::RedBlackSet<ext::String>, extension, extensions) |
67 |
douglas |
474 |
{ |
68 |
douglas |
493 |
args.InsertLast(_B("-or")); |
69 |
douglas |
492 |
args.InsertLast(_B("-iname")); |
70 |
douglas |
474 |
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 |
douglas |
493 |
while (ios::ReadLine(*find->GetReader(), file)) if (!locals.Contains(file)) |
77 |
|
|
{ |
78 |
|
|
} |
79 |
douglas |
474 |
|
80 |
|
|
find->Join(); |
81 |
|
|
} |
82 |
|
|
|
83 |
douglas |
471 |
AddPort(6996); |
84 |
|
|
} |
85 |
douglas |
469 |
|
86 |
douglas |
471 |
DecentralizedMedia::~DecentralizedMedia() |
87 |
douglas |
469 |
{ |
88 |
douglas |
471 |
if (!process.IsEmpty()) |
89 |
|
|
{ |
90 |
|
|
_H<api::Thread> thread(new api::Thread(etl::BindAll(&DecentralizedMedia::Destroy, this))); |
91 |
douglas |
470 |
|
92 |
douglas |
471 |
bmp.Quit(); |
93 |
douglas |
469 |
|
94 |
douglas |
471 |
thread->Join(); |
95 |
|
|
} |
96 |
douglas |
464 |
} |
97 |
douglas |
471 |
|
98 |
douglas |
491 |
_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 |
douglas |
471 |
void DecentralizedMedia::Process(const net::Http::Request& request, net::Http::Response& response) |
154 |
|
|
{ |
155 |
douglas |
474 |
if (request.method_ == _B("EXTENSIONS")) |
156 |
|
|
{ |
157 |
|
|
response.SetStatus(200); |
158 |
|
|
|
159 |
douglas |
492 |
_foreach (const ext::RedBlackSet<ext::String>, extension, extensions) |
160 |
douglas |
476 |
response << *extension << ios::NewLineNoFlush; |
161 |
douglas |
474 |
} |
162 |
|
|
else if (request.method_ == _B("MEDIA")) |
163 |
|
|
{ |
164 |
|
|
// XXX: implement |
165 |
|
|
} |
166 |
douglas |
471 |
else |
167 |
|
|
waf::Server::Process(request, response); |
168 |
|
|
} |