1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00
invoiceninja/app/Http/Controllers/DocumentController.php

132 lines
3.7 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
namespace App\Http\Controllers;
2016-03-23 03:23:45 +01:00
2016-05-01 21:30:39 +02:00
use App\Http\Requests\CreateDocumentRequest;
2017-01-30 20:40:43 +01:00
use App\Http\Requests\DocumentRequest;
2016-05-26 18:04:43 +02:00
use App\Http\Requests\UpdateDocumentRequest;
2017-01-30 20:40:43 +01:00
use App\Models\Document;
use App\Ninja\Repositories\DocumentRepository;
use Redirect;
use Response;
use View;
2016-05-01 21:30:39 +02:00
2016-03-23 03:23:45 +01:00
class DocumentController extends BaseController
{
protected $documentRepo;
2016-04-28 14:16:33 +02:00
protected $entityType = ENTITY_DOCUMENT;
2016-03-23 03:23:45 +01:00
public function __construct(DocumentRepository $documentRepo)
{
// parent::__construct();
$this->documentRepo = $documentRepo;
}
2016-05-26 18:04:43 +02:00
2016-05-01 21:30:39 +02:00
public function get(DocumentRequest $request)
2016-03-23 03:23:45 +01:00
{
2016-05-01 21:30:39 +02:00
return static::getDownloadResponse($request->entity());
2016-03-24 02:25:33 +01:00
}
2016-05-26 18:04:43 +02:00
2017-01-30 17:05:31 +01:00
public static function getDownloadResponse($document)
{
2016-03-23 20:41:05 +01:00
$direct_url = $document->getDirectUrl();
2017-01-30 17:05:31 +01:00
if ($direct_url) {
2016-03-23 20:41:05 +01:00
return redirect($direct_url);
2016-03-23 03:23:45 +01:00
}
2016-05-26 18:04:43 +02:00
2016-03-24 01:46:09 +01:00
$stream = $document->getStream();
2016-05-26 18:04:43 +02:00
2017-01-30 17:05:31 +01:00
if ($stream) {
2016-03-24 01:46:09 +01:00
$headers = [
2017-01-30 20:40:43 +01:00
'Content-Type' => Document::$types[$document->type]['mime'],
'Content-Length' => $document->size,
2016-03-24 01:46:09 +01:00
];
2017-01-30 17:05:31 +01:00
$response = Response::stream(function () use ($stream) {
2016-03-24 01:46:09 +01:00
fpassthru($stream);
}, 200, $headers);
2017-01-30 17:05:31 +01:00
} else {
2016-03-24 01:46:09 +01:00
$response = Response::make($document->getRaw(), 200);
$response->header('content-type', Document::$types[$document->type]['mime']);
2016-03-24 01:46:09 +01:00
}
2016-05-26 18:04:43 +02:00
2016-03-23 03:23:45 +01:00
return $response;
}
2016-05-26 18:04:43 +02:00
2016-05-01 21:30:39 +02:00
public function getPreview(DocumentRequest $request)
2016-03-23 20:41:05 +01:00
{
2016-05-01 21:30:39 +02:00
$document = $request->entity();
2016-05-26 18:04:43 +02:00
2017-01-30 17:05:31 +01:00
if (empty($document->preview)) {
2017-01-30 20:40:43 +01:00
return Response::view('error', ['error' => 'Preview does not exist!'], 404);
2016-03-23 20:41:05 +01:00
}
2016-05-26 18:04:43 +02:00
2016-03-23 20:41:05 +01:00
$direct_url = $document->getDirectPreviewUrl();
2017-01-30 17:05:31 +01:00
if ($direct_url) {
2016-03-23 20:41:05 +01:00
return redirect($direct_url);
}
2016-05-26 18:04:43 +02:00
$previewType = pathinfo($document->preview, PATHINFO_EXTENSION);
2016-03-23 20:41:05 +01:00
$response = Response::make($document->getRawPreview(), 200);
$response->header('content-type', Document::$types[$previewType]['mime']);
2016-05-26 18:04:43 +02:00
2016-03-23 20:41:05 +01:00
return $response;
}
2016-05-26 18:04:43 +02:00
2016-05-01 21:30:39 +02:00
public function getVFSJS(DocumentRequest $request, $publicId, $name)
{
$document = $request->entity();
2016-05-26 18:04:43 +02:00
2017-01-30 20:40:43 +01:00
if (substr($name, -3) == '.js') {
2016-03-23 23:40:42 +01:00
$name = substr($name, 0, -3);
}
2016-05-26 18:04:43 +02:00
2017-01-30 20:40:43 +01:00
if (! $document->isPDFEmbeddable()) {
return Response::view('error', ['error' => 'Image does not exist!'], 404);
2016-03-23 23:40:42 +01:00
}
2016-05-26 18:04:43 +02:00
2017-01-30 20:40:43 +01:00
$content = $document->preview ? $document->getRawPreview() : $document->getRaw();
2016-03-23 23:40:42 +01:00
$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');
2016-05-26 18:04:43 +02:00
2016-03-23 23:40:42 +01:00
return $response;
}
2016-05-26 18:04:43 +02:00
2016-05-01 21:30:39 +02:00
public function postUpload(CreateDocumentRequest $request)
2016-03-23 03:23:45 +01:00
{
$result = $this->documentRepo->upload($request->all(), $doc_array);
2016-05-26 18:04:43 +02:00
2017-01-30 17:05:31 +01:00
if (is_string($result)) {
return Response::json([
'error' => $result,
2017-01-30 20:40:43 +01:00
'code' => 400,
], 400);
} else {
2018-02-12 16:35:57 +01:00
if ($request->grapesjs) {
$response = [
'data' => [
2018-02-13 11:06:50 +01:00
$result->getProposalUrl()
2018-02-12 16:35:57 +01:00
]
];
} else {
$response = [
'error' => false,
'document' => $doc_array,
'code' => 200,
];
}
return Response::json($response, 200);
}
2016-03-23 03:23:45 +01:00
}
2016-05-26 18:04:43 +02:00
public function delete(UpdateDocumentRequest $request)
{
$request->entity()->delete();
return RESULT_SUCCESS;
}
2016-03-23 03:23:45 +01:00
}