Skip to content

Commit

Permalink
Merge pull request #59 from atriatech/v2
Browse files Browse the repository at this point in the history
added a config
  • Loading branch information
RezDev94 authored Jun 30, 2021
2 parents 71095ab + 235b6f4 commit bb23629
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 8 deletions.
4 changes: 3 additions & 1 deletion src/AtriatechMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace Atriatech\Media;

use Atriatech\Media\Facades\AtriatechMedia as AtriatechMediaFacade;

trait AtriatechMedia
{
public function media()
{
return $this->morphToMany(Medium::class, 'media_item', 'medium_items', 'media_item_id', 'medium_id')->withPivot('name');
return $this->morphToMany(AtriatechMediaFacade::medium_model(), 'media_item', 'medium_items', 'media_item_id', 'medium_id')->withPivot('name');
}

public function addMedia($paths)
Expand Down
1 change: 1 addition & 0 deletions src/Facades/AtriatechMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

/**
* @method static bool upload($file, $path = '')
* @method static Medium medium_model()
*/

class AtriatechMedia extends Facade {
Expand Down
8 changes: 6 additions & 2 deletions src/Helpers/MediumHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Atriatech\Media\Helpers;

use Atriatech\Media\Medium;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;

Expand All @@ -25,7 +24,7 @@ public static function upload($file, $path = '')
$counter++;
}
$file->storeAs($path, $fileName);
$media = Medium::create([
$media = self::medium_model()::create([
'path' => $path . $fileName,
'mime_type' => $file->getMimeType(),
]);
Expand Down Expand Up @@ -126,4 +125,9 @@ public static function upload($file, $path = '')
'updated_at' => $media->updated_at,
];
}

public static function medium_model()
{
return config('atriatech_media.medium_model') ?? Atriatech\Media\Medium::class;
}
}
10 changes: 5 additions & 5 deletions src/MediumController.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function getFiles(Request $request)
}
$dirs = $this->getDirectories($path);

$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) {
$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) {
$item->visibility = $item->visibility;
$item->size = $item->size;
$item->basename = $item->basename;
Expand Down Expand Up @@ -123,7 +123,7 @@ public function deleteItem(Request $request)
if (Storage::exists($item)) {
$this->rrmdir(((!empty(config('atriatech_media.url_prefix'))) ? trim(config('atriatech_media.url_prefix'), '/') . '/' : '') . ltrim(Storage::url($item), '/'));
} else {
$media = Medium::path([['path' => $item]])->first();
$media = AtriatechMedia::medium_model()::path([['path' => $item]])->first();
if (!empty($media)) {
$attributes = $media->getAttributes();
$item_path[] = $attributes['path'];
Expand Down Expand Up @@ -153,7 +153,7 @@ public function renameItem(Request $request)
$newName = $request->input('newName');

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

$attributes = $medium->getAttributes();
$newPath = str_replace(pathinfo($attributes['path'], PATHINFO_FILENAME), $newName, $attributes['path']);
Expand Down Expand Up @@ -243,7 +243,7 @@ private function rrmdir($dir)
if (is_dir($dir . '/' . $object) && !is_link($dir . "/" . $object)) {
$this->rrmdir($dir . '/' . $object);
} else {
Medium::where('path', 'public' . mb_substr(ltrim($dir . '/' . $object, './'), 7))->delete();
AtriatechMedia::medium_model()::where('path', 'public' . mb_substr(ltrim($dir . '/' . $object, './'), 7))->delete();
unlink($dir . '/' . $object);
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/config/atriatech_media.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,9 @@
* default is url()
*/
'media_url' => '',

/**
* medium model
*/
'medium_model' => Atriatech\Media\Medium::class,
];

0 comments on commit bb23629

Please sign in to comment.