Commit 0a31e92d authored by Oleg Dzhimiev's avatar Oleg Dzhimiev

default option set to download single zip

parent 49717aca
......@@ -128,7 +128,7 @@ function respond_xml($result,$error=null,$color_mode = 3){ // default white bold
// PARALLEL HTTP REQUESTS
// Using parallel requests, PHP has to be configured '--with-curl=' (and libcurl should be installed)
function curl_multi_start($urls) {
function curl_multi_start($urls, $with_headers=0) {
// numprime is needed to actually send the request and remote starts executing it
// Not really clear - what it should be
$numprime = 4; // magic number, see http://lampe2e.blogspot.com/2015/03/making-stuff-faster-with-curlmultiexec.html
......@@ -138,7 +138,7 @@ function curl_multi_start($urls) {
$ch = curl_init ();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_HEADER, $with_headers);
$aCurlHandles[] = $ch;
curl_multi_add_handle ($curl_mh, $ch);
}
......@@ -152,7 +152,7 @@ function curl_multi_start($urls) {
return array ("mh" => $curl_mh,"handles" => $aCurlHandles);
}
function curl_multi_finish($data, $use_xml=true, $ntry=0, $echo = false) {
function curl_multi_finish($data, $use_xml=true, $ntry=0, $echo = false, $with_headers = 0) {
$curl_active = 1;
$curl_mrc = CURLM_OK;
$nrep = 0;
......@@ -170,6 +170,7 @@ function curl_multi_finish($data, $use_xml=true, $ntry=0, $echo = false) {
}
}
$results = array ();
$names = array ();
if ($use_xml) {
foreach ($data['handles'] as $i => $ch) {
$xml = simplexml_load_string (curl_multi_getcontent ($ch));
......@@ -187,13 +188,50 @@ function curl_multi_finish($data, $use_xml=true, $ntry=0, $echo = false) {
foreach ($data['handles'] as $i => $ch) {
$r = curl_multi_getcontent ($ch);
curl_multi_remove_handle ($curl_mh, $ch);
if ($with_headers==1){
$hsize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$h = substr($r, 0, $hsize);
$r = substr($r, $hsize);
$names[] = curl_get_filename_from_header($h);
}
$results[] = $r;
}
if ($with_headers==1){
$results = Array('names' => $names,'contents'=> $results);
}
}
curl_multi_close ($curl_mh);
return $results;
}
function curl_get_filename_from_header($h){
$h = explode("\n",$h);
$res = "";
if ($h){
foreach($h as $e){
if (strlen($e)!=0){
$tmp = explode(":",$e);
if ($tmp[0]=="Content-Disposition"){
$tmp = explode(";",$tmp[1]);
foreach($tmp as $t){
$t = trim($t);
$t = explode("=",$t);
if ($t[0]=="filename"){
$res = trim($t[1]," \t\n\r\0\x0B\"");
break;
}
}
break;
}
}
}
}
return $res;
}
// DEVICES
/** Get a list of suitable partitions. The list will contain SATA devices only and
......
......@@ -22,6 +22,8 @@
?>
<?php
include "../include/elphel_functions_include.php";
$port0 = 2323;
$path = "/sys/devices/soc0/elphel393-detect_sensors@0";
$available_ports = Array();
......@@ -99,48 +101,53 @@
if (isset($_GET['zip'])){
$tmpdir = "/tmp/snapshot";
// create tmp dir
if (!is_dir($tmpdir)){
mkdir($tmpdir);
$contents = Array();
$filenames = Array();
$rqs = Array();
foreach($available_ports as $port){
array_push($rqs,"http://{$_SERVER['SERVER_ADDR']}:$port/timestamp_name/bimg");
}
$cdata = curl_multi_start($rqs,1);
$results = curl_multi_finish($cdata,false,0,false,1);
$filenames = $results['names'];
$contents = $results['contents'];
/*
foreach($available_ports as $port){
exec("wget --content-disposition -P $tmpdir http://{$_SERVER['SERVER_ADDR']}:$port/timestamp_name/bimg");
//exec("wget --content-disposition -P $tmpdir http://{$_SERVER['SERVER_ADDR']}:$port/timestamp_name/bimg");
//fopen("http://{$_SERVER['SERVER_ADDR']}:$port/timestamp_name/bimg",'r');
$content = file_get_contents("http://{$_SERVER['SERVER_ADDR']}:$port/timestamp_name/bimg");
$filename = get_filename_from_headers($http_response_header);
if ($filename==""){
$filename = "bimg.jp4";
}
array_push($filenames,$filename);
array_push($contents,$content);
}
*/
$fstring = "";
$zipfile = "bimg.zip";
$zipfilename = preg_replace("/_\d+\.jp4$/",".zip",$filenames[0]);
$files = scandir($tmpdir);
//remove . & ..
array_splice($files,0,2);
//tmpfile
$tmpfile = tmpfile();
$tmpfilename = stream_get_meta_data($tmpfile)['uri'];
$prefixed_files = preg_filter('/^/', "$tmpdir/", $files);
$fstring = implode(" ",$prefixed_files);
$zip = new ZipArchive;
// pick name for the zip archive
foreach($files as $file){
$tmp = explode(".",$file);
if ($tmp[1]=="jp4"||$tmp[1]=="jpeg"){
$tmp = explode("_",$tmp[0]);
if ($tmp[2]=="0"){
$zipfile = $tmp[0]."_".$tmp[1].".zip";
break;
}
if ($zip->open($tmpfilename, ZipArchive::OVERWRITE)!==TRUE) {
die("cannot access temporary file <$tmpfilename>\n");
}
foreach($filenames as $k=>$v){
$zip->addFromString($v,$contents[$k]);
}
$zipped_data = `zip -qj - $fstring `;
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zipfile.'"');
echo $zipped_data;
$zip->close();
// clean up
foreach($prefixed_files as $file){
unlink($file);
}
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zipfilename.'"');
echo file_get_contents($tmpfilename);
die();
......
......@@ -59,6 +59,8 @@ function download_all(rtp){
pass_to_file_reader(filename,http.response);
if ($("#synced").prop("checked")) {
read_tp();
}else{
$("#snapshot").attr("disabled",false);
}
}
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment