Skip to content

Commit

Permalink
make download controller models dynamic
Browse files Browse the repository at this point in the history
  • Loading branch information
nivv committed Oct 13, 2022
1 parent 0c3414b commit 7c1621c
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/Http/Controllers/Api/Fabriq/DownloadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Ikoncept\Fabriq\Http\Controllers\Api\Fabriq;

use Ikoncept\Fabriq\Fabriq;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Infab\Core\Http\Controllers\Api\ApiController;
Expand All @@ -14,15 +15,18 @@ class DownloadController extends ApiController
{
use ApiControllerTrait;

public const DOWNLOADABLE_TYPES = [
'images' => 'Ikoncept\Fabriq\Models\Image',
'files' => 'Ikoncept\Fabriq\Models\File',
'videos' => 'Ikoncept\Fabriq\Models\Video',
];
protected static function downloadableTypes() : array
{
return [
'images' => Fabriq::getFqnModel('image'),
'files' => Fabriq::getFqnModel('file'),
'videos' => Fabriq::getFqnModel('video') ,
];
}

public function index(Request $request): BinaryFileResponse
{
$type = self::DOWNLOADABLE_TYPES[$request->type];
$type = self::downloadableTypes()[$request->type];
$files = $type::whereIn('id', $request->items)->get();

$zip = new ZipArchive();
Expand Down Expand Up @@ -63,7 +67,7 @@ public function index(Request $request): BinaryFileResponse
*/
public function show(Request $request, int $id)
{
$type = self::DOWNLOADABLE_TYPES[$request->type];
$type = self::downloadableTypes()[$request->type];
$item = $type::where('id', $id)->firstOrFail();
$conversion = '';
$mediaFile = $item->getFirstMedia($request->type);
Expand Down

0 comments on commit 7c1621c

Please sign in to comment.