summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--commands.c62
1 files changed, 61 insertions, 1 deletions
diff --git a/commands.c b/commands.c
index 21b0703..8a9efff 100644
--- a/commands.c
+++ b/commands.c
@@ -1,5 +1,8 @@
#include "commands.h"
#include "music.h"
+#include "transcode.h"
+#include "decoder.h"
+#include "encoder.h"
#include <glib.h>
#include <string.h>
@@ -107,7 +110,7 @@ static void commands_get_raw(GSocketConnection *connection, const gchar *cmd) {
GString *string = g_string_new(NULL);
g_string_append(string, "HTTP/1.1 200 OK\r\n");
- g_string_append(string, "content-type: octet-stream\r\n");
+ g_string_append(string, "content-type: application/octet-stream\r\n");
g_string_append_printf(string, "content-length: %lu\r\n", filesize);
g_string_append(string, "\r\n");
@@ -141,12 +144,69 @@ commands_get_raw_free_path:
g_free(path);
}
+static void commands_get_mp3(GSocketConnection *connection, const gchar *cmd) {
+ GError *error = NULL;
+ gchar **data = g_strsplit(cmd, " ", 2);
+ g_assert(data[0] != NULL && data[1] != NULL);
+
+ gchar *path = g_strdup(data[1]);
+ g_strfreev(data);
+
+ struct file *f = music_find_file(path);
+
+ if(f == NULL) {
+ g_warning("couldn't find %s", path);
+ send_404(connection);
+ goto commands_get_mp3_free_path;
+ }
+
+ GOutputStream *os = g_io_stream_get_output_stream((GIOStream*)connection);
+ GFile *file = g_file_new_for_path(path);
+ GFileInputStream *is = g_file_read(file, NULL, &error);
+
+ if(is == NULL) {
+ g_warning(error->message);
+ g_error_free(error);
+ goto commands_get_mp3_file_unref;
+ }
+
+ GString *string = g_string_new(NULL);
+ g_string_append(string, "HTTP/1.1 200 OK\r\n");
+ g_string_append(string, "content-type: application/octet-stream\r\n");
+ g_string_append(string, "\r\n");
+
+ if(g_output_stream_write_all(os, string->str, string->len, NULL, NULL,
+ &error) == FALSE) {
+ g_warning(error->message);
+ g_error_free(error);
+ g_string_free(string, TRUE);
+ goto commands_get_mp3_file_unref;
+ }
+
+ g_string_free(string, TRUE);
+
+ extern const struct decoder_plugin decoder_mpg123_decoder;
+ extern const struct encoder_plugin encoder_lame_encoder;
+
+ transcode((GInputStream*)is, &decoder_mpg123_decoder, os, &encoder_lame_encoder);
+
+commands_get_mp3_file_unref:
+
+ g_object_unref(is);
+ g_object_unref(file);
+
+commands_get_mp3_free_path:
+ g_free(path);
+}
+
void commands_handle(GSocketConnection *connection, const gchar *cmd) {
g_debug("handling command string %s", cmd);
if(g_ascii_strncasecmp(cmd, "/list", 5) == 0) {
commands_list(connection, cmd);
} else if(g_ascii_strncasecmp(cmd, "/get_raw", 8) == 0) {
commands_get_raw(connection, cmd);
+ } else if(g_ascii_strncasecmp(cmd, "/get_mp3", 8) == 0) {
+ commands_get_mp3(connection, cmd);
} else {
g_warning("no command handlers found");
}