Created
September 5, 2017 20:17
-
-
Save x0st/820c991a6db6b8e3f425ed21a5e255bd to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
* @param EgnyteState $stateModel | |
* | |
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View | |
*/ | |
public function index(EgnyteState $stateModel) | |
{ | |
if (Auth::getUser()->isEditingDocument()) { | |
sleep(2); | |
if (Auth::getUser()->isEditingDocument()) { | |
throw new ConflictHttpException(trans('editing.simultaneous_editing')); | |
} | |
} | |
Auth::getUser()->markAsEditingDocument(); | |
$egnyteDocumentSize = Auth::getUser()->egnyte()->getFileInfo($stateModel->egnyteDocumentToEdit->getDocumentPath())->getSize(); | |
// STEP 2: CHECK IF THE DOCUMENT IS LESS 20 MB | |
if ($egnyteDocumentSize > config('editing.max_file_size.b')) { | |
throw new BadRequestHttpException(trans('editing.too_big_document'), null, ExceptionCodes::TOO_BIG_DOCUMENT); | |
} | |
// STEP 1: DOWNLOAD THE DOCUMENT FROM EGNYTE | |
$documentFilePath = Auth::getUser()->egnyte()->downloadFileAsTempFile( | |
$stateModel->egnyteDocumentToEdit->getDocumentPath() | |
); | |
// STEP 3: CHECK IF THE DOCUMENT HAS LESS THAN 150 PAGES | |
if (get_pdf_page_count($documentFilePath) > config('editing.max_page_count.pdf')) { | |
throw new BadRequestHttpException(trans('editing.too_many_pages'), null, ExceptionCodes::TOO_MANY_PAGES); | |
} | |
// STEP 4: UPLOAD THE DOCUMENT TO PDFFILLER | |
$documentFileUrl = InternetAccessibleFileMaker::one($documentFilePath, true); | |
$documentRepresentation = Auth::getUser()->pdffiller()->documentUploader()->uploadByUrl($documentFileUrl); | |
// STEP 5: GET THE URL FOR EDITING THE DOCUMENT | |
$urlForOpeningPDFfillerEditor = Auth::getUser()->pdffiller()->urlsForEditingProvider()->getUrlForEditingDocument( | |
$documentRepresentation | |
); | |
// STEP 6: PDFFILLER DOCUMENT ID TO THE STATE | |
$stateModel->egnyteDocumentToEdit->setPdffillerDocumentId($documentRepresentation->getId()); | |
$stateModel->egnyteDocumentToEdit->save(); | |
JavaScriptFacade::put([ | |
'state' => $stateModel->getState(), | |
'exceptionCodes' => ExceptionCodes::asArray(), | |
]); | |
$stateModel->setStatus($stateModel::STATUS_PROCESSING); | |
$stateModel->save(); | |
if (config('app.debug', true) === false) { | |
unlink(public_path('files/' . basename($documentFilePath))); | |
} | |
return view('editing.index', [ | |
'url' => $urlForOpeningPDFfillerEditor, | |
'state' => $stateModel->getState() | |
]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment