download.php 1.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<?php
/*

  Copyright (C) 2017 Elphel Inc.

  SPDX-License-Identifier: AGPL-3.0+

  https://www.elphel.com

*/

// https://stackoverflow.com/questions/1061710/php-zip-files-on-the-fly
// * solution for ZipArchive worked only from a command line
// * solution that worked: command line zip

// contstants
$TEXTURE_EXTENSIONS = ['png','jpeg','jpg','tif','tiff','gif'];

// check if file parameter is specified
if (!isset($_GET['file'])) die("-1");

$file = $_GET['file'];
23
$filestring = $file;
24 25 26 27 28 29 30 31 32 33 34 35 36

$patterns = ['/(\.)+\//','/^\//'];
$replacements = ['',''];

// check if file exists
if (!is_file($file)) die("-2");
if (substr($file,-4)!=".x3d") die("-3");

$pathinfo = pathinfo($file);
$path = $pathinfo['dirname'];

$tmp = explode("/",$path);

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
37
$zipfile =  (array_slice($tmp,-2)[0])."_".(array_slice($tmp,-1)[0]).".zip";
38 39 40 41 42 43 44 45 46

// alright, there's this file
$contents = file_get_contents($file);

// extract file list
preg_match_all('/url="([^\s]+('.implode('|',$TEXTURE_EXTENSIONS).'))"/i',$contents,$matches);

// make a string
foreach($matches[1] as $v){
47
//	$filestring .= " \\\n$path/$v"; // split lines do not help, limit is for the php string length in shell_exec()
48
  $filestring .= " $path/$v";
49 50
}

51
// add obj
52 53
$objfile = substr($file,0,-3)."obj";
if (is_file($objfile)) $filestring .= " $objfile";
54 55

// add mtl
56 57
$mtlfile = substr($file,0,-3)."mtl";
if (is_file($mtlfile)) $filestring .= " $mtlfile";
58

59
$zipped_data = `zip -qj - $filestring `;
60 61 62 63 64
if (strlen($zipped_data) == 0){
	$filestring=$path."/*.*"; // too long filestring, just compress everything in the model directory
	$zipped_data = `zip -qj - $filestring `;
}

65 66 67 68 69
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zipfile.'"');
echo $zipped_data;

?>