Skip to content
This repository has been archived by the owner on Aug 20, 2023. It is now read-only.

Commit

Permalink
Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
francoism90 committed Feb 13, 2020
1 parent 9509793 commit 7bcdb63
Show file tree
Hide file tree
Showing 321 changed files with 2,985 additions and 2,685 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
32 changes: 21 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ MediaDB is very much in development and is not yet suitable for production purpo

For the time being please consult the upstream documentation of used packages in `composer.json` for possible other missing dependencies or recommendations.

### Install
## Install

#### nginx
### Nginx

See `doc/nginx` for configuration examples.

#### Laravel
### Laravel

See `doc/supervisor` for configuration examples.

Expand All @@ -52,17 +52,15 @@ php artisan elastic:update-mapping "App\Models\User"

It is advisable to view all configuration files and change them when necessary, especially `.env`, `config/vod.php`, `config/hashids.php` and `config/filesystems.php`.

##### Compile Assets
#### Compile Assets

```bash
cd /path/to/html
npm install
npm run dev
```

### Production

#### Generate VOD key and IV
### Generating VOD key + IV

```bash
dd if=/dev/urandom bs=1 count=32 2> /dev/null | xxd -p -c32
Expand All @@ -74,7 +72,7 @@ VOD_KEY=
VOD_IV=
```

#### Set VOD secure link
### Set VOD secure link

```bash
"$secure_link_expires$arg_id$remote_addr my-secret";
Expand All @@ -84,14 +82,26 @@ VOD_IV=
VOD_SECRET=
```

#### Optimize
## Upgrade

### Elasticsearch

```bash
php artisan elastic:update-index "App\Support\Scout\MediaIndexConfigurator"
php artisan elastic:update-index "App\Support\Scout\UserIndexConfigurator"
php artisan elastic:update-mapping "App\Models\Media"
php artisan elastic:update-mapping "App\Models\User"
```

## Optimize

```bash
composer install --optimize-autoloader --no-dev
npm install
npm run prod
php artisan optimize
```

##### Modules
### Modules

- https://github.com/kaltura/nginx-vod-module#performance-recommendations
- <https://github.com/kaltura/nginx-vod-module#performance-recommendations>
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@
use App\Http\Requests\Media\UpdateRequest;
use App\Http\Resources\MediaResource;
use App\Models\Media;
use App\Support\QueryBuilder\Filters\Media\CollectionFilter;
use App\Support\QueryBuilder\Filters\Media\RandomFilter;
use App\Support\QueryBuilder\Filters\Media\RelatedFilter;
use App\Support\QueryBuilder\Filters\QueryFilter;
use App\Support\QueryBuilder\Filters\TaggedFilter;
use App\Support\QueryBuilder\Filters\RelatedFilter;
use App\Support\QueryBuilder\Sorts\MostViewsSorter;
use App\Support\QueryBuilder\Sorts\PopularMonthSorter;
use App\Support\QueryBuilder\Sorts\PopularWeekSorter;
Expand All @@ -33,19 +30,16 @@ public function index()
$query = QueryBuilder::for(Media::class)
->allowedIncludes(['model', 'tags'])
->allowedSorts([
AllowedSort::custom('popular-month', new PopularMonthSorter()),
AllowedSort::custom('popular-week', new PopularWeekSorter()),
AllowedSort::custom('recent', new RecentSorter()),
AllowedSort::custom('recommended', new RecommendedSorter()),
AllowedSort::custom('trending', new TrendingSorter()),
AllowedSort::custom('views', new MostViewsSorter()),
AllowedSort::custom('recommended', new RecommendedSorter())->defaultDirection('desc'),
AllowedSort::custom('popular-month', new PopularMonthSorter())->defaultDirection('desc'),
AllowedSort::custom('popular-week', new PopularWeekSorter())->defaultDirection('desc'),
AllowedSort::custom('recent', new RecentSorter())->defaultDirection('desc'),
AllowedSort::custom('trending', new TrendingSorter())->defaultDirection('desc'),
AllowedSort::custom('views', new MostViewsSorter())->defaultDirection('desc'),
])
->allowedFilters([
AllowedFilter::custom('collection', new CollectionFilter()),
AllowedFilter::custom('random', new RandomFilter()),
AllowedFilter::custom('related', new RelatedFilter()),
AllowedFilter::custom('query', new QueryFilter()),
AllowedFilter::custom('tags', new TaggedFilter()),
])
->jsonPaginate();

Expand Down Expand Up @@ -136,7 +130,7 @@ public function update(UpdateRequest $request, Media $media)
public function destroy(Media $media)
{
if ($media->delete()) {
return response()->json('success', 200);
return new MediaResource($media);
}

return response()->json('error', 500);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
use App\Http\Controllers\Controller;
use App\Http\Resources\TagResource;
use App\Models\Tag;
use App\Support\QueryBuilder\Filters\QueryFilter;
use App\Support\QueryBuilder\Filters\Tag\MediaFilter;
use App\Support\QueryBuilder\Filters\SimpleQueryFilter;
use Spatie\QueryBuilder\AllowedFilter;
use Spatie\QueryBuilder\QueryBuilder;

Expand All @@ -17,10 +16,9 @@ public function index()
$query = QueryBuilder::for(Tag::class)
->defaultSort('order_column')
->allowedFilters([
AllowedFilter::custom('feed', new MediaFilter()),
AllowedFilter::custom('query', new QueryFilter()),
AllowedFilter::custom('query', new SimpleQueryFilter()),
])
->get();
->paginate(Tag::count());

return TagResource::collection($query);
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public function toArray($request)
{
return [
'id' => $this->getRouteKey(),
'slug' => $this->slug,
'name' => $this->name,
'type' => $this->type,
'views' => $this->views,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public function toArray($request)
'media' => $this->media()->count(),
'views' => $this->views,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'relationships' => [
'media' => MediaResource::collection($this->whenLoaded('media')),
],
Expand Down
File renamed without changes.
File renamed without changes.
14 changes: 12 additions & 2 deletions src/app/Models/Media.php → app/Models/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace App\Models;

use App\Support\Scout\MediaIndexConfigurator;
use App\Support\Scout\Rules\MultiMatchRule;
use App\Traits\Hashidable;
use App\Traits\Randomable;
use App\Traits\Relateable;
use App\Traits\Securable;
use App\Traits\Streamable;
use App\Traits\Taggable;
Expand All @@ -29,7 +29,6 @@ class Media extends BaseMedia implements ViewableContract
use HasStatuses;
use HasTags;
use Randomable;
use Relateable;
use Searchable;
use Sluggable;
use Securable;
Expand All @@ -54,16 +53,27 @@ class Media extends BaseMedia implements ViewableContract
*/
protected $indexConfigurator = MediaIndexConfigurator::class;

/**
* @var array
*/
protected $searchRules = [
MultiMatchRule::class,
];

/**
* @var array
*/
protected $mapping = [
'properties' => [
'name' => [
'type' => 'text',
'analyzer' => 'autocomplete',
'search_analyzer' => 'autocomplete_search',
],
'description' => [
'type' => 'text',
'analyzer' => 'autocomplete',
'search_analyzer' => 'autocomplete_search',
],
],
];
Expand Down
61 changes: 61 additions & 0 deletions app/Models/Tag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace App\Models;

use App\Traits\Hashidable;
use CyrildeWit\EloquentViewable\Contracts\Viewable as ViewableContract;
use CyrildeWit\EloquentViewable\Viewable;
use Spatie\Tags\Tag as TagModel;

class Tag extends TagModel implements ViewableContract
{
use Hashidable;
use Viewable;

/**
* @return morphedByMany
*/
public function collections()
{
return $this->morphedByMany(Collection::class, 'taggable', 'taggables');
}

/**
* @return morphedByMany
*/
public function media()
{
return $this->morphedByMany(Media::class, 'taggable', 'taggables');
}

/**
* @return int
*/
public function getViewsAttribute(): int
{
return views($this)->unique()->count();
}

/**
* @param Builder $query
* @param array $tags
* @param string $type
* @param string $locale
*
* @return Builder
*/
public function scopeWithSlugTranslated($query, array $tags = [], string $type = null, string $locale = null)
{
$locale = $locale ?? app()->getLocale();

return $query
->when($type, function ($query, $type) {
return $query->where('type', $type);
})
->where(function ($query) use ($tags, $locale) {
foreach ($tags as $tag) {
$query->orWhereJsonContains("slug->{$locale}", $tag);
}
});
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Providers;

use App\Models\Collection;
use App\Models\Media;
use App\Models\Tag;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
Expand All @@ -25,6 +26,10 @@ public function boot()
{
parent::boot();

Route::bind('collection', function ($value, $route) {
return Collection::getModelByKey($value);
});

Route::bind('media', function ($value, $route) {
return Media::getModelByKey($value);
});
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 7bcdb63

Please sign in to comment.