Skip to content

Commit e09876c

Browse files
committed
coverage test
- created media model
1 parent c874cff commit e09876c

File tree

13 files changed

+771
-128
lines changed

13 files changed

+771
-128
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"php": "^8.2",
2020
"illuminate/support": "^10.2 || ^11.0 || ^12.0",
2121
"spatie/laravel-medialibrary": "^11.12",
22-
"javaabu/helpers": "^1.61",
22+
"javaabu/helpers": "^1.64",
2323
"javaabu/activitylog": "^1.4"
2424
},
2525
"require-dev": {

config/mediapicker.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525

2626
'attachment_model' => \Javaabu\Mediapicker\Models\Attachment::class,
2727

28+
/*
29+
* Collection name to be used for media picker uploads
30+
*/
31+
'collection_name' => 'mediapicker',
32+
2833
/*
2934
|--------------------------------------------------------------------------
3035
| Attachment Observer

src/Concerns/OwnsMedia.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
namespace Javaabu\Mediapicker\Concerns;
4+
5+
use Javaabu\Helpers\Media\AllowedMimeTypes;
6+
use Javaabu\Mediapicker\Mediapicker;
7+
use Spatie\Image\Enums\Fit;
8+
use Spatie\MediaLibrary\MediaCollections\File;
9+
use Spatie\MediaLibrary\MediaCollections\Models\Media;
10+
11+
trait OwnsMedia
12+
{
13+
public function getMediapickerCollectionName(): string
14+
{
15+
return Mediapicker::collectionName();
16+
}
17+
18+
/*
19+
* Register the media picker collections.
20+
*/
21+
public function registerMediapickerCollections()
22+
{
23+
$this->addMediaCollection($this->getMediapickerCollectionName())
24+
->acceptsFile(function (File $file) {
25+
return AllowedMimeTypes::isAllowedMimeType($file->mimeType);
26+
});
27+
}
28+
29+
/*
30+
* Register the media picker conversions.
31+
*/
32+
public function registerMediapickerConversions(?Media $media = null)
33+
{
34+
$this->addMediaConversion('mediapicker-large')
35+
->fit(Fit::Max, 1200, 1200)
36+
->width(1200)
37+
->height(1200)
38+
->keepOriginalImageFormat()
39+
->shouldBePerformedOn($this->getMediapickerCollectionName());
40+
41+
$this->addMediaConversion('mediapicker-thumb')
42+
->width(250)
43+
->height(250)
44+
->fit(Fit::Crop, 250, 250)
45+
->keepOriginalImageFormat()
46+
->shouldBePerformedOn($this->getMediapickerCollectionName());
47+
}
48+
49+
/*
50+
* Check if can owns the given media
51+
*/
52+
public function ownsMedia(Media $media): bool
53+
{
54+
return $media->model_type == $this->getMorphClass() && $media->model_id == $this->getKey();
55+
}
56+
57+
public function canViewAnyMedia(): bool
58+
{
59+
return $this->can('view_media');
60+
}
61+
62+
public function canCreateMedia(): bool
63+
{
64+
return $this->can('edit_media');
65+
}
66+
67+
public function canViewOthersMedia(): bool
68+
{
69+
return $this->can('view_others_media');
70+
}
71+
72+
public function canEditOthersMedia(): bool
73+
{
74+
return $this->can('edit_others_media');
75+
}
76+
77+
public function canDeleteOthersMedia(): bool
78+
{
79+
return $this->can('delete_others_media');
80+
}
81+
82+
public function canViewMedia(Media $media): bool
83+
{
84+
return $this->canViewAnyMedia() &&
85+
($this->canViewOthersMedia() || $this->ownsMedia($media));
86+
}
87+
88+
public function canEditMedia(Media $media): bool
89+
{
90+
return $this->canCreateMedia() &&
91+
($this->canEditOthersMedia() || $this->ownsMedia($media));
92+
}
93+
94+
public function canDeleteMedia(Media $media): bool
95+
{
96+
return $this->can('delete_media') &&
97+
($this->canDeleteOthersMedia() || $this->ownsMedia($media));
98+
}
99+
}

src/Contracts/Media.php

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/Contracts/MediaOwner.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Javaabu\Mediapicker\Contracts;
4+
5+
use Illuminate\Contracts\Auth\Access\Authorizable;
6+
use Spatie\MediaLibrary\HasMedia;
7+
use Spatie\MediaLibrary\MediaCollections\Models\Media;
8+
9+
interface MediaOwner extends HasMedia, Authorizable
10+
{
11+
/*
12+
* Register the media picker collections.
13+
*/
14+
public function registerMediapickerCollections();
15+
16+
/*
17+
* Register the media picker conversions.
18+
*/
19+
public function registerMediapickerConversions(?Media $media = null);
20+
21+
public function getMediapickerCollectionName(): string;
22+
23+
/*
24+
* Check if can owns the given media
25+
*/
26+
public function ownsMedia(Media $media): bool;
27+
28+
public function canViewAnyMedia(): bool;
29+
30+
public function canCreateMedia(): bool;
31+
32+
public function canViewOthersMedia(): bool;
33+
34+
public function canEditOthersMedia(): bool;
35+
36+
public function canDeleteOthersMedia(): bool;
37+
38+
public function canViewMedia(Media $media): bool;
39+
40+
public function canEditMedia(Media $media): bool;
41+
42+
public function canDeleteMedia(Media $media): bool;
43+
}

0 commit comments

Comments
 (0)