DownloadExport.php 2.08 KB
<?php

require_once("RequestManager.php");
require_once("Constants.php");

$op_id = $_REQUEST['id'];

if (empty($op_id))
{
	header("HTTP/1.0 400 Bad Request");
	echo json_encode(array("success" => false, "error" => "Cannot get operation id"));
	exit;
}

//run request
$reqMgr = new RequestManager();
$res = $reqMgr->run('export_result',array('id' => $op_id),OutputTypeEnum::OUTPUT_RESULTFILE);

if (!$res["success"])
{
	header("HTTP/1.0 400 Bad Request");
	echo json_encode(array("success" => true, "error" => "Error to retrieve exported file"));
	exit;
}

//get result file info
$file_info = array();
$file_info["path"] = $res["path"];
$file_info["format"] = $res["format"];

switch($file_info["format"])
{
	case "ascii" :
		$file_info["extension"] = ".txt";
		$file_info["mimetype"]  = "text/plain";
		break;
	case "votable" :
		$file_info["extension"] = ".xml";
		$file_info["mimetype"]  = "application/xml";
		break;
	case "cdf" :
		$file_info["extension"] = ".cdf";
		$file_info["mimetype"]  = "application/octet-stream";
		break;
	case "netcdf" :
		$file_info["extension"] = ".nc";
		$file_info["mimetype"]  = "application/octet-stream";
		break;
	default :
		$file_info["extension"] = "";
		$file_info["mimetype"]  = "text/plain";
}

$file_info["name"] = "treps_export_".$op_id.$file_info["extension"];

//make sure the file exists
if (!is_file($file_info["path"]))
{
	header("HTTP/1.0 400 Bad Request");
	echo json_encode(array("success" => true, "error" => "Exported file don't exist anymore"));
	exit;
}

$file_info["size"]  = filesize($file_info["path"]);

//hide notices
@ini_set('error_reporting', E_ALL & ~ E_NOTICE);

//turn off compression on the server
@apache_setenv('no-gzip', 1);
@ini_set('zlib.output_compression', 'Off');

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate");
header('Content-Description: File Transfer');
header("Content-Type: ".$file_info["mimetype"]);
header("Content-Disposition: attachment; filename=\"".$file_info['name']."\"");
header("Content-Length: ".$file_info["size"]);
ob_clean();
flush();

//read file
readfile($file_info["path"]);
exit;

?>