App\Http\Controllers\ClientPortalController::getDocumentVFSJS PHP Method

getDocumentVFSJS() public method

public getDocumentVFSJS ( $publicId, $name )
    public function getDocumentVFSJS($publicId, $name)
    {
        if (!($contact = $this->getContact())) {
            return $this->returnError();
        }
        $document = Document::scope($publicId, $contact->account_id)->first();
        if (!$document->isPDFEmbeddable()) {
            return Response::view('error', ['error' => 'Image does not exist!'], 404);
        }
        $authorized = false;
        if ($document->expense && $document->expense->client_id == $contact->client_id) {
            $authorized = true;
        } else {
            if ($document->invoice && $document->invoice->client_id == $contact->client_id) {
                $authorized = true;
            }
        }
        if (!$authorized) {
            return Response::view('error', ['error' => 'Not authorized'], 403);
        }
        if (substr($name, -3) == '.js') {
            $name = substr($name, 0, -3);
        }
        $content = $document->preview ? $document->getRawPreview() : $document->getRaw();
        $content = 'ninjaAddVFSDoc(' . json_encode(intval($publicId) . '/' . strval($name)) . ',"' . base64_encode($content) . '")';
        $response = Response::make($content, 200);
        $response->header('content-type', 'text/javascript');
        $response->header('cache-control', 'max-age=31536000');
        return $response;
    }