void handleFileExportRequest(const http::Request& request, http::Response* pResponse) { // see if this is a single or multiple file request std::string file = request.queryParamValue("file"); if (!file.empty()) { // resolve alias and ensure that it exists FilePath filePath = module_context::resolveAliasedPath(file); if (!filePath.exists()) { pResponse->setError(http::status::NotFound, "file doesn't exist"); return; } // get the name std::string name = request.queryParamValue("name"); if (name.empty()) { pResponse->setError(http::status::BadRequest, "name not specified"); return; } // download as attachment setAttachmentResponse(request, name, filePath, pResponse); } else { handleMultipleFileExportRequest(request, pResponse); } }
void handleFileShow(const http::Request& request, http::Response* pResponse) { // get the file path FilePath filePath(request.queryParamValue("path")); if (!filePath.exists()) { pResponse->setNotFoundError(request.uri()); return; } // send it back pResponse->setCacheWithRevalidationHeaders(); pResponse->setCacheableFile(filePath, request); }
void handleMultipleFileExportRequest(const http::Request& request, http::Response* pResponse) { // name parameter std::string name = request.queryParamValue("name"); if (name.empty()) { pResponse->setError(http::status::BadRequest, "name not specified"); return; } // parent parameter std::string parent = request.queryParamValue("parent"); if (parent.empty()) { pResponse->setError(http::status::BadRequest, "parent not specified"); return; } FilePath parentPath = module_context::resolveAliasedPath(parent); if (!parentPath.exists()) { pResponse->setError(http::status::BadRequest, "parent doesn't exist"); return; } // files parameters (paths relative to parent) std::vector<std::string> files; for (int i=0; ;i++) { // get next file (terminate when we stop finding files) std::string fileParam = "file" + boost::lexical_cast<std::string>(i); std::string file = request.queryParamValue(fileParam); if (file.empty()) break; // verify that the file exists FilePath filePath = parentPath.complete(file); if (!filePath.exists()) { pResponse->setError(http::status::BadRequest, "file " + file + " doesn't exist"); return; } // add it files.push_back(file); } // create the zip file FilePath tempZipFilePath = module_context::tempFile("export", "zip"); Error error = r::exec::RFunction(".rs.createZipFile", tempZipFilePath.absolutePath(), parentPath.absolutePath(), files).call(); if (error) { LOG_ERROR(error); pResponse->setError(error); return; } // return attachment setAttachmentResponse(request, name, tempZipFilePath, pResponse); }