Add File.category

This commit is contained in:
Martin Rüegg 2023-09-23 13:09:51 +02:00
parent 3480969ae6
commit 1f709ea194
No known key found for this signature in database
3 changed files with 93 additions and 0 deletions

View File

@ -3,6 +3,7 @@ HumHub Changelog
1.15.0-beta.2 (Unreleased)
--------------------------
- Enh #6591: Add field `file.category`
- Fix #6551: Migration's transaction with invalid savepoint
- Fix #6549: Empty profile field type causing an unspecific error
- Enh #6529: Add boolean return-type to `*safe*` methods in migrations

View File

@ -0,0 +1,60 @@
<?php
/*
* @link https://www.humhub.org/
* @copyright Copyright (c) 2023 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
use humhub\components\Migration;
use humhub\modules\file\models\File;
/**
* Add and film GUID column
*/
class m230618_135509_file_add_category_column extends Migration
{
// protected properties
protected string $table;
public function __construct($config = [])
{
$this->table = File::tableName();
parent::__construct($config);
}
/**
* {@inheritdoc}
*/
public function safeUp(): void
{
$command = Yii::$app->getDb()
->createCommand()
;
$schema = Yii::$app->getDb()->getTableSchema($this->table, true);
$after = $schema->getColumn('state') ? 'state' : 'guid';
$this->safeAddColumn(
$this->table,
'category',
$this->integer(11)
->unsigned()
->after($after)
);
$this->safeCreateIndex("ix-$this->table-category", $this->table, ['category', 'object_model', 'object_id']);
$command->update(
$this->table,
['category' => File::CATEGORY_ATTACHED_FILE],
[
'and',
['not', ['object_model' => null]],
['not', ['object_id' => null]],
]
)
->execute()
;
}
}

View File

@ -34,6 +34,7 @@ use yii\web\UploadedFile;
*
* @property integer $id
* @property string $guid
* @property integer|null $category
* @property string $file_name
* @property string $title
* @property string $mime_type
@ -62,6 +63,24 @@ use yii\web\UploadedFile;
*/
class File extends FileCompat
{
/**
* Categories are still experimental. Expect changes in v1.16
*
* Generally speaking, there are two value-ranges:
* - Byte 1, bits 1-8, or values 1-32767:
* reserved for humhub
* - Byte 2, bits 9-16, or values 32768-65535:
* available for the object_model of the owning record.
*/
public const CATEGORY_ATTACHED_FILE = 16;
public const CATEGORY_ATTACHED_IMAGE = self::CATEGORY_ATTACHED_FILE + self::CATEGORY_VARIANT_1; // 17 = 16 + 1
public const CATEGORY_BANNER_IMAGE = self::CATEGORY_ATTACHED_FILE + self::CATEGORY_VARIANT_1 + self::CATEGORY_VARIANT_2; // 19 = 16 + 1 + 2
public const CATEGORY_OG_IMAGE = self::CATEGORY_ATTACHED_FILE + self::CATEGORY_VARIANT_1 + self::CATEGORY_VARIANT_4; // 21 = 16 + 1 + 4
public const CATEGORY_VARIANT_1 = 1;
public const CATEGORY_VARIANT_2 = 2;
public const CATEGORY_VARIANT_4 = 4;
public const CATEGORY_VARIANT_8 = 8;
/**
* @event Event that is triggered after a new file content has been stored.
*/
@ -107,6 +126,7 @@ class File extends FileCompat
'pattern' => '/[^a-zA-Z0-9\.ä\/\-\+]/',
'message' => Yii::t('FileModule.base', 'Invalid Mime-Type')
],
[['category'], 'integer'], // ToDo: merge with size after all fields have been added
[['file_name', 'title'], 'string', 'max' => 255],
[['size'], 'integer'],
];
@ -165,6 +185,18 @@ class File extends FileCompat
$this->old_updated_by = $this->getOldAttribute('updated_by');
$this->old_updated_at = $this->getOldAttribute('updated_at');
if ($this->category === null) {
/** @noinspection NestedPositiveIfStatementsInspection */
if ($this->object_model && $this->object_id !== null) {
$this->category = self::CATEGORY_ATTACHED_FILE;
$mime_type = $this->mime_type;
if ($mime_type && str_starts_with($mime_type, 'image/')) {
$this->category = self::CATEGORY_ATTACHED_IMAGE;
}
}
}
return parent::beforeSave($insert);
}