add: chunk conten_provider for file download
This commit is contained in:
@@ -192,15 +192,32 @@ void Api::file(const httplib::Request& rq, httplib::Response& rs)
|
||||
sql << fmt::format("SELECT * FROM Files WHERE hash='{}';", file), soci::into(dw);
|
||||
|
||||
|
||||
std::ifstream fs(dw.local_path, std::ios_base::binary);
|
||||
fs.seekg(0, std::ios_base::end);
|
||||
auto size = fs.tellg();
|
||||
fs.seekg(0);
|
||||
rs.body.resize(static_cast<size_t>(size));
|
||||
fs.read(&rs.body[0], static_cast<std::streamsize>(size));
|
||||
auto fs = std::make_shared<std::ifstream>(dw.local_path, std::ios_base::binary | std::ios::in);
|
||||
fs->seekg(0, std::ios_base::end);
|
||||
auto size = fs->tellg();
|
||||
fs->seekg(0);
|
||||
|
||||
// rs.body.resize(static_cast<size_t>(size));
|
||||
// fs.read(&rs.body[0], static_cast<std::streamsize>(size));
|
||||
|
||||
rs.set_header("Content-Disposition", fmt::format("attachment; filename={};", dw.filename));
|
||||
rs.set_header("content-type", fmt::format("application/{}", dw.format));
|
||||
|
||||
rs.set_content_provider(
|
||||
static_cast<size_t>(size),
|
||||
fmt::format("application/{}", dw.format).c_str(),
|
||||
[file = fs](size_t offset, size_t length, httplib::DataSink& sink) mutable {
|
||||
size_t size = std::min(length, (size_t)1024 * 500);
|
||||
|
||||
//spdlog::info("Stream offset {} length {}", offset, length);
|
||||
std::vector<char> data;
|
||||
data.resize(size);
|
||||
|
||||
file->seekg(offset);
|
||||
file->read(data.data(), data.size());
|
||||
|
||||
sink.write(data.data(), data.size());
|
||||
return true;
|
||||
});
|
||||
}
|
||||
catch (std::exception const& e)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user