forked from projects/fipamo
file upload service added
ported over the new file uploader from the old build and made it a service make for from some additonal file processing i.e. image optimization or video converstion before it is saved to the system
This commit is contained in:
parent
bd5f57b9ac
commit
6ce5e91624
5 changed files with 107 additions and 1 deletions
22
app/Http/Controllers/API/FileUploadAPIController.php
Normal file
22
app/Http/Controllers/API/FileUploadAPIController.php
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers\API;
|
||||||
|
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use App\Services\FileUploadService;
|
||||||
|
|
||||||
|
class FileUploadAPIController extends Controller
|
||||||
|
{
|
||||||
|
protected $upload;
|
||||||
|
|
||||||
|
public function __construct(FileUploadService $fileUploadService)
|
||||||
|
{
|
||||||
|
$this->upload = $fileUploadService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function upload(Request $request, $type = null)
|
||||||
|
{
|
||||||
|
return $this->upload->handleFile($request, $type);
|
||||||
|
}
|
||||||
|
}
|
|
@ -11,6 +11,7 @@ use App\Services\ContentService;
|
||||||
use App\Services\PaginateService;
|
use App\Services\PaginateService;
|
||||||
use App\Services\ThemeService;
|
use App\Services\ThemeService;
|
||||||
use App\Services\DocService;
|
use App\Services\DocService;
|
||||||
|
use App\Services\FileUploadService;
|
||||||
|
|
||||||
class FipamoServiceProvider extends ServiceProvider
|
class FipamoServiceProvider extends ServiceProvider
|
||||||
{
|
{
|
||||||
|
@ -47,6 +48,10 @@ class FipamoServiceProvider extends ServiceProvider
|
||||||
$this->app->bind(DocService::class, function ($app) {
|
$this->app->bind(DocService::class, function ($app) {
|
||||||
return new DocService();
|
return new DocService();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
$this->app->bind(FileUploadService::class, function ($app) {
|
||||||
|
return new FileUploadService();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
77
app/Services/FileUploadService.php
Normal file
77
app/Services/FileUploadService.php
Normal file
|
@ -0,0 +1,77 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Services;
|
||||||
|
|
||||||
|
class FileUploadService
|
||||||
|
{
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function handleFile($request, $type = null)
|
||||||
|
{
|
||||||
|
//$upload = $request->getUploadedFiles(); //grab uploaded files
|
||||||
|
//$options = $request->getParsedBody();
|
||||||
|
//$file = $upload['upload_files'][0];
|
||||||
|
//front end sends one by one for progress tracking, so grab first
|
||||||
|
$file = $request->file('upload_files');
|
||||||
|
$type = $file[0]->extension();
|
||||||
|
$public_path = '../public';
|
||||||
|
$filesPath = '';
|
||||||
|
$path = date('Y') . '/' . date('m');
|
||||||
|
$response = [];
|
||||||
|
switch ($type) {
|
||||||
|
case 'jpeg':
|
||||||
|
case 'jpg':
|
||||||
|
case 'png':
|
||||||
|
case 'gif':
|
||||||
|
if (isset($options["source"])) {
|
||||||
|
if ($options["source"] == "avatar-upload") {
|
||||||
|
$filesPath = '/assets/images/user/' . $path . '/';
|
||||||
|
//Member::updateData('avi', $filesPath . $file->getClientFileName());
|
||||||
|
} else {
|
||||||
|
$filesPath = '/assets/images/user/' . $path . '/';
|
||||||
|
// Settings::updateGlobalData('background', $filesPath . '/' . $file->getClientFileName());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$filesPath = '/assets/images/blog/' . $path . '/';
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
case 'video/mp4':
|
||||||
|
$filesPath = '/assets/video/blog/' . $path . '/';
|
||||||
|
break;
|
||||||
|
case 'audio/mpeg':
|
||||||
|
$filesPath = '/assets/sound/blog/' . $path . '/';
|
||||||
|
break;
|
||||||
|
case 'application/pdf':
|
||||||
|
case 'text/plain':
|
||||||
|
case 'text/rtf':
|
||||||
|
$filesPath = '/assets/docs/blog/' . $path . '/';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
//FileUploader::uploadFile('../public' . $filesPath, $file);
|
||||||
|
$response = [];
|
||||||
|
try {
|
||||||
|
// if does not exist, so lets create it.
|
||||||
|
//var_dump($filesPath);
|
||||||
|
if (!is_dir($public_path . $filesPath)) {
|
||||||
|
mkdir($public_path . $filesPath, 0755, true);
|
||||||
|
}
|
||||||
|
$filename = urlencode($file[0]->getClientOriginalName());
|
||||||
|
$file[0]->move($public_path . $filesPath, $filename);
|
||||||
|
} catch (RuntimeException $e) {
|
||||||
|
echo 'ERROR ' . $e->getMessage();
|
||||||
|
}
|
||||||
|
|
||||||
|
$response = [
|
||||||
|
'message' => "File Uploaded. Great!",
|
||||||
|
"filePath" => $filesPath . urlencode($file[0]->getClientOriginalName()),
|
||||||
|
"fileName" => urlencode($file[0]->getClientOriginalName()),
|
||||||
|
'type' => $type,
|
||||||
|
];
|
||||||
|
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
}
|
|
@ -117,7 +117,7 @@ export default class FileManager {
|
||||||
self.mm
|
self.mm
|
||||||
.filesUpload(theFile.type, upload, progress)
|
.filesUpload(theFile.type, upload, progress)
|
||||||
.then(result => {
|
.then(result => {
|
||||||
item.setAttribute('data-source', result.filePath);
|
item.setAttribute('data-id', result.filePath);
|
||||||
item.style.background =
|
item.style.background =
|
||||||
'url(' +
|
'url(' +
|
||||||
f.target.result +
|
f.target.result +
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
use App\Http\Controllers\API\AuthAPIController;
|
use App\Http\Controllers\API\AuthAPIController;
|
||||||
use App\Http\Controllers\API\PageAPIController;
|
use App\Http\Controllers\API\PageAPIController;
|
||||||
|
use App\Http\Controllers\API\FileUploadAPIController;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
|
@ -18,3 +19,4 @@ use App\Http\Controllers\API\PageAPIController;
|
||||||
//check if session is active
|
//check if session is active
|
||||||
Route::get("/v1/status", [AuthAPIController::class, 'status']);
|
Route::get("/v1/status", [AuthAPIController::class, 'status']);
|
||||||
Route::put("/v1/page/write", [PageAPIController::class, 'write']);
|
Route::put("/v1/page/write", [PageAPIController::class, 'write']);
|
||||||
|
Route::post("/v1/files", [FileUploadAPIController::class, 'upload']);
|
||||||
|
|
Loading…
Reference in a new issue