added attchments
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
<?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('attachments', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('thread_id')->nullable()->constrained('threads')->nullOnDelete();
|
||||
$table->foreignId('post_id')->nullable()->constrained('posts')->nullOnDelete();
|
||||
$table->foreignId('user_id')->nullable()->constrained('users')->nullOnDelete();
|
||||
$table->string('disk', 50)->default('local');
|
||||
$table->string('path');
|
||||
$table->string('original_name');
|
||||
$table->string('extension', 30)->nullable();
|
||||
$table->string('mime_type', 150);
|
||||
$table->unsignedBigInteger('size_bytes');
|
||||
$table->timestamps();
|
||||
$table->softDeletes();
|
||||
|
||||
$table->index('thread_id', 'idx_attachments_thread_id');
|
||||
$table->index('post_id', 'idx_attachments_post_id');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('attachments');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('attachment_groups', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('name', 150);
|
||||
$table->unsignedInteger('max_size_kb')->default(25600);
|
||||
$table->boolean('is_active')->default(true);
|
||||
$table->timestamps();
|
||||
});
|
||||
|
||||
if (Schema::hasTable('attachment_types')) {
|
||||
$types = DB::table('attachment_types')->orderBy('id')->get();
|
||||
foreach ($types as $type) {
|
||||
DB::table('attachment_groups')->insert([
|
||||
'name' => $type->label ?? $type->key ?? 'General',
|
||||
'max_size_kb' => $type->max_size_kb ?? 25600,
|
||||
'is_active' => $type->is_active ?? true,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
if (DB::table('attachment_groups')->count() === 0) {
|
||||
DB::table('attachment_groups')->insert([
|
||||
'name' => 'General',
|
||||
'max_size_kb' => 25600,
|
||||
'is_active' => true,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('attachment_groups');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('attachment_extensions', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('extension', 30)->unique();
|
||||
$table->foreignId('attachment_group_id')->nullable()->constrained('attachment_groups')->nullOnDelete();
|
||||
$table->json('allowed_mimes')->nullable();
|
||||
$table->timestamps();
|
||||
});
|
||||
|
||||
if (Schema::hasTable('attachment_types') && Schema::hasTable('attachment_groups')) {
|
||||
$groups = DB::table('attachment_groups')->orderBy('id')->get()->values();
|
||||
$types = DB::table('attachment_types')->orderBy('id')->get()->values();
|
||||
|
||||
foreach ($types as $index => $type) {
|
||||
$group = $groups[$index] ?? null;
|
||||
if (!$group) {
|
||||
continue;
|
||||
}
|
||||
$extensions = [];
|
||||
if (!empty($type->allowed_extensions)) {
|
||||
$decoded = json_decode($type->allowed_extensions, true);
|
||||
if (is_array($decoded)) {
|
||||
$extensions = $decoded;
|
||||
}
|
||||
}
|
||||
foreach ($extensions as $ext) {
|
||||
$ext = strtolower(trim((string) $ext));
|
||||
if ($ext === '') {
|
||||
continue;
|
||||
}
|
||||
DB::table('attachment_extensions')->updateOrInsert(
|
||||
['extension' => $ext],
|
||||
[
|
||||
'attachment_group_id' => $group->id,
|
||||
'allowed_mimes' => $type->allowed_mimes,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('attachment_extensions');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('attachments', function (Blueprint $table) {
|
||||
$table->foreignId('attachment_extension_id')->nullable()->constrained('attachment_extensions')->nullOnDelete();
|
||||
$table->foreignId('attachment_group_id')->nullable()->constrained('attachment_groups')->nullOnDelete();
|
||||
$table->index('attachment_extension_id', 'idx_attachments_extension_id');
|
||||
$table->index('attachment_group_id', 'idx_attachments_group_id');
|
||||
});
|
||||
|
||||
if (Schema::hasTable('attachment_extensions')) {
|
||||
$extensions = DB::table('attachment_extensions')->get()->keyBy('extension');
|
||||
$attachments = DB::table('attachments')->select('id', 'extension')->get();
|
||||
foreach ($attachments as $attachment) {
|
||||
$ext = strtolower(trim((string) $attachment->extension));
|
||||
if ($ext === '' || !$extensions->has($ext)) {
|
||||
continue;
|
||||
}
|
||||
$extRow = $extensions->get($ext);
|
||||
DB::table('attachments')
|
||||
->where('id', $attachment->id)
|
||||
->update([
|
||||
'attachment_extension_id' => $extRow->id,
|
||||
'attachment_group_id' => $extRow->attachment_group_id,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('attachments', function (Blueprint $table) {
|
||||
$table->dropIndex('idx_attachments_extension_id');
|
||||
$table->dropIndex('idx_attachments_group_id');
|
||||
$table->dropConstrainedForeignId('attachment_extension_id');
|
||||
$table->dropConstrainedForeignId('attachment_group_id');
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,35 @@
|
||||
<?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
|
||||
{
|
||||
if (Schema::hasColumn('attachments', 'attachment_type_id')) {
|
||||
Schema::table('attachments', function (Blueprint $table) {
|
||||
$table->dropForeign(['attachment_type_id']);
|
||||
$table->dropIndex('idx_attachments_type_id');
|
||||
$table->dropColumn('attachment_type_id');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
if (!Schema::hasColumn('attachments', 'attachment_type_id')) {
|
||||
Schema::table('attachments', function (Blueprint $table) {
|
||||
$table->foreignId('attachment_type_id')->constrained('attachment_types');
|
||||
$table->index('attachment_type_id', 'idx_attachments_type_id');
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::dropIfExists('attachment_types');
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
// Intentionally left empty. attachment_types is deprecated.
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
if (Schema::hasColumn('attachment_groups', 'category')) {
|
||||
Schema::table('attachment_groups', function (Blueprint $table) {
|
||||
$table->dropColumn('category');
|
||||
});
|
||||
}
|
||||
|
||||
if (Schema::hasColumn('attachment_groups', 'allowed_mimes')) {
|
||||
if (Schema::hasTable('attachment_extensions')) {
|
||||
if (!Schema::hasColumn('attachment_extensions', 'allowed_mimes')) {
|
||||
Schema::table('attachment_extensions', function (Blueprint $table) {
|
||||
$table->json('allowed_mimes')->nullable();
|
||||
});
|
||||
}
|
||||
|
||||
$groups = DB::table('attachment_groups')
|
||||
->select('id', 'allowed_mimes')
|
||||
->get()
|
||||
->keyBy('id');
|
||||
|
||||
$extensions = DB::table('attachment_extensions')
|
||||
->select('id', 'attachment_group_id', 'allowed_mimes')
|
||||
->get();
|
||||
|
||||
foreach ($extensions as $extension) {
|
||||
if (!empty($extension->allowed_mimes)) {
|
||||
continue;
|
||||
}
|
||||
$group = $groups->get($extension->attachment_group_id);
|
||||
if (!$group || empty($group->allowed_mimes)) {
|
||||
continue;
|
||||
}
|
||||
DB::table('attachment_extensions')
|
||||
->where('id', $extension->id)
|
||||
->update([
|
||||
'allowed_mimes' => $group->allowed_mimes,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Schema::table('attachment_groups', function (Blueprint $table) {
|
||||
$table->dropColumn('allowed_mimes');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
if (!Schema::hasColumn('attachment_groups', 'category')) {
|
||||
Schema::table('attachment_groups', function (Blueprint $table) {
|
||||
$table->string('category', 50)->default('other');
|
||||
});
|
||||
}
|
||||
|
||||
if (!Schema::hasColumn('attachment_groups', 'allowed_mimes')) {
|
||||
Schema::table('attachment_groups', function (Blueprint $table) {
|
||||
$table->json('allowed_mimes')->nullable();
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('attachment_groups', function (Blueprint $table) {
|
||||
$table->foreignId('parent_id')->nullable()->constrained('attachment_groups')->nullOnDelete();
|
||||
$table->unsignedInteger('position')->default(1);
|
||||
$table->index(['parent_id', 'position'], 'idx_attachment_groups_parent_position');
|
||||
});
|
||||
|
||||
$groups = DB::table('attachment_groups')->orderBy('id')->get();
|
||||
$position = 1;
|
||||
foreach ($groups as $group) {
|
||||
DB::table('attachment_groups')
|
||||
->where('id', $group->id)
|
||||
->update(['position' => $position++]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('attachment_groups', function (Blueprint $table) {
|
||||
$table->dropIndex('idx_attachment_groups_parent_position');
|
||||
$table->dropConstrainedForeignId('parent_id');
|
||||
$table->dropColumn('position');
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user