2016-03-23 03:23:45 +01:00
|
|
|
<?php namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use Datatable;
|
|
|
|
use Input;
|
|
|
|
use Redirect;
|
|
|
|
use Session;
|
|
|
|
use URL;
|
|
|
|
use Utils;
|
|
|
|
use View;
|
|
|
|
use Validator;
|
|
|
|
use Response;
|
|
|
|
use App\Models\Document;
|
|
|
|
use App\Ninja\Repositories\DocumentRepository;
|
|
|
|
|
|
|
|
class DocumentController extends BaseController
|
|
|
|
{
|
|
|
|
protected $documentRepo;
|
|
|
|
protected $model = 'App\Models\Document';
|
|
|
|
|
|
|
|
public function __construct(DocumentRepository $documentRepo)
|
|
|
|
{
|
|
|
|
// parent::__construct();
|
|
|
|
|
|
|
|
$this->documentRepo = $documentRepo;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get($publicId)
|
|
|
|
{
|
|
|
|
$document = Document::scope($publicId)
|
|
|
|
->firstOrFail();
|
|
|
|
|
|
|
|
if(!$this->checkViewPermission($document, $response)){
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2016-03-24 02:25:33 +01:00
|
|
|
return static::getDownloadResponse($document);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getDownloadResponse($document){
|
2016-03-23 20:41:05 +01:00
|
|
|
$direct_url = $document->getDirectUrl();
|
|
|
|
if($direct_url){
|
|
|
|
return redirect($direct_url);
|
2016-03-23 03:23:45 +01:00
|
|
|
}
|
|
|
|
|
2016-03-24 01:46:09 +01:00
|
|
|
$stream = $document->getStream();
|
2016-03-23 03:23:45 +01:00
|
|
|
|
2016-03-24 01:46:09 +01:00
|
|
|
if($stream){
|
|
|
|
$headers = [
|
2016-03-24 16:33:28 +01:00
|
|
|
'Content-Type' => Document::$types[$document->type]['mime'],
|
2016-03-24 01:46:09 +01:00
|
|
|
'Content-Length' => $document->size,
|
|
|
|
];
|
|
|
|
|
|
|
|
$response = Response::stream(function() use ($stream) {
|
|
|
|
fpassthru($stream);
|
|
|
|
}, 200, $headers);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
$response = Response::make($document->getRaw(), 200);
|
2016-03-24 16:33:28 +01:00
|
|
|
$response->header('content-type', Document::$types[$document->type]['mime']);
|
2016-03-24 01:46:09 +01:00
|
|
|
}
|
|
|
|
|
2016-03-23 03:23:45 +01:00
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2016-03-23 20:41:05 +01:00
|
|
|
public function getPreview($publicId)
|
|
|
|
{
|
|
|
|
$document = Document::scope($publicId)
|
|
|
|
->firstOrFail();
|
|
|
|
|
|
|
|
if(!$this->checkViewPermission($document, $response)){
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(empty($document->preview)){
|
|
|
|
return Response::view('error', array('error'=>'Preview does not exist!'), 404);
|
|
|
|
}
|
|
|
|
|
|
|
|
$direct_url = $document->getDirectPreviewUrl();
|
|
|
|
if($direct_url){
|
|
|
|
return redirect($direct_url);
|
|
|
|
}
|
|
|
|
|
2016-03-24 16:33:28 +01:00
|
|
|
$previewType = pathinfo($document->preview, PATHINFO_EXTENSION);
|
2016-03-23 20:41:05 +01:00
|
|
|
$response = Response::make($document->getRawPreview(), 200);
|
2016-03-24 16:33:28 +01:00
|
|
|
$response->header('content-type', Document::$types[$previewType]['mime']);
|
2016-03-23 20:41:05 +01:00
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2016-03-23 23:40:42 +01:00
|
|
|
public function getVFSJS($publicId, $name){
|
|
|
|
$document = Document::scope($publicId)
|
|
|
|
->firstOrFail();
|
|
|
|
|
|
|
|
if(substr($name, -3)=='.js'){
|
|
|
|
$name = substr($name, 0, -3);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!$this->checkViewPermission($document, $response)){
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2016-03-24 16:33:28 +01:00
|
|
|
if(!$document->isPDFEmbeddable()){
|
2016-03-23 23:40:42 +01:00
|
|
|
return Response::view('error', array('error'=>'Image does not exist!'), 404);
|
|
|
|
}
|
|
|
|
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2016-03-23 03:23:45 +01:00
|
|
|
public function postUpload()
|
|
|
|
{
|
2016-03-23 20:41:05 +01:00
|
|
|
if (!Utils::isPro()) {
|
2016-03-23 03:59:20 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-23 03:23:45 +01:00
|
|
|
if(!$this->checkCreatePermission($response)){
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2016-03-24 20:13:54 +01:00
|
|
|
$result = $this->documentRepo->upload(Input::all()['file'], $doc_array);
|
|
|
|
|
|
|
|
if(is_string($result)){
|
|
|
|
return Response::json([
|
|
|
|
'error' => $result,
|
|
|
|
'code' => 400
|
|
|
|
], 400);
|
|
|
|
} else {
|
|
|
|
return Response::json([
|
|
|
|
'error' => false,
|
|
|
|
'document' => $doc_array,
|
|
|
|
'code' => 200
|
|
|
|
], 200);
|
|
|
|
}
|
2016-03-23 03:23:45 +01:00
|
|
|
}
|
|
|
|
}
|