Skip to content

Commit bb23629

Browse files
authored
Merge pull request #59 from atriatech/v2
added a config
2 parents 71095ab + 235b6f4 commit bb23629

5 files changed

Lines changed: 20 additions & 8 deletions

File tree

src/AtriatechMedia.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
namespace Atriatech\Media;
44

5+
use Atriatech\Media\Facades\AtriatechMedia as AtriatechMediaFacade;
6+
57
trait AtriatechMedia
68
{
79
public function media()
810
{
9-
return $this->morphToMany(Medium::class, 'media_item', 'medium_items', 'media_item_id', 'medium_id')->withPivot('name');
11+
return $this->morphToMany(AtriatechMediaFacade::medium_model(), 'media_item', 'medium_items', 'media_item_id', 'medium_id')->withPivot('name');
1012
}
1113

1214
public function addMedia($paths)

src/Facades/AtriatechMedia.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
/**
88
* @method static bool upload($file, $path = '')
9+
* @method static Medium medium_model()
910
*/
1011

1112
class AtriatechMedia extends Facade {

src/Helpers/MediumHelper.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Atriatech\Media\Helpers;
44

5-
use Atriatech\Media\Medium;
65
use Illuminate\Support\Facades\Storage;
76
use Intervention\Image\Facades\Image;
87

@@ -25,7 +24,7 @@ public static function upload($file, $path = '')
2524
$counter++;
2625
}
2726
$file->storeAs($path, $fileName);
28-
$media = Medium::create([
27+
$media = self::medium_model()::create([
2928
'path' => $path . $fileName,
3029
'mime_type' => $file->getMimeType(),
3130
]);
@@ -126,4 +125,9 @@ public static function upload($file, $path = '')
126125
'updated_at' => $media->updated_at,
127126
];
128127
}
128+
129+
public static function medium_model()
130+
{
131+
return config('atriatech_media.medium_model') ?? Atriatech\Media\Medium::class;
132+
}
129133
}

src/MediumController.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function getFiles(Request $request)
4343
}
4444
$dirs = $this->getDirectories($path);
4545

46-
$files = Medium::whereRaw("REPLACE(path, SUBSTRING_INDEX(path, '/', -1), '') = '" . $path . "/'")->limit($limit)->offset($offset)->orderBy('created_at', 'desc')->orderBy('id', 'desc')->get()->map(function ($item) {
46+
$files = AtriatechMedia::medium_model()::whereRaw("REPLACE(path, SUBSTRING_INDEX(path, '/', -1), '') = '" . $path . "/'")->limit($limit)->offset($offset)->orderBy('created_at', 'desc')->orderBy('id', 'desc')->get()->map(function ($item) {
4747
$item->visibility = $item->visibility;
4848
$item->size = $item->size;
4949
$item->basename = $item->basename;
@@ -123,7 +123,7 @@ public function deleteItem(Request $request)
123123
if (Storage::exists($item)) {
124124
$this->rrmdir(((!empty(config('atriatech_media.url_prefix'))) ? trim(config('atriatech_media.url_prefix'), '/') . '/' : '') . ltrim(Storage::url($item), '/'));
125125
} else {
126-
$media = Medium::path([['path' => $item]])->first();
126+
$media = AtriatechMedia::medium_model()::path([['path' => $item]])->first();
127127
if (!empty($media)) {
128128
$attributes = $media->getAttributes();
129129
$item_path[] = $attributes['path'];
@@ -153,7 +153,7 @@ public function renameItem(Request $request)
153153
$newName = $request->input('newName');
154154

155155
if (Storage::exists($item)) {
156-
$media = Medium::whereRaw("REPLACE(path, SUBSTRING_INDEX(path, '/', -1), '') = '" . $item . "/'")->get();
156+
$media = AtriatechMedia::medium_model()::whereRaw("REPLACE(path, SUBSTRING_INDEX(path, '/', -1), '') = '" . $item . "/'")->get();
157157
$newPath = substr($item, 0, strpos($item, strrchr(rtrim($item, '/'), '/'))) . "/" . $newName;
158158
foreach ($media as $medium) {
159159
$medium->update([
@@ -175,7 +175,7 @@ public function renameItem(Request $request)
175175
}
176176
Storage::move($item, $newPath);
177177
} else {
178-
$medium = Medium::path([$item])->first();
178+
$medium = AtriatechMedia::medium_model()::path([$item])->first();
179179

180180
$attributes = $medium->getAttributes();
181181
$newPath = str_replace(pathinfo($attributes['path'], PATHINFO_FILENAME), $newName, $attributes['path']);
@@ -243,7 +243,7 @@ private function rrmdir($dir)
243243
if (is_dir($dir . '/' . $object) && !is_link($dir . "/" . $object)) {
244244
$this->rrmdir($dir . '/' . $object);
245245
} else {
246-
Medium::where('path', 'public' . mb_substr(ltrim($dir . '/' . $object, './'), 7))->delete();
246+
AtriatechMedia::medium_model()::where('path', 'public' . mb_substr(ltrim($dir . '/' . $object, './'), 7))->delete();
247247
unlink($dir . '/' . $object);
248248
}
249249
}

src/config/atriatech_media.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,9 @@
6464
* default is url()
6565
*/
6666
'media_url' => '',
67+
68+
/**
69+
* medium model
70+
*/
71+
'medium_model' => Atriatech\Media\Medium::class,
6772
];

0 commit comments

Comments
 (0)