-
Notifications
You must be signed in to change notification settings - Fork 554
/
FileRepository.php
42 lines (35 loc) · 1.32 KB
/
FileRepository.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
<?php
namespace A17\Twill\Repositories;
use A17\Twill\Models\File;
use A17\Twill\Repositories\Behaviors\HandleTags;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
class FileRepository extends ModuleRepository
{
use HandleTags;
public function __construct(File $model)
{
$this->model = $model;
}
public function afterDelete($object): void
{
$storageId = $object->uuid;
if (Config::get('twill.file_library.cascade_delete')) {
Storage::disk(Config::get('twill.file_library.disk'))->delete($storageId);
// Get the folder and remove it as well if empty.
$folder = Str::finish(Str::beforeLast($storageId, '/'), '/');
if (empty(Storage::disk(Config::get('twill.file_library.disk'))->files($folder))) {
Storage::disk(Config::get('twill.file_library.disk'))->deleteDirectory($folder);
}
}
}
public function prepareFieldsBeforeCreate(array $fields): array
{
if (!isset($fields['size'])) {
$uuid = str_replace(Config::get('filesystems.disks.twill_file_library.root'), '', $fields['uuid']);
$fields['size'] = Storage::disk(Config::get('twill.file_library.disk'))->size($uuid);
}
return $fields;
}
}