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: Upload Image #811

Merged
merged 2 commits into from
Feb 6, 2024
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
1 change: 0 additions & 1 deletion app/Jobs/Discord/SendNewFeature.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public function __construct(int $feature)
/**
* Execute the job.
*
* @return void
*/
public function handle(): void
{
Expand Down
15 changes: 15 additions & 0 deletions app/Livewire/Roadmap/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@

use App\Enums\FeatureStatus;
use App\Models\Feature;
use App\Models\FeatureFile;
use Livewire\Attributes\Validate;
use Livewire\Component;
use Livewire\WithFileUploads;

class Form extends Component
{
use WithFileUploads;

#[Validate('image|nullable|max:3072')] // 3MB Max
public $file;

#[Validate('required|min:5')]
public string $title = '';

Expand All @@ -31,6 +38,14 @@ public function save()
$feat->status_id = FeatureStatus::Draft;
$feat->save();

if ($this->file) {
$file = $this->file->storeAs('features/' . $feat->id, uniqid() . '.' . $this->file->getClientOriginalExtension(), 's3');
$featFile = new FeatureFile();
$featFile->feature_id = $feat->id;
$featFile->path = $file;
$featFile->save();
}

$this->success = true;
$this->title = '';
$this->description = '';
Expand Down
6 changes: 6 additions & 0 deletions app/Models/Feature.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;

/**
Expand Down Expand Up @@ -52,6 +53,11 @@ public function uservote(): HasOne
->where('user_id', auth()->user()->id);
}

public function featureFiles(): HasMany
{
return $this->hasMany(FeatureFile::class, 'feature_id', 'id');
}

public function scopeApproved(Builder $builder): Builder
{
return $builder->whereIn('status_id', [\App\Enums\FeatureStatus::Approved])
Expand Down
25 changes: 25 additions & 0 deletions app/Models/FeatureFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
* @property int $id
* @property int $feature_id
* @property string $path
* @property Feature $feature
*/
class FeatureFile extends Model
{
public $fillable = [
'feature_id',
'path',
];

public function feature(): BelongsTo
{
return $this->belongsTo(Feature::class, 'feature_id');
}
}
3 changes: 1 addition & 2 deletions app/Models/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,14 @@ class Image extends Model
];

/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
* @return BelongsTo
*/
public function user()
{
return $this->belongsTo(User::class, 'created_by');
}

/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function campaign(): BelongsTo
{
Expand Down
159 changes: 159 additions & 0 deletions config/livewire.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
<?php

return [

/*
|---------------------------------------------------------------------------
| Class Namespace
|---------------------------------------------------------------------------
|
| This value sets the root class namespace for Livewire component classes in
| your application. This value will change where component auto-discovery
| finds components. It's also referenced by the file creation commands.
|
*/

'class_namespace' => 'App\\Livewire',

/*
|---------------------------------------------------------------------------
| View Path
|---------------------------------------------------------------------------
|
| This value is used to specify where Livewire component Blade templates are
| stored when running file creation commands like `artisan make:livewire`.
| It is also used if you choose to omit a component's render() method.
|
*/

'view_path' => resource_path('views/livewire'),

/*
|---------------------------------------------------------------------------
| Layout
|---------------------------------------------------------------------------
| The view that will be used as the layout when rendering a single component
| as an entire page via `Route::get('/post/create', CreatePost::class);`.
| In this case, the view returned by CreatePost will render into $slot.
|
*/

'layout' => 'components.layouts.app',

/*
|---------------------------------------------------------------------------
| Lazy Loading Placeholder
|---------------------------------------------------------------------------
| Livewire allows you to lazy load components that would otherwise slow down
| the initial page load. Every component can have a custom placeholder or
| you can define the default placeholder view for all components below.
|
*/

'lazy_placeholder' => null,

/*
|---------------------------------------------------------------------------
| Temporary File Uploads
|---------------------------------------------------------------------------
|
| Livewire handles file uploads by storing uploads in a temporary directory
| before the file is stored permanently. All file uploads are directed to
| a global endpoint for temporary storage. You may configure this below:
|
*/

'temporary_file_upload' => [
'disk' => 'local', // Example: 'local', 's3' | Default: 'default'
'rules' => null, // Example: ['file', 'mimes:png,jpg'] | Default: ['required', 'file', 'max:12288'] (12MB)
'directory' => null, // Example: 'tmp' | Default: 'livewire-tmp'
'middleware' => null, // Example: 'throttle:5,1' | Default: 'throttle:60,1'
'preview_mimes' => [ // Supported file types for temporary pre-signed file URLs...
'png', 'gif', 'bmp', 'svg', 'wav', 'mp4',
'mov', 'avi', 'wmv', 'mp3', 'm4a',
'jpg', 'jpeg', 'mpga', 'webp', 'wma',
],
'max_upload_time' => 5, // Max duration (in minutes) before an upload is invalidated...
],

/*
|---------------------------------------------------------------------------
| Render On Redirect
|---------------------------------------------------------------------------
|
| This value determines if Livewire will run a component's `render()` method
| after a redirect has been triggered using something like `redirect(...)`
| Setting this to true will render the view once more before redirecting
|
*/

'render_on_redirect' => false,

/*
|---------------------------------------------------------------------------
| Eloquent Model Binding
|---------------------------------------------------------------------------
|
| Previous versions of Livewire supported binding directly to eloquent model
| properties using wire:model by default. However, this behavior has been
| deemed too "magical" and has therefore been put under a feature flag.
|
*/

'legacy_model_binding' => false,

/*
|---------------------------------------------------------------------------
| Auto-inject Frontend Assets
|---------------------------------------------------------------------------
|
| By default, Livewire automatically injects its JavaScript and CSS into the
| <head> and <body> of pages containing Livewire components. By disabling
| this behavior, you need to use @livewireStyles and @livewireScripts.
|
*/

'inject_assets' => true,

/*
|---------------------------------------------------------------------------
| Navigate (SPA mode)
|---------------------------------------------------------------------------
|
| By adding `wire:navigate` to links in your Livewire application, Livewire
| will prevent the default link handling and instead request those pages
| via AJAX, creating an SPA-like effect. Configure this behavior here.
|
*/

'navigate' => [
'show_progress_bar' => true,
'progress_bar_color' => '#2299dd',
],

/*
|---------------------------------------------------------------------------
| HTML Morph Markers
|---------------------------------------------------------------------------
|
| Livewire intelligently "morphs" existing HTML into the newly rendered HTML
| after each update. To make this process more reliable, Livewire injects
| "markers" into the rendered Blade surrounding @if, @class & @foreach.
|
*/

'inject_morph_markers' => true,

/*
|---------------------------------------------------------------------------
| Pagination Theme
|---------------------------------------------------------------------------
|
| When enabling Livewire's pagination feature by using the `WithPagination`
| trait, Livewire will use Tailwind templates to render pagination views
| on the page. If you want Bootstrap CSS, you can specify: "bootstrap"
|
*/

'pagination_theme' => 'tailwind',
];
30 changes: 30 additions & 0 deletions database/migrations/2024_02_06_162941_create_feature_files.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class () extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('feature_files', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('feature_id');
$table->string('path')->nullable();
$table->timestamps();

$table->foreign('feature_id')->references('id')->on('features')->onDelete('cascade');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('feature_files');
}
};
7 changes: 7 additions & 0 deletions resources/views/livewire/roadmap/form.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@
</div>
</div>

<div class="field field-description">
<input type="file" wire:model="file">
<div>
@error('file') <span class="text-red-300">{{ $message }}</span> @enderror
</div>
</div>

<p class="text-light">Once reviewed, your idea will show up in the ideas section. If we have questions, we'll contact you on the <a href="https://kanka.io/go/discord" class="link link-light">Discord</a>.</p>

<input type="submit" value="Submit idea" class="btn-round rounded-full" />
Expand Down
1 change: 0 additions & 1 deletion routes/campaigns/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,3 @@

Route::get('/w/{campaign}/search/live', [App\Http\Controllers\Search\LiveController::class, 'index'])->name('search.live');
Route::get('/w/{campaign}/search/recent', [App\Http\Controllers\Search\RecentController::class, 'index'])->name('search.recent');