<?php
include_once("../../../auth.php");
if (isset($_GET['chunkid']) && $_GET['chunkid'] != "" && isset($_GET['outfile']) && $_GET['outfile'] != ""){
	if (!file_exists("tmp/outfiles/")){
		mkdir("tmp/outfiles/",0777,true);
	}
	$outfolder = "tmp/outfiles/";
	$outfile = $outfolder . strip_tags($_GET['outfile']);
	$storepath = "tmp/" . $_GET['chunkid'] . "/";
	//This is a bit tricky. We use timestamp as the folder to store each uuids. So, to get the file uuid, we need to get the first file (aka {uuid}_0 ) for the file uuid.
	$fc = glob($storepath . "*_0");
	$fc = basename($fc[0]);
	$fuuid = explode("_",$fc)[0];
	//Call arozdfs for command
	//This command build the file from chunks and place the outfile in tmp/outfiles/ with the given filename from variable "outfile"
	$output = shell_exec('arozdfs.exe open -storepath "' . $storepath . '" -uuid "' . $fuuid . '" -outfile "' . $outfolder . $_GET['outfile'] . '" -c'); //Remove -c if you do not want the file chunks to be removed after building the file.
	
	$files = glob($storepath . "*_*");
	if (count($files) == 0){
		rmdir($storepath);
	}
	echo "DONE";
}else{
	die("ERROR. Undefined chunkid or outfile.");
}
?>