You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

250 lines
7.9 KiB
PHP

<?php
namespace App\Services;
use App\Models\Asset;
use Illuminate\Http\Request;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Intervention\Image\Facades\Image;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
class DiskFileService implements FileInterface
{
private $dirver = 'local';
private $uploadPath = 'upload';
private $storage;
public function __construct()
{
$this->storage = Storage::disk($this->dirver);
}
public function deleteFile($uid, $callback = '')
{
$asset = Asset::query()->where('uid', $uid)->first();
if ($asset && !empty($asset->path) && !empty($asset->name)) {
$file = $asset->path . $asset->name;
logger('file delete : ' . $file);
if ($this->storage->exists($file)) {
$this->storage->delete($file);
logger('Deleted OK!');
}
$asset->delete();
}
if (!empty($callback)) {
$callback();
}
}
public function saveFile($user, Request $request, $name = 'file')
{
$file = $request->file($name);
if (is_array($file)) {
$files = $file;
$file = null;
$saves = [];
foreach ($files as $file) {
$saves[] = $this->save($user, $file);
}
return $saves;
} else if ($file) {
return $this->save($user, $file);
}
return false;
}
public function save($user, UploadedFile $file)
{
if ($file == null || !$file->isValid()) {
return false;
}
logger($file->getClientOriginalName() . " - " . $file->getMimeType() . " - " . $file->getSize() . " - " . $file->getClientOriginalExtension());
// 날짜 별로 누적
$save_path = $this->uploadPath . '/' . date('Y/m/d/');
$this->storage->makeDirectory($save_path);
$data = array();
$allowedImageMimeTypes = ['image/jpeg','image/gif','image/png','image/bmp','image/svg+xml'];
// 서버 파일 저장용 이름 변경
$save_name = md5(microtime() . $file->getClientOriginalName()) . '.' . $file->getClientOriginalExtension();
$destinationPath = $this->storage->path($save_path);
logger($destinationPath . $save_name);
$data['uid'] = Str::uuid()->toString();
$data['orgin_name'] = $file->getClientOriginalName();
$data['name'] = $save_name;
$data['path'] = $save_path;
$data['type'] = $file->getMimeType();
$data['size'] = $file->getSize();
$data['ext'] = strtolower($file->getClientOriginalExtension());
$data['count'] = 0;
$data['created_id'] = $user->id;
if ($file->move($destinationPath, $save_name)) {
if (in_array($data['type'], $allowedImageMimeTypes)) {
// 가로세로 자동 회전
$image = Image::make($destinationPath . '/' . $save_name);
if($data['ext'] == 'jpg' || $data['ext'] == 'jpeg') {
$image->orientate()->save($destinationPath . '/' . $save_name);
}
// 가로세로 사이즈 구하기
$image = Image::make($destinationPath . '/' . $save_name);
$data['width'] = $image->width();
$data['height'] = $image->height();
}
logger('Saved OK!');
// 데이터 DB 저장
return Asset::query()->create($data);
}
logger('Saved Fail!');
return false;
}
public function download($uid, string $name = null)
{
$headers = array();
$asset = Asset::query()->where('uid', $uid)->first();
if (!$asset || $asset->id == 0) {
abort(404, "파일이 존재하지 않습니다.");
}
$asset->increment('count');
$file = $asset->path . $asset->name;
if (!$this->storage->exists($file)) {
abort(404, '파일이 존재하지 않습니다.');
}
$fullPath = $this->storage->path($file);
//download 통합. 다운로드 파일이름 커스텀시 그것으로 사용.
$down_name = $asset->orgin_name;
if($name) {
$down_name = $name . '.' . $asset->ext;
}
return response()->download($fullPath, $down_name, $headers);
}
public function stream($uid)
{
$asset = Asset::query()->where('uid', $uid)->first();
if (!$asset || $asset->id == 0) {
abort(404, '파일이 존재하지 않습니다.');
}
$asset->increment("count");
$file = $asset->path . $asset->name;
if (!$this->storage->exists($file)) {
abort(404, '파일이 존재하지 않습니다.');
}
$fullPath = $this->storage->path($file);
logger('stream file : ' . $fullPath);
$response = new BinaryFileResponse($fullPath);
BinaryFileResponse::trustXSendfileTypeHeader();
return $response;
}
public function downloadImageRate($uid, $type, $size)
{
$headers = array();
$asset = Asset::query()->where('uid', $uid)->first();
$thumbFolder = $type . $size;
if (!$asset || $asset->id == 0) {
abort(404, '파일이 존재하지 않습니다.');
}
// $asset->increment("count");
$file = $asset->path . $asset->name;
$thumbPath = $asset->path . $thumbFolder;
$thumbFile = $thumbPath . '/' . $asset->name;
if (!$this->storage->exists($thumbFile)) {
if ($this->storage->exists($file)) {
$save_path = $asset->path . $thumbFolder;
$this->storage->makeDirectory($save_path);
$fullPath = $this->storage->path($file);
// 너비 고정
if ($type == 'w') {
Image::make($fullPath)->resize($size, null, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
})->save($this->storage->path($thumbFile));
// 높이 고정
} else if ($type == 'h') {
Image::make($fullPath)->resize(null, $size, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
})->save($this->storage->path($thumbFile));
}
}
}
if (!$this->storage->exists($thumbFile)) {
abort(404, '파일이 존재하지 않습니다.');
}
return response()->download($this->storage->path($thumbFile), $asset->orgin_name, $headers);
}
public function downloadImage($uid, $width, $height)
{
$headers = array();
$asset = Asset::query()->where('uid', $uid)->first();
$thumbFolder = $width .'x'. $height;
if (!$asset || $asset->id == 0) {
abort(404, '파일이 존재하지 않습니다.');
}
$asset->increment('count');
$file = $asset->path . $asset->name;
$thumbPath = $asset->path . $thumbFolder;
$thumbFile = $thumbPath . '/' . $asset->name;
if (!$this->storage->exists($thumbFile)) {
if ($this->storage->exists($file)) {
$save_path = $asset->path . $thumbFolder;
$this->storage->makeDirectory($save_path);
Image::make($this->storage->path($file))->fit($width, $height, function ($constraint) {
$constraint->upsize();
})->save($this->storage->path($thumbFile));
}
}
if (!$this->storage->exists($thumbFile)) {
abort(404, '파일이 존재하지 않습니다.');
}
// logger("------------- download -----------");
return response()->download($this->storage->path($thumbFile), $asset->orgin_name, $headers);
}
}