Created
August 24, 2016 18:39
-
-
Save willianmano/f7c52e05451d8e118fd68d9b466d31c3 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
public function postCreate(Request $request) | |
{ | |
$this->validate($request, $this->contratoRepository->createRules()); | |
try { | |
DB::beginTransaction(); | |
$contratoData = $request->all(); | |
$contratoData['str_resenha'] = $this->gerarTextoResenha($contratoData); | |
$contrato = $this->contratoRepository->create($contratoData); | |
if(!empty($request->files)) { | |
if(!$this->validaAnexos($request->file('anexos'))) { | |
throw new \Exception('Você só pode anexar arquivos no formato PDF'); | |
} | |
foreach ($request->file('anexos') as $uploadedFile) { | |
$anexo = $this->cadastrarAnexo($uploadedFile, 'contrato', 'Anexo de contrato'); | |
if(!$anexo) { | |
throw new \Exception('Você está tentando enviar um arquivo inválido'); | |
} | |
$anexosData[] = [ | |
'int_anexo_id' => $anexo->int_anexo_id, | |
'int_contrato_id' => $contrato->int_contrato_id | |
]; | |
} | |
$contrato->anexos()->attach($anexosData); | |
} | |
DB::commit(); | |
Flash::success('Contrato cadastrado com sucesso!'); | |
return redirect()->to('/contratta/contratos/view/' . $contrato->int_contrato_id); | |
} catch (DuplicatedFileUploadedException $e) { | |
DB::rollback(); | |
if(!config('app.debug')) { | |
Flash::error('Você está tentando enviar um arquivo que já foi anexado em outro processo.'); | |
return redirect()->back()->withInput($request->all()); | |
} else { | |
throw $e; | |
} | |
} catch (\Exception $e) { | |
DB::rollback(); | |
if(!config('app.debug')) { | |
Flash::error($e->getMessage()); | |
return redirect()->back()->withInput($request->all()); | |
} else { | |
throw $e; | |
} | |
} | |
} | |
private function validaAnexos(array $data) | |
{ | |
// Valida todos os uploads | |
foreach ($data as $upload) { | |
// Ignore array member if it's not an UploadedFile object, just to be extra safe | |
if (!is_a($upload, 'Symfony\Component\HttpFoundation\File\UploadedFile')) { | |
continue; | |
} | |
$validator = Validator::make( | |
['file' => $upload], | |
['file' => 'required|mimes:pdf'] | |
); | |
if ($validator->fails()) { | |
return false; | |
} | |
} | |
return true; | |
} | |
private function cadastrarAnexo(UploadedFile $anexo, $tipoAnexo, $descricao) | |
{ | |
if (!$anexo->isValid()) { | |
return false; | |
} | |
$fileHash = sha1_file($anexo); | |
$p1 = substr($fileHash, 0, 2); | |
$p2 = substr($fileHash, 2, 2); | |
$filePath = "/uploads/contratta/{$p1}/{$p2}/"; | |
$fullFilePath = storage_path().$filePath.$fileHash; | |
if (file_exists($fullFilePath)) { | |
throw new DuplicatedFileUploadedException(); | |
} | |
$anexoData = [ | |
'ind_tipo_anexo' => $tipoAnexo, | |
'str_nome_arquivo' => $anexo->getClientOriginalName(), | |
'str_desc_anexo' => $descricao, | |
'str_mime_type' => $anexo->getClientMimeType(), | |
'str_link_anexo' => $filePath.$fileHash, | |
]; | |
$anexoObject = $this->anexoRepository->create($anexoData); | |
// Move o arquivo para a pasta de uploads do sistema SUPREMA | |
$anexo->move(storage_path().$filePath, $fileHash); | |
return $anexoObject; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment