encoding troubleshooting

This commit is contained in:
Čarodej
2021-09-08 18:18:36 +02:00
parent 1db153aeef
commit a9556896ca
6 changed files with 219 additions and 187 deletions
+23 -30
View File
@@ -39,10 +39,10 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
*/
class File extends Model
{
use Searchable;
use SoftDeletes;
use Sortable;
use Searchable;
use HasFactory;
use Sortable;
public ?string $public_access = null;
@@ -76,6 +76,11 @@ class File extends Model
return FileFactory::new();
}
public function setNameAttribute($name): void
{
$this->attributes['name'] = mb_convert_encoding($name, 'UTF-8');
}
/**
* Set routes with public access
*/
@@ -84,14 +89,6 @@ class File extends Model
$this->public_access = $token;
}
/**
* Format fileSize
*/
public function getFilesizeAttribute(): string
{
return Metric::bytes($this->attributes['filesize'])->format();
}
/**
* Format thumbnail url
*/
@@ -149,36 +146,32 @@ class File extends Model
return $route;
}
/**
* Index file
*/
public function toSearchableArray(): array
{
$array = $this->toArray();
$name = Str::slug($array['name'], ' ');
return [
'id' => $this->id,
'name' => $name,
'nameNgrams' => utf8_encode((new TNTIndexer)->buildTrigrams(implode(', ', [$name]))),
];
}
public function parent(): BelongsTo
{
return $this->belongsTo(Folder::class, 'folder_id', 'id');
}
public function folder(): HasOne
{
return $this->hasOne(Folder::class, 'id', 'folder_id');
}
public function shared(): HasOne
{
return $this->hasOne(Share::class, 'item_id', 'id');
}
public function toSearchableArray(): array
{
$name = mb_convert_encoding(
mb_strtolower($this->name, 'UTF-8'), 'UTF-8'
);
$trigram = (new TNTIndexer)
->buildTrigrams(implode(', ', [$name]));
return [
'id' => $this->id,
'name' => $name,
'nameNgrams' => $trigram,
];
}
protected static function boot()
{
parent::boot();
+6 -2
View File
@@ -1,6 +1,7 @@
<?php
namespace Domain\Files\Resources;
use ByteUnits\Metric;
use Domain\Sharing\Resources\ShareResource;
use Illuminate\Http\Resources\Json\JsonResource;
@@ -9,21 +10,24 @@ class FileResource extends JsonResource
/**
* Transform the resource into an array.
*
* TODO: optimize created_at/updated_at conversion because of performance issue
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
// TODO: optimize created_at/updated_at conversion because of performance issue
$fileSize = Metric::bytes($this->filesize)->format();
return [
'data' => [
'id' => $this->id,
'type' => $this->type,
'attributes' => [
'filesize' => $fileSize,
'name' => $this->name,
'basename' => $this->basename,
'mimetype' => $this->mimetype,
'filesize' => $this->filesize,
'file_url' => $this->file_url,
'thumbnail' => $this->thumbnail,
'metadata' => $this->metadata,