From 48e97e7cd1cf10cc8b0e7cae7b65afb525ff7648 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Sun, 25 Aug 2019 22:21:21 -0400 Subject: [PATCH] Added support for saving downloading song. It doesn't work outside of curl from the command line. The response body is empty, I'll look into it --- src/controller/songController.hpp | 41 ++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/src/controller/songController.hpp b/src/controller/songController.hpp index b293d25..ae02bc7 100644 --- a/src/controller/songController.hpp +++ b/src/controller/songController.hpp @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -132,12 +133,39 @@ public: songDb.id = id; songDb = songRepo.retrieveRecord(songDb, songFilter::id); + std::cout << "song path " << songDb.songPath << std::endl; + + std::ifstream fl(songDb.songPath.c_str(), std::ios::in | std::ios::binary | std::ios::ate); + + //auto rawSong = oatpp::String((v_int32) fl.tellg()); + fl.seekg(0); + //fl.read((char*)rawSong->getData(), rawSong->getSize()); + + std::stringstream buf; + std::copy(std::istreambuf_iterator(fl), + std::istreambuf_iterator(), + std::ostreambuf_iterator(buf) + ); + fl.close(); + + //std::cout << buf.str().c_str() << std::endl; + + //auto raw = buf.str(); + + auto rawSong = std::make_shared(oatpp::String(buf.str().data(), (v_int32)buf.str().size(), true)); + //std::cout << rawSong->c_str() << std::endl; + //std::cout << rawSong->std_str() << std::endl; + //std::shared_ptr rawSong = std::make_shared(); + //rawSong->loadFromFile(songDb.songPath.c_str()); + + /** std::cout << "constructing FileInputStream" << std::endl; oatpp::data::stream::FileInputStream file(songDb.songPath.c_str()); //oatpp::data::stream::FileOutputStream file(songDb.songPath.c_str()); auto songPath = fs::path(songDb.songPath); auto songSize = fs::file_size(songPath); std::cout << "prepping data" << std::endl; + std::cout << "byte size " << songSize << std::endl; auto data = new char[songSize]; std::cout << "data will be loaded" << std::endl; auto byteCount = file.read(&data, songSize); @@ -151,8 +179,19 @@ public: delete[] data; std::cout << "sending file" << std::endl; + */ + /** + auto songData = oatpp::data::stream::ChunkedBuffer::createShared(); + songData->write(buf.str().data(), buf.str().size()); - return createResponse(Status::CODE_200, songData); + //auto response = createResponse(Status::CODE_200, songData); + */ + auto response = createResponse(Status::CODE_200, *rawSong); + response->putHeader("Accept-Ranges", "bytes"); + response->putHeader(Header::CONNECTION, Header::Value::CONNECTION_KEEP_ALIVE); + response->putHeader(Header::CONTENT_TYPE, "audio/mpeg"); + + return response; } #include OATPP_CODEGEN_END(ApiController)