Created
April 13, 2020 23:05
-
-
Save ingliths/57cbf499664a6da89df0ebf016031d9a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private function uploadArquivoAgendamento($sigla, $name){ | |
| try { | |
| $this->loadModel("TempUploadArquivo"); | |
| $qtdMaxUpload = $this->getQtdMaxArquivoUpload(); | |
| $tempoMaxUpload = $this->getTempoMaxArquivoUpload(); | |
| $formatosValidos = Configure::read('FORMATOS_UPLOAD'); | |
| $tmp = explode('.', $this->request->data['Agendamento'][$name]['name']); | |
| $extensao = end($tmp); | |
| $idUsuario = $this->Auth->user()['id']; | |
| // false - subtrai / true - soma | |
| $dataAuxiliar = Util::opeHorasMinutos(date('Y-m-d H:i:s'), $tempoMaxUpload, false); | |
| $tempUploadArquivo = $this->TempUploadArquivo->find('list', array( | |
| 'conditions' => array( | |
| 'TempUploadArquivo.created <= ' => $dataAuxiliar, | |
| 'TempUploadArquivo.usuario_id' => $idUsuario | |
| ) | |
| )); | |
| // deletando os registros antigos | |
| $this->limparTabelaUploads($tempUploadArquivo); | |
| //consultando a quantidade de registros a partir da data-hora | |
| $totalRegistrosUpload = $this->TempUploadArquivo->find('count', array( | |
| 'conditions' => array( | |
| 'TempUploadArquivo.created >= ' => $dataAuxiliar, | |
| 'TempUploadArquivo.usuario_id' => $idUsuario | |
| ) | |
| )); | |
| // pr($tempUploadArquivo);die; | |
| if (!empty ($data)) { | |
| if (count (data) > $this->max_files){ | |
| //throw new NotFoundException("error Processing Request , Max number files accepted is {$this- max_files}", 1); | |
| } | |
| } | |
| if(intval($totalRegistrosUpload) < intval($qtdMaxUpload)){ | |
| // continua o upload | |
| $dataTempUpload = array(); | |
| $tamanhoMaxArquivo = Util::convertToBytes($this->getTamanhoMaxArquivoUpload()); | |
| // verifica se o tamanho do arquivo é menor do que o tamanho máximo (Paramêtros Gerais) | |
| if($this->request->data['Agendamento'][$name]['size'] < $tamanhoMaxArquivo){ | |
| //verifica o formato do arquivo é um dos formatos válidos para upload | |
| if (in_array($extensao, $formatosValidos)) { | |
| // executa o upload | |
| $filepathPart = WWW_ROOT. '/' . $name; | |
| if(!is_dir($filepathPart)) { | |
| mkdir($filepathPart); | |
| } | |
| $file = $this->request->data['Agendamento'][$name]; | |
| $temp = explode(".", $this->request->data['Agendamento'][$name]['name']); | |
| $filename = uniqid(). $sigla.$sigla.'.' . end($temp); | |
| $filePath= $filepathPart.'/'.$filename; | |
| $filePath = str_replace("\\","/",$filePath); | |
| $this->request->data['Agendamento'][$name] = $filename; | |
| if(isset($this->request->data['Agendamento'][$name]) && !empty($this->request->data['Agendamento'][$name])){ | |
| $this->request->data['Agendamento'][$name] = $filename; | |
| } | |
| move_uploaded_file($file['tmp_name'],$filePath); | |
| $this->request->data['Agendamento'][$name.'_path'] = '/' . $name.'/'.$filename; | |
| $dataTempUpload['usuario_id'] = $idUsuario; | |
| $dataTempUpload['arquivo'] = '/' . $name.'/'.$filename; | |
| $this->TempUploadArquivo->save($dataTempUpload); | |
| $this->TempUploadArquivo->id = null; | |
| $dataTempUpload = null; | |
| $this->TempUploadArquivo = null; | |
| }else{ | |
| // Formato inválido | |
| $texto = ""; | |
| foreach ($formatosValidos as $key => $formato) { | |
| $tmp = explode('.', $formato); | |
| $formato = end($tmp); | |
| $texto .= $formato; | |
| if(intval($key+1) < count($formatosValidos)){ | |
| $texto .= ", "; | |
| } | |
| } | |
| throw new Exception("Somente o(s) formato(s): ". $texto . " é(são) aceito(s) para upload."); | |
| } | |
| }else{ | |
| throw new Exception("O tamanho do arquivo é maior que o permitido (". $this->getTamanhoMaxArquivoUpload() ." mb)"); | |
| } | |
| }else{ | |
| //mesagem para o usuário de que ele passou do limite e tem que aguardar um tempo | |
| $dataHabilitarUpload = Util::opeHorasMinutos(date('Y-m-d H:i:s'), $tempoMaxUpload, true); | |
| throw new Exception("Você já efetuou " . $totalRegistrosUpload . " upload(s). Aguarde até ". date("d/m/Y H:i:s", strtotime($dataHabilitarUpload)) . " para efetuar nova(s) submissão(ões) de arquivo(s)"); | |
| } | |
| }catch(\Exception $e){ | |
| if(isset($this->request->params['pass']['0']) && !empty(($this->request->params['pass']['0']))){ | |
| $this->Session->setFlash($e->getMessage(), 'flash_alert'); | |
| return $this->redirect(array('controller' => $this->request->controller, 'action' => $this->request->action, $this->request->params['pass']['0'])); | |
| }else{ | |
| $this->Session->setFlash($e->getMessage(), 'flash_alert'); | |
| return $this->redirect(array('controller' => $this->request->controller, 'action' => $this->request->action)); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment