Commit 63934248 authored by Nicolas Widart's avatar Nicolas Widart

Saving the file data

parent 36374ddb
<?php namespace Modules\Media\Http\Controllers\Api; <?php namespace Modules\Media\Http\Controllers\Api;
use Illuminate\Support\Str; use Illuminate\Support\Facades\Response;
use Modules\Media\Http\Requests\UploadMediaRequest; use Modules\Media\Http\Requests\UploadMediaRequest;
use Modules\Media\Repositories\FileRepository; use Modules\Media\Services\FileService;
class MediaController class MediaController
{ {
...@@ -10,10 +10,14 @@ class MediaController ...@@ -10,10 +10,14 @@ class MediaController
* @var FileRepository * @var FileRepository
*/ */
private $file; private $file;
/**
* @var FileService
*/
private $fileService;
public function __construct(FileRepository $file) public function __construct(FileService $fileService)
{ {
$this->file = $file; $this->fileService = $fileService;
} }
/** /**
...@@ -44,17 +48,9 @@ class MediaController ...@@ -44,17 +48,9 @@ class MediaController
*/ */
public function store(UploadMediaRequest $request) public function store(UploadMediaRequest $request)
{ {
$file = $request->file('file'); $savedFile = $this->fileService->store($request->file('file'));
$fileName = Str::slug($file->getClientOriginalName());
// Move the uploaded file to /public/assets/media/
$file->move(public_path() . '/assets/media', $fileName);
// Save the file info to db
$savedFile = $this->file->createFromFile($file);
// Return json response about those return Response::json($savedFile->toArray());
dd($request->file('file'));
} }
/** /**
......
<?php namespace Modules\Media\Repositories\Eloquent; <?php namespace Modules\Media\Repositories\Eloquent;
use Modules\Core\Repositories\Eloquent\EloquentBaseRepository; use Modules\Core\Repositories\Eloquent\EloquentBaseRepository;
use Modules\Media\Helpers\FileHelper;
use Modules\Media\Repositories\FileRepository; use Modules\Media\Repositories\FileRepository;
use Symfony\Component\HttpFoundation\File\UploadedFile; use Symfony\Component\HttpFoundation\File\UploadedFile;
...@@ -23,8 +24,16 @@ class EloquentFileRepository extends EloquentBaseRepository implements FileRepos ...@@ -23,8 +24,16 @@ class EloquentFileRepository extends EloquentBaseRepository implements FileRepos
*/ */
public function createFromFile(UploadedFile $file) public function createFromFile(UploadedFile $file)
{ {
$fileName = FileHelper::slug($file->getClientOriginalName());
$this->model->create([ $this->model->create([
'filename' => '' 'filename' => $fileName,
'path' => public_path() . "/assets/media/{$fileName}",
'extension' => $file->guessClientExtension(),
'mimetype' => $file->getClientMimeType(),
'filesize' => $file->getFileInfo()->getSize(),
]); ]);
return $this->model;
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment