2016-03-23 03:23:45 +01:00
|
|
|
<?php namespace app\Ninja\Repositories;
|
|
|
|
|
|
|
|
use DB;
|
|
|
|
use Utils;
|
|
|
|
use Response;
|
|
|
|
use App\Models\Document;
|
|
|
|
use App\Ninja\Repositories\BaseRepository;
|
2016-03-23 20:41:05 +01:00
|
|
|
use Intervention\Image\ImageManager;
|
2016-03-23 03:23:45 +01:00
|
|
|
use Session;
|
|
|
|
|
|
|
|
class DocumentRepository extends BaseRepository
|
|
|
|
{
|
|
|
|
// Expenses
|
|
|
|
public function getClassName()
|
|
|
|
{
|
|
|
|
return 'App\Models\Document';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function all()
|
|
|
|
{
|
|
|
|
return Document::scope()
|
|
|
|
->with('user')
|
|
|
|
->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function find()
|
|
|
|
{
|
|
|
|
$accountid = \Auth::user()->account_id;
|
|
|
|
$query = DB::table('clients')
|
|
|
|
->join('accounts', 'accounts.id', '=', 'clients.account_id')
|
|
|
|
->leftjoin('clients', 'clients.id', '=', 'clients.client_id')
|
|
|
|
/*->leftJoin('expenses', 'expenses.id', '=', 'clients.expense_id')
|
|
|
|
->leftJoin('invoices', 'invoices.id', '=', 'clients.invoice_id')*/
|
|
|
|
->where('documents.account_id', '=', $accountid)
|
|
|
|
/*->where('vendors.deleted_at', '=', null)
|
|
|
|
->where('clients.deleted_at', '=', null)*/
|
|
|
|
->select(
|
|
|
|
'documents.account_id',
|
|
|
|
'documents.path',
|
|
|
|
'documents.deleted_at',
|
|
|
|
'documents.size',
|
|
|
|
'documents.width',
|
|
|
|
'documents.height',
|
|
|
|
'documents.id',
|
|
|
|
'documents.is_deleted',
|
|
|
|
'documents.public_id',
|
|
|
|
'documents.invoice_id',
|
|
|
|
'documents.expense_id',
|
|
|
|
'documents.user_id',
|
|
|
|
'invoices.public_id as invoice_public_id',
|
|
|
|
'invoices.user_id as invoice_user_id',
|
|
|
|
'expenses.public_id as expense_public_id',
|
|
|
|
'expenses.user_id as expense_user_id'
|
|
|
|
);
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function upload($input)
|
|
|
|
{
|
|
|
|
$uploaded = $input['file'];
|
|
|
|
|
|
|
|
$extension = strtolower($uploaded->extension());
|
|
|
|
if(empty(Document::$extensions[$extension])){
|
|
|
|
return Response::json([
|
|
|
|
'error' => 'Unsupported extension',
|
|
|
|
'code' => 400
|
|
|
|
], 400);
|
|
|
|
}
|
|
|
|
|
|
|
|
$documentType = Document::$extensions[$extension];
|
|
|
|
$filePath = $uploaded->path();
|
|
|
|
$name = $uploaded->getClientOriginalName();
|
|
|
|
|
2016-03-23 20:41:05 +01:00
|
|
|
if(filesize($filePath)/1000 > MAX_DOCUMENT_SIZE){
|
2016-03-23 03:23:45 +01:00
|
|
|
return Response::json([
|
|
|
|
'error' => 'File too large',
|
|
|
|
'code' => 400
|
|
|
|
], 400);
|
|
|
|
}
|
|
|
|
|
|
|
|
$documentTypeData = Document::$types[$documentType];
|
|
|
|
|
2016-03-23 20:41:05 +01:00
|
|
|
$hash = sha1_file($filePath);
|
2016-03-23 03:23:45 +01:00
|
|
|
$filename = \Auth::user()->account->account_key.'/'.$hash.'.'.$documentTypeData['extension'];
|
|
|
|
|
|
|
|
$document = Document::createNew();
|
|
|
|
$disk = $document->getDisk();
|
|
|
|
if(!$disk->exists($filename)){// Have we already stored the same file
|
2016-03-23 20:41:05 +01:00
|
|
|
$disk->put($filename, file_get_contents($filePath));
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is an image; check if we need to create a preview
|
|
|
|
if(in_array($documentType, array('image/jpeg','image/png','image/gif','image/bmp','image/tiff'))){
|
|
|
|
$makePreview = false;
|
|
|
|
$imageSize = getimagesize($filePath);
|
2016-03-23 23:40:42 +01:00
|
|
|
$width = $imageSize[0];
|
|
|
|
$height = $imageSize[1];
|
2016-03-23 20:41:05 +01:00
|
|
|
$imgManagerConfig = array();
|
|
|
|
if(in_array($documentType, array('image/gif','image/bmp','image/tiff'))){
|
|
|
|
// Needs to be converted
|
|
|
|
$makePreview = true;
|
|
|
|
} else {
|
2016-03-23 23:40:42 +01:00
|
|
|
if($width > DOCUMENT_PREVIEW_SIZE || $height > DOCUMENT_PREVIEW_SIZE){
|
2016-03-23 20:41:05 +01:00
|
|
|
$makePreview = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if($documentType == 'image/bmp' || $documentType == 'image/tiff'){
|
|
|
|
if(!class_exists('Imagick')){
|
|
|
|
// Cant't read this
|
|
|
|
$makePreview = false;
|
|
|
|
} else {
|
|
|
|
$imgManagerConfig['driver'] = 'imagick';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if($makePreview){
|
|
|
|
$previewType = 'jpg';
|
|
|
|
if(in_array($documentType, array('image/png','image/gif','image/bmp','image/tiff'))){
|
|
|
|
// Has transparency
|
|
|
|
$previewType = 'png';
|
|
|
|
}
|
|
|
|
|
|
|
|
$document->preview = \Auth::user()->account->account_key.'/'.$hash.'.'.$documentTypeData['extension'].'.x'.DOCUMENT_PREVIEW_SIZE.'.'.$previewType;
|
|
|
|
if(!$disk->exists($document->preview)){
|
|
|
|
// We haven't created a preview yet
|
|
|
|
$imgManager = new ImageManager($imgManagerConfig);
|
|
|
|
|
|
|
|
$img = $imgManager->make($filePath);
|
2016-03-23 23:40:42 +01:00
|
|
|
|
|
|
|
if($width <= DOCUMENT_PREVIEW_SIZE && $height <= DOCUMENT_PREVIEW_SIZE){
|
|
|
|
$previewWidth = $width;
|
|
|
|
$previewHeight = $height;
|
|
|
|
} else if($width > $height) {
|
|
|
|
$previewWidth = DOCUMENT_PREVIEW_SIZE;
|
|
|
|
$previewHeight = $height * DOCUMENT_PREVIEW_SIZE / $width;
|
|
|
|
} else {
|
|
|
|
$previewHeight = DOCUMENT_PREVIEW_SIZE;
|
|
|
|
$previewWidth = $width * DOCUMENT_PREVIEW_SIZE / $height;
|
|
|
|
}
|
|
|
|
|
|
|
|
$img->resize($previewWidth, $previewHeight);
|
2016-03-23 20:41:05 +01:00
|
|
|
|
|
|
|
$previewContent = (string) $img->encode($previewType);
|
|
|
|
$disk->put($document->preview, $previewContent);
|
2016-03-23 23:40:42 +01:00
|
|
|
$base64 = base64_encode($previewContent);
|
2016-03-23 20:41:05 +01:00
|
|
|
}
|
2016-03-23 23:40:42 +01:00
|
|
|
else{
|
|
|
|
$base64 = base64_encode($disk->get($document->preview));
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
$base64 = base64_encode(file_get_contents($filePath));
|
|
|
|
}
|
2016-03-23 03:23:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$document->path = $filename;
|
|
|
|
$document->type = $documentType;
|
2016-03-23 20:41:05 +01:00
|
|
|
$document->size = filesize($filePath);
|
2016-03-23 03:23:45 +01:00
|
|
|
$document->name = substr($name, -255);
|
|
|
|
|
2016-03-23 20:41:05 +01:00
|
|
|
if(!empty($imageSize)){
|
|
|
|
$document->width = $imageSize[0];
|
|
|
|
$document->height = $imageSize[1];
|
2016-03-23 03:23:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$document->save();
|
2016-03-23 23:40:42 +01:00
|
|
|
$doc_array = $document->toArray();
|
2016-03-23 03:23:45 +01:00
|
|
|
|
2016-03-23 23:40:42 +01:00
|
|
|
if(!empty($base64)){
|
|
|
|
$mime = !empty($previewType)?Document::$extensions[$previewType]:$documentType;
|
|
|
|
$doc_array['base64'] = 'data:'.$mime.';base64,'.$base64;
|
|
|
|
}
|
2016-03-23 03:23:45 +01:00
|
|
|
|
|
|
|
return Response::json([
|
|
|
|
'error' => false,
|
2016-03-23 23:40:42 +01:00
|
|
|
'document' => $doc_array,
|
2016-03-23 03:23:45 +01:00
|
|
|
'code' => 200
|
|
|
|
], 200);
|
|
|
|
}
|
|
|
|
}
|