"git@code.castopod.org:adaures/castopod.git" did not exist on "e6bfdfc3902705285701c13c8067fe0f538425c6"
Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
declare(strict_types=1);
/**
* @copyright 2020 Podlibre
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
* @link https://castopod.org/
*/
namespace App\Models;
use App\Entities\Audio;
use App\Entities\Image;
use App\Entities\Media;
use CodeIgniter\Database\ConnectionInterface;
use CodeIgniter\Model;
use CodeIgniter\Validation\ValidationInterface;
class MediaModel extends Model
{
/**
* @var string
*/
protected $table = 'media';
/**
* @var string
*/
protected $returnType = Media::class;
/**
* @var string[]
*/
protected $allowedFields = [
'id',
'file_path',
'file_size',
'file_content_type',
'file_metadata',
'type',
'description',
'language_code',
'uploaded_by',
'updated_by',
];
/**
* Model constructor.
*
* @param ConnectionInterface|null $db DB Connection
* @param ValidationInterface|null $validation Validation
*/
public function __construct(
protected string $fileType,
ConnectionInterface &$db = null,
ValidationInterface $validation = null
) {
switch ($fileType) {
case 'audio':
$this->returnType = Audio::class;
break;
case 'image':
$this->returnType = Image::class;
break;
default:
// do nothing, keep Media class as default
break;
}
parent::__construct($db, $validation);
}
/**
* @return Media|Image|Audio
*/
public function getMediaById(int $mediaId): object
{
$cacheName = "media#{$mediaId}";
if (! ($found = cache($cacheName))) {
$builder = $this->where([
'id' => $mediaId,
]);
$found = $builder->first();
cache()
->save($cacheName, $found, DECADE);
}
return $found;
}
/**
* @param Media|Image $media
*/
public function saveMedia(object $media): int | false
{
// insert record in database
if (! $mediaId = $this->insert($media, true)) {
return false;
}
// @phpstan-ignore-next-line
return $mediaId;
}
public function deleteFile(int $mediaId): void
{
// TODO: get file, delete it from disk & from database
}
}