@@ -7,6 +7,7 @@ use App\PostsTag;
|
||||
use App\Services\TagDetectorService;
|
||||
use App\Tag;
|
||||
use DateTime;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Redirect;
|
||||
|
||||
@@ -121,18 +122,27 @@ class HomeController extends Controller
|
||||
|
||||
public function store(TagDetectorService $tag_detector)
|
||||
{
|
||||
$today = new DateTime();
|
||||
$user_id = Auth::user()->getAuthIdentifier();
|
||||
|
||||
$data = request()->validate([
|
||||
'message' => 'required'
|
||||
$validated_data = request()->validate([
|
||||
'message' => 'required',
|
||||
'file' => 'file|mimes:jpeg,png,jpg,gif,svg',
|
||||
]);
|
||||
|
||||
$data = [
|
||||
'user_id' => Auth::user()->getAuthIdentifier(),
|
||||
'date_post' => new DateTime(), // Take back the date from the form ?
|
||||
'content' => $data['message'],
|
||||
'user_id' => $user_id,
|
||||
'date_post' => new DateTime(),
|
||||
'content' => $validated_data['message'],
|
||||
];
|
||||
|
||||
if (!empty($validated_data['file'])) {
|
||||
/** @var UploadedFile $uploaded_document */
|
||||
$uploaded_document = $validated_data['file'];
|
||||
$file_name = $uploaded_document->getClientOriginalName();
|
||||
$uploaded_document->storeAs(Auth::user()->getFolder(), $file_name);
|
||||
$data['image'] = $file_name;
|
||||
}
|
||||
|
||||
$tags = $tag_detector->detectFrom($data['content']);
|
||||
|
||||
$check = Post::create($data);
|
||||
|
22
app/User.php
22
app/User.php
@@ -36,4 +36,26 @@ class User extends Authenticatable
|
||||
protected $casts = [
|
||||
'email_verified_at' => 'datetime',
|
||||
];
|
||||
|
||||
public function getFolder()
|
||||
{
|
||||
$arrayHash = str_split(strtolower(md5($this->id)));
|
||||
return sprintf('%s/%s', $arrayHash[0], $arrayHash[1]);
|
||||
}
|
||||
|
||||
public function getImageData(Post $post)
|
||||
{
|
||||
if (empty($post->image)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$path = sprintf('%s/%s/%s', storage_path('app'), $this->getFolder(), $post->image);
|
||||
|
||||
if (!is_readable($path)) {
|
||||
return '';
|
||||
}
|
||||
$type = pathinfo($path, PATHINFO_EXTENSION);
|
||||
$data = file_get_contents($path);
|
||||
return sprintf('data:image/%s;base64,%s', $type, base64_encode($data));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user