Blame view

server/php/DownloadExport.php 2.27 KB
346b85c6   Benjamin Renard   First commit with...
1
2
3
4
5
6
<?php

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

$op_id = $_REQUEST['id'];
92b37092   Laurent BEIGBEDER   #6610: génération...
7
$file_name = $_REQUEST['filename'];
346b85c6   Benjamin Renard   First commit with...
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

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";
}

92b37092   Laurent BEIGBEDER   #6610: génération...
55
56
//set name only if from local browse input mode
if($file_name) {
0c120677   Laurent BEIGBEDER   #6610: date added...
57
	$file_info["name"] = $file_name."_".date("Ymdhis").$file_info["extension"];
92b37092   Laurent BEIGBEDER   #6610: génération...
58
59
60
61
}else {
	$file_info["name"] = "treps_export_".$op_id.$file_info["extension"];
}

346b85c6   Benjamin Renard   First commit with...
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

//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;

?>