1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
#include "music.h"
#include "decoder.h"
#include "encoder.h"
#include "transcode.h"
#include <boost/format.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/filesystem/fstream.hpp>
namespace music {
MusicDirectory *root_directory = NULL;
void init(std::string root) {
// remove trailing slash
if(boost::algorithm::ends_with(root, "/"))
root = root.substr(0, root.size()-1);
root_directory = new MusicDirectory(root);
}
MusicListing *find(std::string path, MusicDirectory& root) {
// remove trailing slash
if(boost::algorithm::ends_with(path, "/"))
path = path.substr(0, path.size()-1);
fs::path full_path = root_directory->path / path;
if(root.path == full_path)
return &root;
for(MusicDirectories::iterator it = root.directories.begin(); it != root.directories.end(); it++) {
MusicListing *ml = find(path, *it);
if(ml) return ml;
}
for(MusicTracks::iterator it = root.tracks.begin(); it != root.tracks.end(); it++) {
std::string rel_path = it->path.string().substr(root_directory->path.string().size());
std::cout << rel_path << std::endl;
if(rel_path == path)
return &(*it);
}
return NULL;
}
MusicListing *find(std::string path) {
return find(path, *root_directory);
}
}; // namespace music
void MusicDirectory::render(HTTPRequest& req, HTTPResponse& res) {
res.add_header("content-type", "text/html");
for(MusicDirectories::iterator it = directories.begin(); it != directories.end(); it++) {
std::string rel_path = it->path.string().substr(music::root_directory->path.string().size());
res.write(boost::str(boost::format("<a href=\"%s\">%s</a><br />") % rel_path % rel_path));
}
for(MusicTracks::iterator it = tracks.begin(); it != tracks.end(); it++) {
std::string rel_path = it->path.string().substr(music::root_directory->path.string().size());
res.write(boost::str(boost::format("<a href=\"%s\">%s</a><br />") % rel_path % rel_path));
}
}
MusicTrack::MusicTrack(const fs::path path) {
std::cout << path << std::endl;
this->path = path;
}
MusicDirectory::MusicDirectory(const fs::path root) {
this->path = root;
std::cout << root << std::endl;
fs::path p(root);
fs::directory_iterator end_itr;
for(fs::directory_iterator it(p); it != end_itr; it++) {
if(fs::is_directory(it->status())) {
directories.push_back(MusicDirectory(it->path().string()));
} else if(fs::is_regular_file(it->status())) {
tracks.push_back(MusicTrack(it->path().string()));
}
}
}
void MusicTrack::render(HTTPRequest& req, HTTPResponse& res) {
res.add_header("content-type", "application/octet-stream");
fs::ifstream is(path, std::ios::binary | std::ios::in);
is.seekg(0, std::ios::end);
res.add_header("content-length", boost::str(boost::format("%d") % is.tellg()));
is.seekg(0, std::ios::beg);
if(req.query.find("decoder") != req.query.end() && req.query.find("encoder") != req.query.end()) {
DecoderBase *d = decoder::get_decoder(req.query["decoder"]);
EncoderBase *e = encoder::get_encoder(req.query["encoder"]);
Transcoder t(is, res, *d, *e);
t.run();
delete d;
delete e;
} else {
char data[0x1000];
while(is.good()) {
is.read(data, 0x1000);
res.write(data, is.gcount());
}
}
}
|