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

default option set to download single zip

parent 49717aca
Loading
Loading
Loading
Loading
+49 −11
Original line number Original line Diff line number Diff line
@@ -128,7 +128,7 @@ function respond_xml($result,$error=null,$color_mode = 3){ // default white bold
// PARALLEL HTTP REQUESTS
// PARALLEL HTTP REQUESTS


// Using parallel requests, PHP has to be configured '--with-curl=' (and libcurl should be installed)
// 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
	// numprime is needed to actually send the request and remote starts executing it
	// Not really clear - what it should be
	// Not really clear - what it should be
	$numprime = 4; // magic number, see http://lampe2e.blogspot.com/2015/03/making-stuff-faster-with-curlmultiexec.html
	$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 ();
		$ch = curl_init ();
		curl_setopt ($ch, CURLOPT_URL, $url);
		curl_setopt ($ch, CURLOPT_URL, $url);
		curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt ($ch, CURLOPT_HEADER, 0);
		curl_setopt ($ch, CURLOPT_HEADER, $with_headers);
		$aCurlHandles[] = $ch;
		$aCurlHandles[] = $ch;
		curl_multi_add_handle ($curl_mh, $ch);
		curl_multi_add_handle ($curl_mh, $ch);
	}
	}
@@ -152,7 +152,7 @@ function curl_multi_start($urls) {
	return array ("mh" => $curl_mh,"handles" => $aCurlHandles);
	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_active = 1;
	$curl_mrc = CURLM_OK;
	$curl_mrc = CURLM_OK;
	$nrep = 0;
	$nrep = 0;
@@ -170,6 +170,7 @@ function curl_multi_finish($data, $use_xml=true, $ntry=0, $echo = false) {
		}
		}
	}
	}
	$results = array ();
	$results = array ();
	$names = array ();
	if ($use_xml) {
	if ($use_xml) {
		foreach ($data['handles'] as $i => $ch) {
		foreach ($data['handles'] as $i => $ch) {
			$xml = simplexml_load_string (curl_multi_getcontent ($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) {
		foreach ($data['handles'] as $i => $ch) {
			$r = curl_multi_getcontent ($ch);
			$r = curl_multi_getcontent ($ch);
			curl_multi_remove_handle ($curl_mh, $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;
			$results[] = $r;
		}
		}
		if ($with_headers==1){
			$results = Array('names' => $names,'contents'=> $results);
		}
	}
	}
	curl_multi_close ($curl_mh);
	curl_multi_close ($curl_mh);
	return $results;
	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
// DEVICES


/** Get a list of suitable partitions. The list will contain SATA devices only and
/** Get a list of suitable partitions. The list will contain SATA devices only and
+38 −31
Original line number Original line Diff line number Diff line
@@ -22,6 +22,8 @@
?>
?>
<?php
<?php


  include "../include/elphel_functions_include.php";

  $port0 = 2323;
  $port0 = 2323;
  $path = "/sys/devices/soc0/elphel393-detect_sensors@0";
  $path = "/sys/devices/soc0/elphel393-detect_sensors@0";
  $available_ports = Array();
  $available_ports = Array();
@@ -99,48 +101,53 @@


    if (isset($_GET['zip'])){
    if (isset($_GET['zip'])){


      $tmpdir = "/tmp/snapshot";
      $contents = Array();

      $filenames = Array();
      // create tmp dir
      $rqs = Array();
      if (!is_dir($tmpdir)){
      foreach($available_ports as $port){
        mkdir($tmpdir);
        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){
      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 = "";
      $zipfilename = preg_replace("/_\d+\.jp4$/",".zip",$filenames[0]);
      $zipfile = "bimg.zip";


      $files = scandir($tmpdir);
      //tmpfile
      //remove . & ..
      $tmpfile = tmpfile();
      array_splice($files,0,2);
      $tmpfilename = stream_get_meta_data($tmpfile)['uri'];


      $prefixed_files = preg_filter('/^/', "$tmpdir/", $files);
      $zip = new ZipArchive;
      $fstring = implode(" ",$prefixed_files);


      // pick name for the zip archive
      if ($zip->open($tmpfilename, ZipArchive::OVERWRITE)!==TRUE) {
      foreach($files as $file){
        die("cannot access temporary file <$tmpfilename>\n");
        $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;
          }
      }
      }

      foreach($filenames as $k=>$v){
        $zip->addFromString($v,$contents[$k]);
      }
      }


      $zipped_data = `zip -qj - $fstring `;
      $zip->close();
      header('Content-type: application/zip');
      header('Content-Disposition: attachment; filename="'.$zipfile.'"');
      echo $zipped_data;


      // clean up
      header('Content-type: application/zip');
      foreach($prefixed_files as $file){
      header('Content-Disposition: attachment; filename="'.$zipfilename.'"');
        unlink($file);
      echo file_get_contents($tmpfilename);
      }


      die();
      die();


+2 −0
Original line number Original line Diff line number Diff line
@@ -59,6 +59,8 @@ function download_all(rtp){
          pass_to_file_reader(filename,http.response);
          pass_to_file_reader(filename,http.response);
          if ($("#synced").prop("checked")) {
          if ($("#synced").prop("checked")) {
            read_tp();
            read_tp();
          }else{
            $("#snapshot").attr("disabled",false);
          }
          }
        }
        }
      };
      };