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

Feature/downloads #268

Merged
merged 8 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ DB_PASSWORD=password
FILESYSTEM_DISK=local
MEDIA_DISK=media
PROFILE_PHOTO_DISK=public
DOWNLOADS_MODULE_DISK=download

BROADCAST_DRIVER=null
CACHE_DRIVER=redis
Expand Down Expand Up @@ -128,3 +129,6 @@ POWERED_BY_EXTRA_LINK=

PING_PROXY_SERVER_USING_IP_ADDRESS=false
QUERY_PROXY_SERVER_USING_IP_ADDRESS=true

MAX_USER_PROFILE_PHOTO_SIZE_KB=512
MAX_USER_COVER_PHOTO_SIZE_KB=1024
6 changes: 4 additions & 2 deletions app/Actions/Fortify/UpdateUserProfileInformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ class UpdateUserProfileInformation implements UpdatesUserProfileInformation
*/
public function update($user, array $input)
{
$maxProfilePhotoSize = config('auth.max_profile_photo_size_kb');
$maxCoverPhotoSize = config('auth.max_cover_photo_size_kb');
Validator::make($input, [
'name' => ['required', 'string', 'max:255'],
// 'email' => ['required', 'email', 'max:255', Rule::unique('users')->ignore($user->id)],
'photo' => ['nullable', 'image', 'max:512'],
'cover_image' => ['nullable', 'image', 'max:1024'],
'photo' => ['nullable', 'image', 'max:' . $maxProfilePhotoSize],
'cover_image' => ['nullable', 'image', 'max:' . $maxCoverPhotoSize],
'dob' => ['nullable', 'date', 'before:today'],
'gender' => ['nullable', 'in:m,f,o'],
'about' => ['nullable', 'string', 'max:255'],
Expand Down
138 changes: 138 additions & 0 deletions app/Http/Controllers/Admin/DownloadController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\CreateDownloadRequest;
use App\Http\Requests\UpdateDownloadRequest;
use App\Models\Download;
use App\Queries\Filters\FilterMultipleFields;
use Inertia\Inertia;
use Spatie\QueryBuilder\AllowedFilter;
use Spatie\QueryBuilder\QueryBuilder;

class DownloadController extends Controller
{
public function index(): \Inertia\Response
{
$this->authorize('viewAny', Download::class);

$perPage = request()->input('perPage', 10);
if ($perPage > 100) {
$perPage = 100;
}

$fields = [
'id',
'name',
'slug',
'description',
'is_external',
'is_external_url_hidden',
'is_only_auth',
'is_active',
'file_name',
'file_url',
'min_role_weight_required',
'download_count',
'created_at',
'updated_at',
];

$downloads = QueryBuilder::for(Download::class)
->select($fields)
->with('media')
->allowedFilters([
...$fields,
AllowedFilter::custom('q', new FilterMultipleFields(['id', 'name', 'description', 'file_name'])),
])
->allowedSorts($fields)
->defaultSort('-id')
->paginate($perPage)
->through(function ($download) {
return $download->append('file')->makeHidden('media');
})
->withQueryString();

return Inertia::render('Admin/Download/IndexDownload', [
'downloads' => $downloads,
'filters' => request()->all(['perPage', 'sort', 'filter']),
]);
}

public function create(): \Inertia\Response
{
$this->authorize('create', Download::class);

return Inertia::render('Admin/Download/CreateDownload');
}

public function store(CreateDownloadRequest $request)
{
$isExternal = $request->is_external;
if ($isExternal) {
$fileUrl = $request->file_url;
}

$download = Download::create([
'name' => $request->name,
'slug' => \Str::slug($request->name),
'description' => $request->description,
'is_external' => $request->is_external,
'is_external_url_hidden' => $request->is_external_url_hidden,
'file_url' => $fileUrl ?? null,
'file_name' => $request->file_name ?? null,
'is_only_auth' => $request->is_only_auth,
'min_role_weight_required' => $request->min_role_weight_required,
'is_active' => $request->is_active,
'created_by' => $request->user()->id,
]);

// Upload the File if !isExternal
if (! $isExternal && $request->hasFile('file')) {
$download->addMediaFromRequest('file')->toMediaCollection('download');
}

return redirect()->route('admin.download.index')
->with(['toast' => ['type' => 'success', 'title' => __('Created Successfully'), 'body' => __('Download has been created successfully')]]);
}

public function edit(Download $download): \Inertia\Response
{
$this->authorize('update', Download::class);

return Inertia::render('Admin/Download/EditDownload', [
'download' => $download->append('file')->makeHidden('media'),
]);
}

public function update(UpdateDownloadRequest $request, Download $download)
{
$this->authorize('update', $download);

$download->update([
'name' => $request->name,
'slug' => \Str::slug($request->name),
'description' => $request->description,
'is_external_url_hidden' => $request->is_external_url_hidden,
'file_name' => $request->file_name ?? null,
'is_only_auth' => $request->is_only_auth,
'min_role_weight_required' => $request->min_role_weight_required,
'is_active' => $request->is_active,
'updated_by' => $request->user()->id,
]);

return redirect()->route('admin.download.index')
->with(['toast' => ['type' => 'success', 'title' => __('Updated Successfully'), 'body' => __('Download has been updated successfully')]]);
}

public function destroy(Download $download)
{
$this->authorize('delete', $download);

$download->delete();

return redirect()->route('admin.download.index')
->with(['toast' => ['type' => 'success', 'title' => __('Deleted Successfully'), 'body' => __('Download has been deleted permanently')]]);
}
}
90 changes: 90 additions & 0 deletions app/Http/Controllers/DownloadController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace App\Http\Controllers;

use App\Models\Download;
use App\Queries\Filters\FilterMultipleFields;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Spatie\QueryBuilder\AllowedFilter;
use Spatie\QueryBuilder\QueryBuilder;

class DownloadController extends Controller
{
public function index(Request $request): \Inertia\Response
{
$isAuthenticated = (bool) $request->user();

$perPage = request()->input('perPage', 10);
if ($perPage > 100) {
$perPage = 100;
}

$fields = [
'id',
'name',
'slug',
'is_active',
'download_count',
'created_at',
'updated_at',
];
$downloads = QueryBuilder::for(Download::class)
->select($fields)
->allowedFilters([
...$fields,
AllowedFilter::custom('q', new FilterMultipleFields(['id', 'name', 'description'])),
])
->allowedSorts($fields)
->defaultSort('-download_count')
->paginate($perPage)
->withQueryString();

return Inertia::render('Download/IndexDownload', [
'downloads' => $downloads,
'filters' => request()->all(['perPage', 'sort', 'filter']),
]);
}

public function show(Request $request, Download $download)
{
$this->authorize('download', $download);

return Inertia::render('Download/ShowDownload', [
'download' => $download->only([
'id',
'name',
'slug',
'description',
'is_active',
'download_count',
'created_at',
'updated_at',
]),
]);
}

public function download(Download $download)
{
$this->authorize('download', $download);

$download->increment('download_count');

$isExternal = $download->is_external;
$isExternalUrlHidden = $download->is_external_url_hidden;

if (! $isExternal) {
$file = $download->file;

return response()->download($file->getPath(), $download->file_name);
}

if ($isExternal && $isExternalUrlHidden) {
return response()->streamDownload(function () use ($download) {
echo file_get_contents($download->file_url);
}, $download->file_name);
}

return redirect($download->file_url);
}
}
20 changes: 11 additions & 9 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace App\Http\Controllers;

use App\Models\Role;
use App\Models\User;
use Illuminate\Http\Request;
use Inertia\Inertia;

class UserController extends Controller
Expand All @@ -15,20 +13,24 @@ public function showProfile(User $user): \Inertia\Response
->makeHidden(['email', 'dob', 'gender', 'updated_at', 'provider_id', 'provider_name', 'two_factor_confirmed_at', 'settings']);

return Inertia::render('User/ShowUser', [
'profileUser' => $user->load('badges:id,name,shortname,sort_order')
'profileUser' => $user->load('badges:id,name,shortname,sort_order'),
]);
}

public function indexStaff(): \Inertia\Response
{
$rolesWithUsers = Role::with('users:id,name,username,profile_photo_path,verified_at')
->where('is_hidden_from_staff_list', false)
->orderByDesc('weight')
->select(['id', 'name', 'display_name', 'is_staff', 'color'])
->where('is_staff', true)->get();
$staffsWithRole = User::with(['roles' => function ($query) {
$query->where('is_hidden_from_staff_list', false)
->orderByDesc('weight');
}])
->whereHas('roles', function ($query) {
$query->where('is_staff', true);
})
->select(['id', 'name', 'username', 'profile_photo_path', 'verified_at'])
->get();

return Inertia::render('User/IndexStaff', [
'rolesWithUsers' => $rolesWithUsers
'staffs' => $staffsWithRole,
]);
}
}
32 changes: 32 additions & 0 deletions app/Http/Requests/CreateDownloadRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Http\Requests;

use App\Models\Download;
use Gate;
use Illuminate\Foundation\Http\FormRequest;

class CreateDownloadRequest extends FormRequest
{
public function authorize(): bool
{
return Gate::allows('create', Download::class);
}

public function rules(): array
{
return [
'name' => 'required|string|max:255|unique:downloads',
'description' => 'nullable|string',
'is_external' => 'required|boolean',
'file_url' => 'nullable|required_if:is_external,true|url',
'file_name' => 'nullable|required_if:is_external,true|string|max:255',
'is_external_url_hidden' => 'nullable|required_if:is_external,true|boolean',
'is_only_auth' => 'required|boolean',
'min_role_weight_required' => 'nullable|integer',
'is_active' => 'required|boolean',

'file' => 'nullable|required_if:is_external,false|file|max:102400', //100mb
];
}
}
32 changes: 32 additions & 0 deletions app/Http/Requests/UpdateDownloadRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class UpdateDownloadRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}

public function rules(): array
{
return [
'name' => [
'required',
'string',
'max:255',
Rule::unique('downloads')->ignore($this->route('download')),
],
'description' => 'nullable|string',
'file_name' => 'nullable|required_if:is_external,true|string|max:255',
'is_external_url_hidden' => 'nullable|required_if:is_external,true|boolean',
'is_only_auth' => 'required|boolean',
'min_role_weight_required' => 'nullable|integer',
'is_active' => 'required|boolean',
];
}
}
Loading