328 lines
8.4 KiB
PHP
Executable File
328 lines
8.4 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Logic\Document;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Facades\Response;
|
|
use Illuminate\Support\Facades\Config;
|
|
use Illuminate\Support\Facades\File;
|
|
use Intervention\Image\ImageManager;
|
|
use Image;
|
|
use App\Models\Document;
|
|
use PhpParser\Comment\Doc;
|
|
|
|
class DocumentRepository
|
|
{
|
|
static public function upload($model, $id, $filename, $extension, $filedata, $user_id, $is_preview = false)
|
|
{
|
|
$originalNameWithoutExt = substr($filename, 0, strlen($filename) - strlen($extension) - 1);
|
|
|
|
$original_without_ext = DocumentRepository::sanitize($originalNameWithoutExt);
|
|
$allowed_filename = DocumentRepository::createUniqueFilename( $original_without_ext, $extension );
|
|
|
|
$path = DocumentRepository::getWebPath($model, $id);
|
|
$pathThumbs = DocumentRepository::getWebPreviewPath($model, $id);
|
|
|
|
$uploadSuccess1 = DocumentRepository::saveOriginal($filedata, $allowed_filename, $extension, $path);
|
|
$uploadSuccess2 = true;
|
|
|
|
$mimeType = 'unknown';
|
|
|
|
if(DocumentRepository::isImage($filedata, $extension))
|
|
{
|
|
$uploadSuccess2 = DocumentRepository::saveIcon($filedata, $allowed_filename, $pathThumbs);
|
|
}
|
|
|
|
if( !$uploadSuccess1 || !$uploadSuccess2 ) {
|
|
return array(
|
|
'error' => true,
|
|
'message' => 'Server error while uploading',
|
|
'code' => 500
|
|
);
|
|
}
|
|
else
|
|
{
|
|
$mimeType = DocumentRepository::getMimeType($path, $allowed_filename);
|
|
}
|
|
|
|
// Wenn das Dokument das erste für diesen Post ist, dann wird es automatisch zum Startbild
|
|
$documentCount = Document::where('model', $model)
|
|
->where('model_id', $id)
|
|
->count();
|
|
if($documentCount == 0)
|
|
{
|
|
$is_preview = true;
|
|
}
|
|
|
|
$sessionFile = new Document;
|
|
$sessionFile->filename = $allowed_filename;
|
|
$sessionFile->original_name = $filename;
|
|
$sessionFile->mime_type = $mimeType;
|
|
$sessionFile->user_id = $user_id;
|
|
$sessionFile->model = $model;
|
|
$sessionFile->model_id = $id;
|
|
$sessionFile->save();
|
|
|
|
// Wenn das Dokument als Startbild festgelegt werden soll,
|
|
// muss zuerst überprüft werden, ob nicht schon ein anderes Dokument als Startbild festgelegt wurde
|
|
if($is_preview == true)
|
|
{
|
|
DocumentRepository::changePreviewFile($model, $id, $allowed_filename);
|
|
}
|
|
|
|
return array(
|
|
'error' => false,
|
|
'code' => 200,
|
|
'fileinfo' => $sessionFile,
|
|
);
|
|
}
|
|
|
|
static public function changePreviewFile($model, $model_id, $filename)
|
|
{
|
|
$documents = Document::where('model', $model)
|
|
->where('model_id', $model_id)
|
|
->where('is_preview', true)
|
|
->where('filename', '!=', $filename)
|
|
->get();
|
|
foreach($documents as $doc)
|
|
{
|
|
$doc->is_preview = false;
|
|
$doc->save();
|
|
}
|
|
|
|
$document = Document::where('filename', $filename)->first();
|
|
if($document)
|
|
{
|
|
$document->is_preview = true;
|
|
$document->save();
|
|
}
|
|
|
|
return DocumentRepository::getEntityFiles($model, $model_id);
|
|
}
|
|
|
|
static public function createUniqueFilename( $filename, $extension )
|
|
{
|
|
$exists = Document::where('filename', $filename.'.'.$extension)->count();
|
|
|
|
if($exists)
|
|
{
|
|
// Generate token for image
|
|
$imageToken = substr(sha1(mt_rand()), 0, 5);
|
|
return $filename . '-' . $imageToken . '.' . $extension;
|
|
}
|
|
|
|
return $filename . '.' . $extension;
|
|
}
|
|
|
|
/**
|
|
* Optimize Original Document
|
|
*/
|
|
static public function saveOriginal( $file, $filename, $extension, $path)
|
|
{
|
|
if(DocumentRepository::isImage($file, $extension))
|
|
{
|
|
$manager = new ImageManager();
|
|
$image = $manager->make( $file );
|
|
|
|
$image->resize(config('images.full_size_width'), config('images.full_size_height'), function($constraint)
|
|
{
|
|
$constraint->aspectRatio();
|
|
});
|
|
$image->orientate();
|
|
return Storage::disk('upload')->put($path.$filename, $image->encode());
|
|
}
|
|
else
|
|
{
|
|
if(substr($filename, 0, 1) != '.')
|
|
{
|
|
return Storage::disk('upload')->put($path.$filename, file_get_contents($file));
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Create Icon From Original
|
|
*/
|
|
static public function saveIcon( $photo, $filename, $path)
|
|
{
|
|
$manager = new ImageManager();
|
|
$image = $manager->make( $photo );
|
|
$image->resize(config('images.icon_size_width'), config('images.icon_size_height'), function ($constraint)
|
|
{
|
|
$constraint->aspectRatio();
|
|
});
|
|
$image->orientate();
|
|
return Storage::disk('upload')->put($path.$filename, $image->encode());
|
|
|
|
// $image->save( $path . $filename );
|
|
|
|
return $image;
|
|
}
|
|
|
|
/**
|
|
* Delete Document From Session folder, based on original filename
|
|
*/
|
|
static public function delete($filename)
|
|
{
|
|
$document = Document::where('filename', 'like', $filename)->first();
|
|
$model = $document->model;
|
|
$id = $document->model_id;
|
|
$isPreview = $document->is_preview;
|
|
|
|
$full_size_dir = DocumentRepository::getWebPath($document->model, $document->model_id);
|
|
$icon_size_dir = DocumentRepository::getWebPreviewPath($document->model, $document->model_id);
|
|
|
|
if(empty($document))
|
|
{
|
|
return Response::json([
|
|
'error' => true,
|
|
'code' => 400
|
|
], 400);
|
|
}
|
|
|
|
$full_path1 = $full_size_dir . $document->filename;
|
|
$full_path2 = $icon_size_dir . $document->filename;
|
|
|
|
Storage::delete($full_path1);
|
|
Storage::delete($full_path2);
|
|
|
|
if( !empty($document))
|
|
{
|
|
$document->delete();
|
|
}
|
|
|
|
// Wenn das Dokument das Startbild war, dann wird ein anderes zum Startbild
|
|
if($isPreview)
|
|
{
|
|
$document = Document::where('model', $model)
|
|
->where('model_id', $id)
|
|
->first();
|
|
if($document)
|
|
{
|
|
DocumentRepository::changePreviewFile($model, $id, $document->filename);
|
|
}
|
|
}
|
|
|
|
return DocumentRepository::getEntityFiles($model, $id);
|
|
}
|
|
|
|
static public function get($path, $filename)
|
|
{
|
|
$file = Storage::disk('upload')->get($path.$filename);
|
|
|
|
return $file;
|
|
}
|
|
|
|
static function sanitize($string, $force_lowercase = true, $anal = false)
|
|
{
|
|
$strip = array("~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "=", "+", "[", "{", "]",
|
|
"}", "\\", "|", ";", ":", "\"", "'", "‘", "’", "“", "”", "–", "—",
|
|
"—", "–", ",", "<", ".", ">", "/", "?");
|
|
$clean = trim(str_replace($strip, "", strip_tags($string)));
|
|
$clean = preg_replace('/\s+/', "-", $clean);
|
|
$clean = ($anal) ? preg_replace("/[^a-zA-Z0-9]/", "", $clean) : $clean ;
|
|
|
|
return ($force_lowercase) ?
|
|
(function_exists('mb_strtolower')) ?
|
|
mb_strtolower($clean, 'UTF-8') :
|
|
strtolower($clean) :
|
|
$clean;
|
|
}
|
|
|
|
static public function getWebPath($model, $id)
|
|
{
|
|
return '/uploads/'.$model.'/'.$id.'/';
|
|
}
|
|
|
|
static public function getWebPreviewPath($model, $id)
|
|
{
|
|
return DocumentRepository::getWebPath($model, $id).'thumbs/';
|
|
}
|
|
|
|
static public function getDirectory($model, $id)
|
|
{
|
|
$path = public_path(DocumentRepository::getWebPath($model, $id));
|
|
DocumentRepository::createPath($path);
|
|
|
|
return $path;
|
|
}
|
|
|
|
static public function getPreviewDirectory($model, $id)
|
|
{
|
|
$path = public_path(DocumentRepository::getWebPreviewPath($model, $id));
|
|
DocumentRepository::createPath($path);
|
|
|
|
return $path;
|
|
}
|
|
|
|
static private function createPath($path) {
|
|
if (is_dir($path))
|
|
{
|
|
return true;
|
|
}
|
|
$prev_path = substr($path, 0, strrpos($path, '/', -2) + 1 );
|
|
$return = DocumentRepository::createPath($prev_path);
|
|
|
|
return ($return && is_writable($prev_path)) ? mkdir($path) : false;
|
|
}
|
|
|
|
static private function isImage($file, $extension)
|
|
{
|
|
$isImage = false;
|
|
|
|
switch(strtoupper($extension))
|
|
{
|
|
// Bei Bildern wird ein Miniaturbild erstellt
|
|
case 'PNG':
|
|
case 'GIF':
|
|
case 'JPG':
|
|
case 'JPEG':
|
|
case 'BMP':
|
|
$isImage = true;
|
|
break;
|
|
}
|
|
|
|
return $isImage;
|
|
}
|
|
|
|
static public function getMimeType($path, $filename)
|
|
{
|
|
return Storage::disk('upload')->mimeType($path.$filename);
|
|
}
|
|
|
|
static public function getEntityFiles($model, $id)
|
|
{
|
|
$documents = Document::where('model', $model)
|
|
->where('model_id', $id)
|
|
->get();
|
|
|
|
$documentsAnswer = array();
|
|
foreach ($documents as $doc)
|
|
{
|
|
$documentsAnswer[] = [
|
|
'path' => $doc->path,
|
|
'previewPath' => $doc->previewpath,
|
|
'filesize' => $doc->filesize,
|
|
'filename' => $doc->filename,
|
|
'filetype' => $doc->filetype,
|
|
'isPreview' => $doc->is_preview
|
|
];
|
|
}
|
|
|
|
return $documentsAnswer;
|
|
}
|
|
|
|
static public function getInfoByOriginalName($model, $id, $originalFilename)
|
|
{
|
|
$document = Document::where('model', $model)
|
|
->where('model_id', $id)
|
|
->where('original_name', $originalFilename)
|
|
->latest()
|
|
->first();
|
|
|
|
if($document)
|
|
{
|
|
return $document;
|
|
}
|
|
}
|
|
} |