summaryrefslogtreecommitdiff
path: root/music.cpp
blob: 8a2896539ed1673ea30163ac29e0f963544e3aae (plain)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include "music.h"
#include "decoder.h"
#include "encoder.h"
#include "tag.h"
#include "database.h"
#include "cache.h"

#include <boost/format.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/join.hpp>
#include <boost/cast.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/foreach.hpp>
#include <soci/soci.h>

fs::path music::root_directory;

void music::init(std::string root) {
	// remove trailing slash
	if(boost::algorithm::ends_with(root, "/"))
		root = root.substr(0, root.size()-1);
	root_directory = root;
}

/** Fetches a MusicListing object from the given \p path.
 *  Prefixes the given path with the music root directory.
 *  This can be either a track (file) or a directory.
 */
MusicListing::p music::get(const HTTP::Connection::PathList& path) {
	// prefix path with our root_directory
	fs::path p = root_directory;
	
	for(HTTP::Connection::PathList::const_iterator it = path.begin(); it != path.end(); it++) {
		// don't allow requests with /../ in the path
		if(*it == "..") {
			return MusicListing::p();
		}
		
		p /= *it;
	}
	
	if(fs::is_directory(p)) {
		boost::shared_ptr<MusicListing> ml(new MusicDirectory(p));
		return ml;
	} else if(fs::is_regular_file(p)) {
		boost::shared_ptr<MusicListing> ml(new MusicTrack(p));
		return ml;
	}

	return MusicListing::p();
}

/** Fetches a MusicListing object from the given \p path.
 *  Splits the given string and calls the above function.
 */
MusicListing::p music::get(const std::string& path) {
	HTTP::Connection::PathList path_vector;
	boost::algorithm::split(path_vector, path, boost::algorithm::is_any_of("/\\"));
	return get(path_vector);
}

/** Fetches a directory.
 *  This is a helper function which returns a null pointer if the fetched MusicListing isn't a directory.
 */
MusicDirectory::p music::get_directory(const std::string& path) {
	MusicListing::p ml = get(path);
	if(!ml || !fs::is_directory(ml->path)) {
		return MusicDirectory::p();
	}
	MusicDirectory::p dir(boost::dynamic_pointer_cast<MusicDirectory>(ml));

	return dir;
}

/** Find tracks in the database.
 *  Does a search on specific fields given by \p search.
 */
std::vector<MusicListing::p> music::find(std::map<std::string, std::string> search) {
	Database db;
	return db.find(search);
}

/** Find tracks in the database.
 *  Returns tracks where title, artist, album or filename matches \p search.
 */
std::vector<MusicListing::p> music::find(std::string search) {
	Database db;
	return db.find(search);
}

/** Initiate an update on \p path and its subdirs.
 *  music::update does the actual work.
 */
void music::begin_update(const std::string path) {
	MusicDirectory::p dir = get_directory(path);
	std::cout << boost::format("updater(%s) called") % path << std::endl;
	if(dir) {
		Database db;
		dir->update(db);
	}
}

/** Recursively update \p dir and its subdirectories.
 */
void MusicDirectory::update(Database& db) {
	BOOST_FOREACH(fs::path track, tracks) {
		std::cout << "track " << track << std::endl;
		Tag::p tag = Tag::load(track.string());
		BOOST_FOREACH(Tag::Fields::value_type& f, tag->fields) {
			std::cout << boost::format("  %s: %s") % f.first % f.second << std::endl;
		}

		db.update(track, tag);
	}

	for(PathListings::iterator it = directories.begin(); it != directories.end(); it++) {
		MusicDirectory dir(*it);
		dir.update(db);
	}
}

void MusicDirectory::render(HTTP::Connection::p req) {
	req->add_header("content-type", "text/html");

	std::string base_path = boost::algorithm::join(req->base_path, "/");

	// strip root_directory from the absolute file path
	std::string rel_base(path.string().substr(music::root_directory.string().size()));
	// set relative base to "/" if we're at the root_directory
	if(!rel_base.size())
		rel_base = "/";
	req->send_data(boost::str(boost::format("<h1>%s</h1>") % rel_base));

	// link to parent directory if we're not at the root_directory
	if(rel_base != "/") {
		std::string rel_parent(rel_base.substr(0, rel_base.find_last_of('/')));
		req->send_data(boost::str(boost::format("<a href=\"/%s%s/\">..</a><br />") % base_path % rel_parent));
	}

	for(PathListings::iterator it = directories.begin(); it != directories.end(); it++) {
		std::string rel_path = it->string().substr(music::root_directory.string().size());
		req->send_data(boost::str(boost::format("<a href=\"/%s%s/\">%s</a><br />") % base_path % rel_path % it->filename()));
	}
	for(PathListings::iterator it = tracks.begin(); it != tracks.end(); it++) {
		std::string rel_path = it->string().substr(music::root_directory.string().size());
		req->send_data(boost::str(boost::format("<a href=\"/%s%s\">%s</a><br />") % base_path % rel_path % it->filename()));
	}
}

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 << this->path << std::endl;

	fs::directory_iterator end_itr;
	for(fs::directory_iterator it(this->path); it != end_itr; it++) {
		if(fs::is_directory(it->status())) {
			directories.push_back(it->path());
		} else if(fs::is_regular_file(it->status())) {
			tracks.push_back(it->path());
		}
	}
}

void MusicTrack::render(HTTP::Connection::p req) {
	req->add_header("Content-Type", "audio/mpeg");
	fs::path file_path = path;
	
	int skip = 0;
	int range = 0;
	
	if(req->headers.count("Range")) {
		if(req->headers["Range"] == "bytes=0-1024") {
			range = 1025;
		} else {
			skip = 1025;
		}
	}
	
	if(req->args.count("decoder") && req->args.count("encoder")) {
		Decoder::p decoder = Decoder::get(req->args["decoder"], path.string());
		Encoder::p encoder = Encoder::get(req->args["encoder"], decoder);
		
		EncodedCache ec(path, decoder, encoder);
		file_path = ec.get_path();
		if(!fs::exists(file_path))
			ec.create_cache();
		// TODO: Make an encoder-to-istream adapter to get rid of this:
		char data[0x10000];
		std::streamsize size = 1;
		
		if(skip) {
			size = encoder->read(data, skip);
		}
		
		if(range) {
			size = encoder->read(data, range);
			if(size > 0) {
				req->send_data(data, size);
			}
			return;
		}
		
		while(size) {
			size = encoder->read(data, 0x10000);
			if(size > 0)
				req->send_data(data, size);
		}
	}

	req->send_file(path);

	/*
	fs::ifstream is(file_path, std::ios::in | std::ios::binary);
	is.seekg(0, std::ios::end);
	req->add_header("content-length", boost::str(boost::format("%d") % is.tellg()));
	is.seekg(0, std::ios::beg);

	req->send_data(is);
	*/
}