Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for user images via S3 #514

Merged
merged 5 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion app/Http/Controllers/DashboardAdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use Illuminate\Support\Facades\Session;
use Inertia\Inertia;
use Inertia\Response;
use Illuminate\Support\Str;
use \Illuminate\Http\UploadedFile;
use Spatie\Permission\Models\Role;

class DashboardAdminController extends Controller
Expand Down Expand Up @@ -260,7 +262,7 @@ public function eventExecuteSubmit(IlluminateRequest $request): RedirectResponse
/**
* Display the register page
*/
public function register(): Response
public function register(Request $request): Response
{
// get courses ordered by name
$courses = Course::orderBy('name')->get();
Expand Down Expand Up @@ -301,6 +303,25 @@ public function registerUser(): RedirectResponse

// create the user
$user = User::create($validated);
if(!is_null(Request::file('profile_image')[0]['file'])) {
$given_file = Request::file('profile_image')[0]['file'];
}
//Check if it is a valid file and valid file
if ($given_file instanceof UploadedFile && $given_file->isValid()) {
$allowedExtensions = ['jpg', 'jpeg', 'png', 'gif'];
$originalExtension = strtolower($given_file->getClientOriginalExtension());

//naming for all files in s3 Buckt: name nachname Studiengang
TitusKirch marked this conversation as resolved.
Show resolved Hide resolved
if (in_array($originalExtension, $allowedExtensions)) {
$uuid = Str::uuid()->toString();
$filename = $uuid.'.'.$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.');

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,9 +6,12 @@
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;
use Illuminate\Http\Request as Req;

class DashboardAdminRandomGeneratorController extends Controller
{
Expand Down Expand Up @@ -75,4 +78,21 @@ public function indexExecuteSubmit(): JsonResponse
'success' => true,
]);
}
//TODO: function must be implemented. See issue #224
public function showImage(Req $request, $filename)
{
$client = Storage::disk('s3')->getClient();
$bucket = Config::get('filesystems.disks.s3.bucket');

$command = $client->getCommand('GetObject', [
'Bucket' => $bucket,
'Key' => $request->post('uuid') //read uuid from Server depends on Vue implementation
]);
//Time is Link expiry, but link doesnt seem to expiry not sure why
$request = $client->createPresignedRequest($command, '+20 minutes');

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

return view('downloadFile', ['url' => $url]);
}
}
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
"license": "MIT",
"require": {
"php": "^8.1",
"aws/aws-sdk-php": "^3.283",
"based/laravel-typescript": "^0.0.4",
"guzzlehttp/guzzle": "^7.2",
"inertiajs/inertia-laravel": "^0.6.9",
"laravel/framework": "^10.15",
"laravel/octane": "^2.0",
"laravel/tinker": "^2.8",
"league/flysystem-aws-s3-v3": "^3.16",
"owen-it/laravel-auditing": "^13.5.1",
"spatie/laravel-permission": "^5.11",
"spiral/roadrunner": "^2023.2.2",
Expand Down
Loading