08 2024
This commit is contained in:
parent
04d677d37a
commit
bfa3bb1df4
1191 changed files with 637397 additions and 10619 deletions
151
app/Repositories/DC/FileRepository.php
Normal file
151
app/Repositories/DC/FileRepository.php
Normal file
|
|
@ -0,0 +1,151 @@
|
|||
<?php
|
||||
|
||||
namespace App\Repositories\DC;
|
||||
|
||||
use Imagick;
|
||||
use Request;
|
||||
use App\Models\DcFile;
|
||||
use App\Services\Util;
|
||||
use App\Models\DcFileTag;
|
||||
use App\Repositories\BaseRepository;
|
||||
use Intervention\Image\Facades\Image;
|
||||
|
||||
|
||||
class FileRepository extends BaseRepository {
|
||||
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
public function uploadFile( $form_data )
|
||||
{
|
||||
$file = $form_data['file'];
|
||||
$originalName = $file->getClientOriginalName();
|
||||
$extension = $file->getClientOriginalExtension();
|
||||
$mine = $file->getClientMimeType();
|
||||
$size = $file->getSize();
|
||||
|
||||
$originalNameWithoutExt = substr($originalName, 0, strlen($originalName) - strlen($extension) - 1);
|
||||
$filename = Util::sanitize($originalNameWithoutExt, true, false, true);
|
||||
$allowed_filename = uniqid() . '_' . $filename.".".$extension;
|
||||
$file->storeAs('dc/files', $allowed_filename, 'public');
|
||||
// $store = $file->store('files/');
|
||||
|
||||
$dc_file = DcFile::create([
|
||||
'filename' => $allowed_filename,
|
||||
'original_name' => $originalName,
|
||||
'ext' => $extension,
|
||||
'mine' => $mine,
|
||||
'size' => $size
|
||||
]);
|
||||
$this->makeThumbFromFile($dc_file);
|
||||
return $dc_file;
|
||||
}
|
||||
|
||||
public function makeThumb($id){
|
||||
$file = DcFile::findOrFail($id);
|
||||
$this->makeThumbFromFile($file);
|
||||
}
|
||||
|
||||
public function makeThumbFromFile($file)
|
||||
{
|
||||
$path = $file->getFile(true);
|
||||
$filename = $file->filename;
|
||||
$mine = \File::mimeType($path);
|
||||
//make thumb
|
||||
$allowedMimeTypes = ['image/jpeg','image/gif','image/png'];
|
||||
if (in_array($mine, $allowedMimeTypes)) {
|
||||
$img = Image::make($path);
|
||||
$img->resize(542, 360, function ($c) {
|
||||
$c->aspectRatio();
|
||||
$c->upsize();
|
||||
});
|
||||
\Storage::disk('public')->put('dc/thumb/'.basename($filename), (string) $img->encode());
|
||||
|
||||
$img = Image::make($path);
|
||||
$img->resize(1600, 900, function ($c) {
|
||||
$c->aspectRatio();
|
||||
$c->upsize();
|
||||
});
|
||||
\Storage::disk('public')->put('dc/big/'.basename($filename), (string) $img->encode());
|
||||
}
|
||||
|
||||
$allowedMimeTypes = ['application/pdf'];
|
||||
if (in_array($mine, $allowedMimeTypes)) {
|
||||
$imagick = new \Imagick ($path.'[0]'); // 0 specifies the first page of the pdf
|
||||
$imagick->setImageBackgroundColor('#ffffff');
|
||||
$imagick = $imagick->mergeImageLayers( Imagick::LAYERMETHOD_FLATTEN );
|
||||
$imagick->setImageFormat('jpg'); // set the format of the output image
|
||||
$imagick->setImageCompression(\Imagick::COMPRESSION_JPEG);
|
||||
$imagick->setImageCompressionQuality(60);
|
||||
|
||||
$imagick->resizeImage(1600, 900, \Imagick::FILTER_LANCZOS, 1,1);
|
||||
$filestore = \Storage::disk('public')->path('dc/big/').basename($filename).".jpg";
|
||||
$imagick->writeImage($filestore);
|
||||
|
||||
$imagick->resizeImage(542, 360, \Imagick::FILTER_LANCZOS, 1,1);
|
||||
$filestore = \Storage::disk('public')->path('dc/thumb/').basename($filename).".jpg";
|
||||
$imagick->writeImage($filestore);
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function tagsUpdate($id, $tags){
|
||||
$file = DcFile::findOrFail($id);
|
||||
$file_tags = DcFileTag::where('file_id', $file->id)->get();
|
||||
|
||||
//remove Tags
|
||||
foreach ($file_tags as $file_tag) {
|
||||
if(is_array($tags) && ($pos = array_search($file_tag->tag_id, $tags)) !== FALSE){
|
||||
unset($tags[$pos]);
|
||||
}else{
|
||||
$file_tag->delete();
|
||||
}
|
||||
}
|
||||
//set taTagsgs
|
||||
if(is_array($tags)){
|
||||
foreach ($tags as $key => $tag_id) {
|
||||
DcFileTag::create([
|
||||
'file_id' => $file->id,
|
||||
'tag_id' => $tag_id
|
||||
]);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public function deleteThumb($id){
|
||||
$file = DcFile::findOrFail($id);
|
||||
$path_thumb = $file->getThumb(true);
|
||||
if (file_exists($path_thumb)) {
|
||||
unlink($path_thumb);
|
||||
}
|
||||
$path_big = $file->getBig(true);
|
||||
if (file_exists($path_big)) {
|
||||
unlink($path_big);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public function deleteFile($id){
|
||||
$file = DcFile::findOrFail($id);
|
||||
$path_thumb = $file->getThumb(true);
|
||||
if (file_exists($path_thumb)) {
|
||||
unlink($path_thumb);
|
||||
}
|
||||
$path_big = $file->getBig(true);
|
||||
if (file_exists($path_big)) {
|
||||
unlink($path_big);
|
||||
}
|
||||
$path = $file->getFile(true);
|
||||
if (file_exists($path)) {
|
||||
unlink($path);
|
||||
}
|
||||
$file->delete();
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue