diff --git a/app/Http/Controllers/InfoSectorialController.php b/app/Http/Controllers/InfoSectorialController.php index da33ae2..4182daf 100644 --- a/app/Http/Controllers/InfoSectorialController.php +++ b/app/Http/Controllers/InfoSectorialController.php @@ -3,7 +3,9 @@ namespace App\Http\Controllers; use App\Http\Requests\UploadInfoSectorialRequest; +use App\Models\InfoSectorial; use App\Traits\ApiResponse; +use Illuminate\Support\Facades\Storage; use Illuminate\Support\Str; class InfoSectorialController extends Controller @@ -14,31 +16,37 @@ class InfoSectorialController extends Controller { $data = $uploadInfoSectorialRequest->all(); - if (!$uploadInfoSectorialRequest->hasFile('reportfile')) { + if (!$uploadInfoSectorialRequest->hasFile('file')) { return $this->errorResponse( false, '', 500); } - $file = $uploadInfoSectorialRequest->file('reportfile'); + $file = $uploadInfoSectorialRequest->file('file'); $data['name'] = Str::of($file->getClientOriginalName())->explode('.')->offsetGet(0); + $data['uid'] = Str::of($file->hashName())->explode('.')->offsetGet(0); $extension = $file->getClientOriginalExtension(); + $data['path'] = $file->storeAs("pdf", $data['uid'].".{$extension}"); - $data['reportfile'] = $file->storeAs('file', $data['name'].".{$extension}"); - - dd($data); + return InfoSectorial::query()->create($data); } public function download() { - $file = public_path("Clockify_Time_Report_Detailed_01_05_2022-31_05_2022.pdf"); + $created_at = InfoSectorial::query()->max('created_at'); - $path = storage_path("public/file/Clockify_Time_Report_Detailed_01_05_2022-31_05_2022.pdf"); + $data = InfoSectorial::query()->where('created_at', '=', $created_at)->first(); + if (Storage::disk('public')->exists($data->path)) + { + $path = Storage::disk('public')->path(($data->path)); - $headers = ['Content-Type: application/pdf']; - $newName = 'itsolutionstuff-pdf-file-'.time().'.pdf'; + $heders = [ + 'Content-Type' => mime_content_type($path) + ]; - return response()->download($file, $newName, $headers); + return response()->download($path, $data->name, $heders); + + } } } diff --git a/app/Http/Requests/UploadInfoSectorialRequest.php b/app/Http/Requests/UploadInfoSectorialRequest.php index 5f71bdb..d359cf7 100644 --- a/app/Http/Requests/UploadInfoSectorialRequest.php +++ b/app/Http/Requests/UploadInfoSectorialRequest.php @@ -24,7 +24,7 @@ class UploadInfoSectorialRequest extends FormRequest public function rules() { return [ - 'reportfile' => 'required|mimes:pdf', + 'file' => ['required','mimes:pdf'], ]; } } diff --git a/app/Models/InfoSectorial.php b/app/Models/InfoSectorial.php new file mode 100644 index 0000000..4f8c732 --- /dev/null +++ b/app/Models/InfoSectorial.php @@ -0,0 +1,36 @@ +format('d/m/Y H:i:s'); + } +} diff --git a/composer.json b/composer.json index 369519b..d6d6141 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,8 @@ "laravel/sanctum": "^2.14.1", "laravel/tinker": "^2.7", "owen-it/laravel-auditing": "^13.0", - "umbrellio/laravel-pg-extensions": "^5.3" + "umbrellio/laravel-pg-extensions": "^5.3", + "ext-fileinfo": "*" }, "require-dev": { "fakerphp/faker": "^1.9.1", diff --git a/database/migrations/2022_06_22_185209_create_info_sectorial_table.php b/database/migrations/2022_06_22_185209_create_info_sectorial_table.php index d3e9c79..c72ea16 100644 --- a/database/migrations/2022_06_22_185209_create_info_sectorial_table.php +++ b/database/migrations/2022_06_22_185209_create_info_sectorial_table.php @@ -15,7 +15,8 @@ return new class extends Migration { Schema::create('info_sectorial', function (Blueprint $table) { $table->id(); - $table->string('title'); + $table->string('uid')->unique()->index(); + $table->string('name'); $table->string('path'); $table->timestamps(); $table->softDeletes(); diff --git a/routes/api.php b/routes/api.php index 20f243e..06628c1 100644 --- a/routes/api.php +++ b/routes/api.php @@ -42,7 +42,7 @@ Route::middleware(['auth:sanctum', 'ability:Admin'])->group(function () { Route::post('faq', [\App\Http\Controllers\FaqController::class, 'store']); Route::delete('faq/{faq}', [\App\Http\Controllers\FaqController::class, 'destroy']); - Route::post('updateFile', [\App\Http\Controllers\InfoSectorialController::class, 'updateFile']); + }); Route::middleware(['auth:sanctum', 'ability:Client'])->group(function () { @@ -60,6 +60,7 @@ Route::middleware(['auth:sanctum', 'ability:Client'])->group(function () { Route::post('operation/summary', [\App\Http\Controllers\OperationSummaryController::class, 'operationSummary']); Route::post('operation', [\App\Http\Controllers\OperationSummaryController::class, 'index']); + Route::post('updateFile', [\App\Http\Controllers\InfoSectorialController::class, 'updateFile']); Route::get('download', [\App\Http\Controllers\InfoSectorialController::class, 'download']); });