Commit 63934248 authored by Nicolas Widart's avatar Nicolas Widart

Saving the file data

parent 36374ddb
<?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\Repositories\FileRepository;
use Modules\Media\Services\FileService;
class MediaController
{
......@@ -10,10 +10,14 @@ class MediaController
* @var FileRepository
*/
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
*/
public function store(UploadMediaRequest $request)
{
$file = $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);
$savedFile = $this->fileService->store($request->file('file'));
// Return json response about those
dd($request->file('file'));
return Response::json($savedFile->toArray());
}
/**
......
<?php namespace Modules\Media\Repositories\Eloquent;
use Modules\Core\Repositories\Eloquent\EloquentBaseRepository;
use Modules\Media\Helpers\FileHelper;
use Modules\Media\Repositories\FileRepository;
use Symfony\Component\HttpFoundation\File\UploadedFile;
......@@ -23,8 +24,16 @@ class EloquentFileRepository extends EloquentBaseRepository implements FileRepos
*/
public function createFromFile(UploadedFile $file)
{
$fileName = FileHelper::slug($file->getClientOriginalName());
$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