download.php 1.45 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);

37 38 39 40 41
if ($tmp[1]=="_all"){
  $zipfile = $tmp[2]."_".$tmp[3].".zip";
}else{
  $zipfile = $tmp[1]."_".$tmp[2].".zip";
}
42 43 44 45 46 47 48 49 50

// 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){
51
  $filestring .= " $path/$v";
52 53
}

54
// add obj
55 56
$objfile = substr($file,0,-3)."obj";
if (is_file($objfile)) $filestring .= " $objfile";
57 58

// add mtl
59 60
$mtlfile = substr($file,0,-3)."mtl";
if (is_file($mtlfile)) $filestring .= " $mtlfile";
61

62
$zipped_data = `zip -qj - $filestring `;
63 64 65 66 67
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zipfile.'"');
echo $zipped_data;

?>