ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/DecentralizedMedia/DecentralizedMedia.cpp
Revision: 503
Committed: 2005-06-18T01:34:41-07:00 (20 years ago) by douglas
File size: 4573 byte(s)
Log Message:
Meep!

File Contents

# Content
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 connection->Execute(_B("UPDATE files SET live = FALSE"));
54
55 _foreach (const ext::RedBlackSet<ext::String>, local, locals)
56 MediaFolder(connection, *local, *local);
57
58 {
59 _L<ext::String> args(locals.Begin(), locals.End());
60
61 args.InsertLast(_B("-type"));
62 args.InsertLast(_B("d"));
63
64 _foreach (const ext::RedBlackSet<ext::String>, extension, extensions)
65 {
66 args.InsertLast(_B("-or"));
67 args.InsertLast(_B("-iname"));
68 args.InsertLast(_S<ios::String>() << _B("*.") << *extension);
69 }
70
71 _H<api::Process> find(new api::Process(_B("/usr/bin/find"), args));
72 ext::String file;
73 api::Path root;
74
75 while (ios::ReadLine(*find->GetReader(), file))
76 if (locals.Contains(file))
77 root = file;
78 else
79 {
80 api::Path path(file);
81
82 if (path.IsDirectory())
83 MediaFolder(connection, path, root);
84 else
85 MediaFile(connection, path, ext::EmptyString, ext::EmptyString, ext::EmptyString, ext::EmptyString, root);
86 }
87
88 find->Join();
89 }
90
91 AddPort(6996);
92 }
93
94 DecentralizedMedia::~DecentralizedMedia()
95 {
96 if (!process.IsEmpty())
97 {
98 _H<api::Thread> thread(new api::Thread(etl::BindAll(&DecentralizedMedia::Destroy, this)));
99
100 bmp.Quit();
101
102 thread->Join();
103 }
104 }
105
106 _L<ext::String> DecentralizedMedia::GetArtists() const
107 {
108 _H<dbi::ResultSet> artists_(connection->Execute(_B("SELECT DISTINCT artist FROM files WHERE live = TRUE")));
109 _L<ext::String> artists;
110
111 while (artists_->MoveNext())
112 artists.InsertLast(artists_->GetString(_B("artist")));
113
114 return artists;
115 }
116
117 _L<ext::String> DecentralizedMedia::GetTitles() const
118 {
119 _H<dbi::ResultSet> titles_(connection->Execute(_B("SELECT DISTINCT title FROM files WHERE live = TRUE")));
120 _L<ext::String> titles;
121
122 while (titles_->MoveNext())
123 titles.InsertLast(titles_->GetString(_B("title")));
124
125 return titles;
126 }
127
128 _L<ext::String> DecentralizedMedia::GetAlbums() const
129 {
130 _H<dbi::ResultSet> albums_(connection->Execute(_B("SELECT DISTINCT album FROM files WHERE live = TRUE")));
131 _L<ext::String> albums;
132
133 while (albums_->MoveNext())
134 albums.InsertLast(albums_->GetString(_B("album")));
135
136 return albums;
137 }
138
139 _L<ext::String> DecentralizedMedia::GetGenres() const
140 {
141 _H<dbi::ResultSet> genres_(connection->Execute(_B("SELECT DISTINCT album FROM files WHERE live = TRUE")));
142 _L<ext::String> genres;
143
144 while (genres_->MoveNext())
145 genres.InsertLast(genres_->GetString(_B("genre")));
146
147 return genres;
148 }
149
150 _L<MediaFolder> DecentralizedMedia::GetFolders() const
151 {
152 _H<dbi::ResultSet> paths(connection->Execute(_B("SELECT DISTINCT root FROM files WHERE live = TRUE")));
153 _L<MediaFolder> folders;
154
155 while (paths->MoveNext())
156 folders.InsertLast(MediaFolder(connection, paths->GetString(_B("path"))));
157
158 return folders;
159 }
160
161 void DecentralizedMedia::Process(const net::Http::Request& request, net::Http::Response& response)
162 {
163 if (request.method_ == _B("EXTENSIONS"))
164 {
165 response.SetStatus(200);
166
167 _foreach (const ext::RedBlackSet<ext::String>, extension, extensions)
168 response << *extension << ios::NewLineNoFlush;
169 }
170 else if (request.method_ == _B("MEDIA"))
171 {
172 // XXX: implement
173 }
174 else
175 waf::Server::Process(request, response);
176 }

Properties

Name Value
svn:eol-style native
svn:keywords Id