Classi per la configurazione upload
Cristiano Magro
2018-01-16 40d16cb1f73f75cb82cb80cb1cf3318861695cf7
src/Vola/UploadFile/FileUp.php
@@ -21,12 +21,206 @@
    private $request;
    private $beanFile;
    public static function create()
    /* @var $logger WrapLog */
    private $logger;
    private $pathMap;
    public static function create($pathMap = null)
    {
        $instance = new self();
        $instance->request = new stdClass();
        $instance->beanFile = new stdClass();
        $instance->pathMap = $pathMap;
        return $instance;
    }
    public function setLogger($logger)
    {
        $this->logger = WrapLog::create($logger);
        return $this;
    }
    /**
     * Imposto i path di destinazione del file.
     *
     * Ricavo dalla mappa il path su cui memorizzare il file corrente, sulla base del parametro
     * passato nella Request. Se non trovo corrispondenza viene lanciata eccezione.
     * @param array $cfgPathMap elenco percorsi validi per la memorizzazione.
     * @return \Upload_FileUp fluent style
     * @throws Upload_Exc_Error
     */
    public function decodeUploadPath(array $cfgPathMap)
    {
        $this->pathMap = $cfgPathMap;
        $key = $this->getRequestValue('dirDestinazione', null);
        $path = $this->getMapValue($key);
        $pathDocumentRoot = filter_input(INPUT_SERVER, 'DOCUMENT_ROOT');
        //genero il percorso reale dove andrà salvato il file
        $this->request->pathForUpload =  $pathDocumentRoot. '/../' . $path;
        return $this;
    }
    /**
     * Ritorno il valore della Request preso dalla chiamata.
     *
     * Se non è definito ritorno il valore di default|null
     * @param string $nomeParam
     * @param string $default
     * @return mixed null|valore chiamata
     */
    public function getRequestValue($nomeParam, $default = null)
    {
        if (isset($this->request->{$nomeParam})) {
            return $this->request->{$nomeParam};
        }
        return $default;
    }
    /**
     * Recupero i parametri passati in GET nella chiamata.
     *
     * Costruisco una request con i parametri standard della chiamata: ope|dest|rand_value
     * @return \Upload_FileUp
     * @deprecated 0.3.2 usare readRequest();
     */
    public function getParametri()
    {
        $this->readRequest();
        return $this;
    }
    /**
     * Recupero i parametri passati in GET nella chiamata.
     *
     * Costruisco una request con i parametri standard della chiamata: ope|dest|rand_value
     * @return \Upload_FileUp
     * @since 0.3.2
     */
    public function readRequest()
    {
        $this->request->operazione = $_REQUEST['ope'];
        $this->request->dirDestinazione = $_REQUEST['dest'];
        $this->request->rand_value = $_REQUEST['rand_value'];
        return $this;
    }
    public function getRequestPathForUpload()
    {
        return $this->request->pathForUpload;
    }
    /**
     * Ricostruisco i dati del BEAN del file caricato sul sistema
     */
    public function getFileBean()
    {
        if (!empty($_FILES)) {
            $this->setTempNomeFile($_FILES['file']['tmp_name']);
            $this->setOrigNomeFile($_FILES['file']['name']);
            $this->setSizeFile($_FILES['file']['size']);
        } else {
            $this->setOrigNomeFile('');
        }
        return $this;
    }
    public function setTempNomeFile($x)
    {
        $this->beanFile->tempNomeFile = $x;
    }
    public function setOrigNomeFile($x)
    {
        $this->beanFile->origNomeFile = $x;
    }
    public function setSizeFile($x)
    {
        $this->beanFile->sizeFile = $x;
    }
    public function getTempNomeFile()
    {
        return $this->beanFile->tempNomeFile;
    }
    /**
     * Ritorno l'estensione del file caricato dall'utente
     * @return string Estenzione del file
     */
    public function getOrigNomeFileExt()
    {
        $path_info = pathinfo($this->getOrigNomeFile());
        return $path_info['extension'];
    }
    public function getOrigNomeFile()
    {
        return $this->beanFile->origNomeFile;
    }
    public function getSizeFile()
    {
        return $this->beanFile->sizeFile;
    }
    /**
     * Il tipo di operazione richiesta nella chiamata.
     * Per selezionare il case switch da attivare
     * @return string operazione da attivare
     */
    public function getOperazione()
    {
        return $this->request->operazione;
    }
    /**
     * ricostruisco il nome del file da registrare su disco.
     *
     * Vengono ripuliti i caratteri strani e le lettere accentate.
     * @return string
     */
    public function getNomeFileDisco()
    {
        $rand = $this->getRequestRandomValue();
        $nome = $this->getOrigNomeFile();
        return self::getNomeFile($rand, $nome);
    }
    public function getRequestRandomValue()
    {
        return $this->request->rand_value;
    }
    /**
     * ricostruisco il nome del file da registrare su disco.
     *
     * Vengono ripuliti i caratteri strani e le lettere accentate.
     * @param type $rand
     * @param string $nome
     * @return string
     */
    public static function getNomeFile($rand, $nome)
    {
        return $rand . "_" . FileUpload::clearNomeFile($nome);
    }
    /**
     * @deprecated 0.3.2 utilizzare toString
     */
    public function stampaTutto()
    {
        if ($this->logger instanceof WrapLog) {
            $this->logger->debug($this->request);
            $this->logger->debug($this->beanFile);
        } else {
            error_log('errorlog non attivo');
        }
    }
    public function __toString()
@@ -64,5 +258,19 @@
        }
    }
    /**
     * Decodifico mediante la mappa il percorso da utilizzare.
     * @param $chiave della Request per il percorso
     * @return mixed  path associato
     * @throws Upload_Exc_Error
     * @since 0.3.2
     */
    protected function getMapValue($chiave)
    {
        if (!isset($cfgPathMap[$chiave])) {
            throw new Upload_Exc_Error('Decodifica chiave percorso non definita');
        }
        return $cfgPathMap[$chiave];
    }
}