/
MediaRepository.php
53 lines (43 loc) · 1.49 KB
/
MediaRepository.php
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
<?php
namespace A17\Twill\Repositories;
use A17\Twill\Models\Media;
use A17\Twill\Repositories\Behaviors\HandleTags;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Storage;
use ImageService;
class MediaRepository extends ModuleRepository
{
use HandleTags;
/**
* @param Media $model
*/
public function __construct(Media $model)
{
$this->model = $model;
}
public function filter($query, array $scopes = [])
{
$this->searchIn($query, $scopes, 'search', ['alt_text', 'filename', 'caption']);
return parent::filter($query, $scopes);
}
public function afterDelete($object)
{
$storageId = $object->uuid;
if (Config::get('twill.media_library.cascade_delete')) {
Storage::disk(Config::get('twill.media_library.disk'))->delete($storageId);
}
}
public function prepareFieldsBeforeCreate($fields)
{
if (Config::get('twill.media_library.init_alt_text_from_filename', true)) {
$fields['alt_text'] = $this->model->altTextFrom($fields['filename']);
}
// if we were not able to determine dimensions with the browser File API, let's ask the Image service
if (!isset($fields['width']) || !isset($fields['height'])) {
$dimensions = ImageService::getDimensions($fields['uuid']);
$fields['width'] = $dimensions['width'] ?? 0;
$fields['height'] = $dimensions['height'] ?? 0;
}
return $fields;
}
}