Skip to content

Commit

Permalink
s3 Image Upload in user register and s3 show image funciton ready for…
Browse files Browse the repository at this point in the history
… use
  • Loading branch information
DontEdit committed Oct 19, 2023
1 parent ac06368 commit 624eee8
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 140 deletions.
25 changes: 18 additions & 7 deletions app/Http/Controllers/DashboardAdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,16 +216,27 @@ public function registerUser(): RedirectResponse
// remove email_confirm from array
unset($validated['email_confirm']);

// TODO: validate input
//upload picture to S3
if (request()->hasFile('profile_image')) {
$image = request()->file('profile_image')[0]['file'];
$image->store('', 's3');
}
// create the user
$user = User::create($validated);

Session::flash('success', 'Der Account <strong>'.$user->email.'</strong> wurde erfolgreich erstellt.'. var_dump(Request::input('file')));
$given_file = Request::file('profile_image')[0]['file'];
//Check if it is a valid file and valid file
if ($given_file instanceof \Illuminate\Http\UploadedFile && $given_file->isValid()) {
$allowedExtensions = ['jpg', 'jpeg', 'png', 'gif'];
$originalExtension = strtolower($given_file->getClientOriginalExtension());

//naming for all files in s3 Buckt: name nachname Studiengang
if (in_array($originalExtension, $allowedExtensions)) {
$filename = $user->firstname.$user->lastname.$user->course_id .'.'.$originalExtension;
$given_file->storeAs('', $filename, 's3');
} else {
Session::flash('error', 'Ungültige Dateiendung!');
}
} else {
Session::flash('error','Ungültige Datei!');
}

Session::flash('success', 'Der Account <strong>'.$user->email.'</strong> wurde erfolgreich erstellt.');

return Redirect::back();
}
Expand Down
20 changes: 20 additions & 0 deletions app/Http/Controllers/DashboardAdminRandomGeneratorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
use App\Models\State;
use App\Models\User;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Storage;
use Inertia\Inertia;
use Inertia\Response;

Expand Down Expand Up @@ -75,4 +77,22 @@ public function indexExecuteSubmit(): JsonResponse
'success' => true,
]);
}
//TODO: function must be integrated in cooperation with Phil
public function showImage(\Illuminate\Http\Request $request, $filename)
{
$client = Storage::disk('s3')->getClient();
$bucket = Config::get('filesystems.disks.s3.bucket');

$command = $client->getCommand('GetObject', [
'Bucket' => $bucket,
'Key' => 'downtest.png'//Naming convention firstname lastname courseID
// maybe find a solution without having to work with the fileending
]);

$request = $client->createPresignedRequest($command, '+200 minutes');

$url = (string)$request->getUri();

return view('downloadFile', ['url' => $url]);
}
}
47 changes: 0 additions & 47 deletions app/Http/Controllers/FileS3Controller.php

This file was deleted.

32 changes: 0 additions & 32 deletions app/Http/Controllers/ImageUploadController.php

This file was deleted.

21 changes: 12 additions & 9 deletions resources/js/formkit.theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ const checkboxClassification = {
label: "ml-3 text-md text-gray-900 dark:text-gray-200",
};

const fileUploadClassification = {
label: "block text-sm font-medium text-gray-700 dark:text-gray-200",
inner: "max-w-md cursor-pointer",
input:
"text-gray-600 text-sm mb-1 file:mr-4 file:py-2 file:px-4 file:rounded-full file:border-0 file:text-sm file:bg-blue-500 file:text-white hover:file:bg-blue-600",
noFiles: "block text-sm font-medium text-gray-700 dark:text-gray-200",
fileItem: "block text-sm font-medium text-gray-700 dark:text-gray-200",
removeFiles: "block text-sm font-medium text-gray-700 dark:text-gray-200",
};


export default {
global: {
outer: "flex-1 formkit-disabled:opacity-60",
Expand All @@ -42,15 +53,7 @@ export default {
"datetime-local": textClassification,
checkbox: checkboxClassification,
email: textClassification,
file: {
label: "block mb-1 font-bold text-sm",
inner: "max-w-md cursor-pointer",
input:
"text-gray-600 text-sm mb-1 file:mr-4 file:py-2 file:px-4 file:rounded-full file:border-0 file:text-sm file:bg-blue-500 file:text-white hover:file:bg-blue-600",
noFiles: "block text-gray-800 text-sm mb-1",
fileItem: "block flex text-gray-800 text-sm mb-1",
removeFiles: "ml-auto text-blue-500 text-sm",
},
file: fileUploadClassification,
month: textClassification,
number: textClassification,
password: textClassification,
Expand Down
2 changes: 1 addition & 1 deletion resources/js/pages/Dashboard/Admin/Register.vue
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
type="file"
name="profile_image"
label="Profilbild hochladen"
validation="file|image|mimes:jpeg,png,gif"
validation="file|image|mimes:jpeg,jpg,png,gif"
/>
</FormRow>
<FormRow>
Expand Down
10 changes: 0 additions & 10 deletions resources/views/downloadFile.blade.php

This file was deleted.

20 changes: 0 additions & 20 deletions resources/views/uploadFile.blade.php

This file was deleted.

14 changes: 0 additions & 14 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
use App\Http\Middleware\RedirectIfAuthenticated;
use App\Http\Middleware\RedirectIfTutor;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ImageUploadController;
use App\Http\Controllers\FileS3Controller;

/*
|--------------------------------------------------------------------------
Expand All @@ -28,18 +26,6 @@
|
*/

//s3 upload test
Route::get('image-upload', [ ImageUploadController::class, 'imageUpload' ])->name('image.upload');
Route::post('image-upload', [ ImageUploadController::class, 'imageUploadPost' ])->name('image.upload.post');

Route::get('/upload', [\App\Http\Controllers\FileS3Controller::class,'uploadFile']);
Route::get('/download/{filename}', [\App\Http\Controllers\FileS3Controller::class,'showImage'])->name('download.file');
Route::post('/upload', [\App\Http\Controllers\FileS3Controller::class,'uploadFile']);
//Route::get('/download/{filename}', 'FileS3Controller@downloadFile')->name('download.file');




Route::get('/', [AppController::class, 'index'])->name('app.index');

Route::middleware(RedirectIfAuthenticated::class)->group(function () {
Expand Down

0 comments on commit 624eee8

Please sign in to comment.