/** * Upload Pdf to s3 and create contracts * * @param $key */ public function uploadPdfToS3AndCreateContracts($key) { $contracts = $this->getJsonData($key); foreach ($contracts as $contract) { $this->updateContractJsonByID($key, $contract->id, ['create_status' => static::CREATE_PROCESSING], 2); try { $this->storage->disk('s3')->put($contract->file, $this->filesystem->get($this->getFilePath($key, $contract->file))); } catch (Exception $e) { $this->logger->error(sprintf('File could not be uploaded : %s', $e->getMessage())); continue; } $data = ['file' => $contract->file, 'filehash' => $contract->filehash, 'user_id' => $contract->user_id, 'metadata' => $contract->metadata]; try { $con = $this->contract->save($data); $this->logger->activity('contract.log.save', ['contract' => $con->id], $con->id, $con->user_id); $this->updateContractJsonByID($key, $contract->id, ['create_status' => static::CREATE_COMPLETED], 2); if ($con) { $this->queue->push('App\\Nrgi\\Services\\Queue\\ProcessDocumentQueue', ['contract_id' => $con->id]); } $this->logger->info('Contract successfully created.', ['Contract Title' => $con->title]); } catch (Exception $e) { $this->logger->error($e->getMessage()); if ($this->storage->disk('s3')->exists($contract->file)) { $this->storage->disk('s3')->delete($contract->file); } $this->updateContractJsonByID($key, $contract->id, ['create_remarks' => trans('contract.save_fail'), 'create_status' => static::CREATE_FAILED], 2); } $this->deleteFile($key, $contract->file); } }
/** * Upload Contract and save in database * * @param array $formData * @return Contract|bool */ public function saveContract(array $formData) { if ($file = $this->uploadContract($formData['file'])) { $metadata = $this->processMetadata($formData); $metadata['file_size'] = $file['size']; $data = ['file' => $file['name'], 'filehash' => $file['hash'], 'user_id' => $this->auth->user()->id, 'metadata' => $metadata]; $supportingDocuments = isset($formData['supporting_document']) ? $formData['supporting_document'] : []; try { $contract = $this->contract->save($data); if (!empty($supportingDocuments)) { $contract->syncSupportingContracts($supportingDocuments); } $this->logger->activity('contract.log.save', ['contract' => $contract->title], $contract->id); $this->logger->info('Contract successfully created.', ['Contract Title' => $contract->title]); } catch (Exception $e) { $this->logger->error($e->getMessage()); $this->deleteFileFromS3($file['name']); return false; } if ($contract) { $this->queue->push('App\\Nrgi\\Services\\Queue\\ProcessDocumentQueue', ['contract_id' => $contract->id]); } return $contract; } return false; }