IHMInputOutputDownloadObjectClass.php 2.73 KB
<?php

/**
 * @class IHMInputOutputDownloadObjectClass
 * @brief Class that's implement an InputOutputInterface used to treat a get download object request
 * @details
 */
class IHMInputOutputDownloadObjectClass implements InputOutputInterface
{
    /*
	 * @brief Get working dir name
	 */
	protected function getWorkingDirName()
	{
		return $this->requestDirPrefix."DownloadData_";
	}

	/*
	 * @brief Get full working dir path
	*/
	protected function getWorkingPath()
	{
		return IHMConfigClass::getRequestPath().$this->getWorkingDirName().'/';
	}

    public function getInputData($input, $function, $requestId = ""){
        if (is_dir(IHMConfigClass::getDownloadTmpPath())) {
			foreach (glob(IHMConfigClass::getDownloadTmpPath().'*.zip') as $filename)  unlink($filename);
			foreach (glob(IHMConfigClass::getDownloadTmpPath().'*.tar.gz') as $filename)  unlink($filename);
		}
        else {
            mkdir(IHMConfigClass::getDownloadTmpPath());
        }

        $this->downloadObjectData = new DownloadObjectRequestDataClass();

        if($input->nodeType == "download" && $input->downloadSrc == "2")
        {
            // Set type
            $this->downloadObjectData->setType(DownloadObjectTypeEnumClass::ASTROIMG);
            
            
             // Set list of links to download   
            if(sizeof($input->list) > 0){
                foreach ($input->list as $file) {
                    $this->downloadObjectData->addFile($file->name, $file->url);
                }
            }
            
        }
        else {
            throw new Exception('Unknown download data type.');
        }

        $outputFileName = "".$this->downloadObjectData->getType()."_".time();

        // Set working dir
        $this->downloadObjectData->setWorkingPath($this->getWorkingPath());

        // Set compression format
        switch ($input->compression) {
            case DownloadObjectFormatEnumClass::ZIP:
                $this->downloadObjectData->setCompressionFormat(DownloadObjectFormatEnumClass::ZIP);
                $extension = ".zip";
                break;
            default:
                throw new Exception('Unknown compression format type.');
        } 
        

        // Set output
        $outputPath = IHMConfigClass::getDownloadTmpPath() . $outputFileName . $extension;
        $this->downloadObjectData->setOutputPath($outputPath);

        return $this->downloadObjectData;
        
    }
    public function getOutput($data)
	{
        if (!$data->getSuccess())
            return array(
                'success' => false,
                'message' => $data->getLastErrorMessage());

        return array(
            'success' => true,
            'download' => str_replace(IHM_SRC_DIR, '', $data->getOutputPath()),
        );
    }
}