53 lines
1.7 KiB
PHP
53 lines
1.7 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace App\Http\Controllers\Admin;
|
||
|
|
|
||
|
|
use App\Http\Controllers\Controller;
|
||
|
|
use App\Models\File;
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
use Illuminate\Support\Facades\Storage;
|
||
|
|
|
||
|
|
class FileController extends Controller
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* make directory in Public Storage By Date
|
||
|
|
*
|
||
|
|
* @return string
|
||
|
|
*/
|
||
|
|
protected function createDir()
|
||
|
|
{
|
||
|
|
$now = Verta();
|
||
|
|
$path = preg_replace('/-/', '/', $now->formatDate());
|
||
|
|
if (!Storage::exists($path)) {
|
||
|
|
Storage::makeDirectory($path);
|
||
|
|
Storage::makeDirectory($path . '/' . 'thumb');
|
||
|
|
}
|
||
|
|
return $path;
|
||
|
|
}
|
||
|
|
public function store(Request $request)
|
||
|
|
{
|
||
|
|
// Store FIle in Public Storage By Date
|
||
|
|
// Date Sample is : <Year>/<Month>/<Day> of today
|
||
|
|
|
||
|
|
$data = $request->all();
|
||
|
|
$path = $this->createDir();
|
||
|
|
$data['original_name'] = $request->file('file')->getClientOriginalName();
|
||
|
|
$data['ext'] = $request->file('file')->getClientOriginalExtension();
|
||
|
|
if (!isset($data['title']) || is_null($data['title'])) {
|
||
|
|
$data['title'] = $data['original_name'];
|
||
|
|
}
|
||
|
|
$file_name_src = md5(time() . $data['original_name']);
|
||
|
|
$file_name = $file_name_src . '.' . $data['ext'];
|
||
|
|
$file = $request->file('file');
|
||
|
|
$data['has_thumb'] = false;
|
||
|
|
$data['size'] = $file->getSize();
|
||
|
|
$file->move(public_path('storage/' . $path . '/'), $file_name);
|
||
|
|
$data['src'] = 'storage/' . $path . '/' . $file_name;
|
||
|
|
// $data['size'] = $file->getSize();
|
||
|
|
$data['user_id'] = auth()->id();
|
||
|
|
$data['sts'] = 1;
|
||
|
|
$file_create = File::create($data);
|
||
|
|
return response()->json($file_create);
|
||
|
|
}
|
||
|
|
}
|