Project

General

Profile

RE: Aynchronous file creation for response ยป test.cpp

Somsubhra Sharangi, 06/11/2012 03:49 PM

 
#include <Wt/WServer>
#include <Wt/WResource>
#include <Wt/Http/Response>
#include <Wt/WFileResource>
#include <fstream>

using namespace Wt;

class GhostFileResource : public Wt::WResource
{
public:
virtual ~GhostFileResource(){
beingDeleted();
}
protected:
virtual void handleRequest(const Wt::Http::Request &request,Wt::Http::Response &response){
std::string fetch_command_prefix = "wget http://www.webtoolkit.eu/videos";
std::string resource_param = request.path();
std::string dynamic_command = fetch_command_prefix + resource_param;
std::cout<< dynamic_command <<std::endl;
system(dynamic_command.c_str());
//maybe do some more processing here to finally generate the file
//WFileResource *file = new WFileResource( resource_param.c_str() );
std::ifstream file(request.path().c_str(), std::ios::in | std::ios::binary);
response.setMimeType ("video/mp4");
response.out() << file.rdbuf();
//response.out() << "Hi";
}
};

int main(int argc, char **argv)
{
try {
WServer server(argv[0]);
server.setServerConfiguration(argc, argv);

GhostFileResource gfr;
//ask for a file that currently does not exist on server
server.addResource(&gfr, "/sintel_trailer.mp4");

if (server.start()) {
WServer::waitForShutdown();
server.stop();
}
} catch (WServer::Exception& e) {
std::cerr << e.what() << std::endl;
} catch (std::exception &e) {
std::cerr << "exception: " << e.what() << std::endl;
}
}

    (1-1/1)