summaryrefslogtreecommitdiff
path: root/music.cpp
blob: f74de9d7ac84e40c8a61ac302906d5c2c20761b1 (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
#include "music.h"
#include "decoder.h"
#include "encoder.h"
#include "transcode.h"
#include "tag.h"
#include "config.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/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;
}

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();
}

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);
}

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;
}

std::vector<MusicListing::p> music::find_artist(const std::string artist) {
	soci::session sql(config::vm["audist.database"].as<std::string>());

	soci::rowset<std::string> rs = (sql.prepare << "SELECT file_name FROM tracks WHERE artist_id IN (SELECT id FROM artists WHERE name LIKE :name)",
		soci::use("%"+artist+"%"));

	std::vector<MusicListing::p> results;
	for(soci::rowset<std::string>::const_iterator it = rs.begin(); it != rs.end(); it++) {
		boost::shared_ptr<MusicListing> ml(new MusicTrack(*it));
		results.push_back(ml);
	}

	return results;
}

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) {
		update(dir->path);
	}
}

void music::update(const MusicDirectory& dir) {
	soci::session sql(config::vm["audist.database"].as<std::string>());

	BOOST_FOREACH(fs::path t, dir.tracks) {
		std::cout << "track " << t << std::endl;
		Tag::p tag = Tag::load(t.string());
		BOOST_FOREACH(Tag::Fields::value_type& f, tag->fields) {
			std::cout << boost::format("  %s: %s") % f.first % f.second << std::endl;
		}

		int artist_id = 0, album_id = 0, track_id = 0;

		if(tag->has_field("artist")) {
			sql << "SELECT id FROM artists WHERE name = :name", soci::use(tag->fields["artist"]), soci::into(artist_id);
			if(!sql.got_data())
				sql << "INSERT INTO artists (name) VALUES (:name) RETURNING id", soci::use(tag->fields["artist"]), soci::into(artist_id);
		}

		if(tag->has_field("album")) {
			std::string query(boost::str(boost::format("SELECT id FROM albums WHERE %s AND name = :name") %
						(artist_id ? boost::str(boost::format("artist_id = %d") % artist_id) : "artist_id IS NULL")));
			sql << query, soci::use(tag->fields["album"]), soci::into(album_id);
			if(!sql.got_data()) {
				soci::indicator ind = (artist_id ? soci::i_ok : soci::i_null);
				sql << "INSERT INTO albums (artist_id, name) VALUES (:artist_id, :name) RETURNING id",
					soci::use(artist_id, ind), soci::use(tag->fields["album"]), soci::into(album_id);
			}
		}

		if(tag->has_field("title")) {
			std::string query(boost::str(boost::format("SELECT id FROM tracks WHERE %s AND %s AND name = :name") %
						(artist_id ? boost::str(boost::format("artist_id = %d") % artist_id) : "artist_id IS NULL") %
						(album_id ? boost::str(boost::format("album_id = %d") % album_id) : "album_id IS NULL")));
			sql << query, soci::use(tag->fields["title"]), soci::into(track_id);
			if(!sql.got_data()) {
				soci::indicator artist_ind = (artist_id ? soci::i_ok : soci::i_null),
					album_ind = (album_id ? soci::i_ok : soci::i_null);
				sql << "INSERT INTO tracks (artist_id, album_id, name, file_name) VALUES (:artist_id, :album_id, :name, :file_name)",
					soci::use(artist_id, artist_ind), soci::use(album_id, album_ind), soci::use(tag->fields["title"]), soci::use(t.string());
			}
		}
	}
	sql.close();

	std::for_each(dir.directories.begin(), dir.directories.end(), update);
}

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

	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=\"/files%s\">%s</a><br />") % rel_path % rel_path));
	}
	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=\"/files%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 << 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", "application/octet-stream");

	if(req->args.count("decoder") && req->args.count("encoder")) {
		DecoderFilter::p d = decoder::get_decoder(req->args["decoder"]);
		EncoderFilter::p e = encoder::get_encoder(req->args["encoder"]);
		Transcoder t(path.string(), req, d, e);
		t.run();
	} else {
		fs::ifstream is(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);
	}
}