PHP 8.2.30
Preview: MediaController.php Size: 4.47 KB
/home/byroehnu/cnggold.com.ng/app/Http/Controllers/Admin/MediaController.php

<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Media;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Inertia\Inertia;

class MediaController extends Controller
{

    public function __construct()
    {
        // for demo mood
        $this->middleware('demo', ['only' => ['destroy']]);
    }

    public function index()
    {
        return Inertia::render('Media/Index');
    }

    /**
     * Display a listing of the resource.
     */
    public function getMediaData(Request $request): LengthAwarePaginator
    {
        $date = $request->filter['date'];
        $type = $request->filter['type'];

        $query = Media::query();
        if ($request->search) {
            $query->where('title', 'LIKE', '%' . $request->search . '%');
        }
        if ($date) {
            $query->where(DB::raw('DATE_FORMAT(created_at, "%b %Y")'), $date);
        }
        if ($type) {
            switch ($type) {
                case 'images':
                    $query->where('type', 'LIKE', 'image/%');
                    break;
                case 'video':
                    $query->where('type', 'LIKE', 'video/%');
                    break;
                case 'audio':
                    $query->where('type', 'LIKE', 'audio/%');
                    break;
                case 'pdf':
                    $query->where('type', 'application/pdf');
                    break;
                case 'text':
                    $query->where('type', 'LIKE', 'text/%');
                    break;
                case 'code':
                    $query->where('type', 'application/javascript')
                        ->orWhere('type', 'application/json')
                        ->orWhere('type', 'application/xml')
                        ->orWhere('type', 'text/html')
                        ->orWhere('type', 'text/css');
                    break;
                case 'zip':
                    $query->where('type', 'application/zip');
                    break;
                case 'spreadsheet':
                    $query->where('type', 'application/vnd.ms-excel')
                        ->orWhere('type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
                        ->orWhere('type', 'application/vnd.oasis.opendocument.spreadsheet')
                        ->orWhere('type', 'application/vnd.ms-excel.sheet.macroenabled.12')
                        ->orWhere('type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.template');
                    break;
            }
        }

        return $query->latest()->paginate(72);
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $file = $request->file('file');

        $fileName = $file->getClientOriginalName();
        $fileSize = $file->getSize();
        $fileType = $file->getMimeType();
        $imageDimensions = null;
        // Check if the file is an image but not an SVG
        if (str_starts_with($fileType, 'image') && $fileType !== 'image/svg+xml') {
            // Get image dimensions
            $dimensions = getimagesize($file->getPathname());
            if ($dimensions) {
                $imageDimensions = $dimensions[0] . ' x ' . $dimensions[1] . ' pixels';
            }
        }
        $path = $file->store('media');
        $media = Media::create([
            'title' => $fileName,
            'user_id' => Auth::id(),
            'media_url' => $path,
            'type' => $fileType,
            'dimensions' => $imageDimensions,
            'size' => $fileSize,
            'driver' => config('filesystems.default'),
        ]);

        return $media;
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Media $media): bool
    {
        $media->delete();

        return true;
    }

    /**
     * Get filterede data.
     *
     * @param  Media  $media
     */
    public function getFilteredMonthYear(): mixed
    {
        $dates = Cache::remember('filtered_month_year', now()->addDays(15), function () {
            return DB::table(DB::raw('(SELECT DISTINCT DATE_FORMAT(created_at, "%b %Y") as month_year FROM media) as sub'))
                ->orderBy('sub.month_year', 'desc')
                ->pluck('month_year')
                ->toArray();
        });

        return $dates;
    }
}

Directory Contents

Dirs: 0 × Files: 41

Name Size Perms Modified Actions
1.15 KB lrw-r--r-- 2025-05-04 10:56:26
Edit Download
3.29 KB lrw-r--r-- 2025-07-19 07:29:54
Edit Download
3.88 KB lrw-r--r-- 2025-05-22 03:36:31
Edit Download
5.76 KB lrw-r--r-- 2025-07-19 07:29:54
Edit Download
3.97 KB lrw-r--r-- 2025-05-01 09:00:37
Edit Download
1.95 KB lrw-r--r-- 2025-05-01 09:00:37
Edit Download
1.91 KB lrw-r--r-- 2025-05-01 09:00:37
Edit Download
3.00 KB lrw-r--r-- 2025-05-08 11:48:26
Edit Download
2.07 KB lrw-r--r-- 2025-05-01 09:00:37
Edit Download
1.09 KB lrw-r--r-- 2025-05-01 09:00:37
Edit Download
411 B lrw-r--r-- 2025-04-08 04:12:56
Edit Download
1.66 KB lrw-r--r-- 2025-05-05 10:37:43
Edit Download
2.75 KB lrw-r--r-- 2025-05-01 09:00:37
Edit Download
3.26 KB lrw-r--r-- 2025-05-15 12:02:59
Edit Download
4.47 KB lrw-r--r-- 2025-05-01 09:00:37
Edit Download
1.89 KB lrw-r--r-- 2025-05-01 09:00:37
Edit Download
4.12 KB lrw-r--r-- 2025-05-08 11:48:26
Edit Download
5.74 KB lrw-r--r-- 2025-07-19 07:29:54
Edit Download
169 B lrw-r--r-- 2025-04-19 04:22:19
Edit Download
1.01 KB lrw-r--r-- 2025-04-19 04:22:19
Edit Download
3.83 KB lrw-r--r-- 2025-05-22 03:36:31
Edit Download
5.83 KB lrw-r--r-- 2025-07-19 07:29:54
Edit Download
5.19 KB lrw-r--r-- 2025-05-01 09:00:37
Edit Download
3.20 KB lrw-r--r-- 2025-05-01 09:00:37
Edit Download
3.59 KB lrw-r--r-- 2025-05-22 13:45:40
Edit Download
3.65 KB lrw-r--r-- 2025-07-19 07:29:54
Edit Download
2.54 KB lrw-r--r-- 2025-05-08 11:48:26
Edit Download
1.18 KB lrw-r--r-- 2025-05-08 11:48:26
Edit Download
2.79 KB lrw-r--r-- 2025-05-01 09:00:37
Edit Download
459 B lrw-r--r-- 2025-04-19 04:22:18
Edit Download
3.74 KB lrw-r--r-- 2025-05-22 03:36:31
Edit Download
5.86 KB lrw-r--r-- 2025-07-19 07:29:54
Edit Download
5.50 KB lrw-r--r-- 2025-05-01 09:00:37
Edit Download
1.78 KB lrw-r--r-- 2025-05-01 09:00:37
Edit Download
1.77 KB lrw-r--r-- 2025-05-01 09:00:37
Edit Download
5.11 KB lrw-r--r-- 2025-07-19 07:29:54
Edit Download
2.98 KB lrw-r--r-- 2025-04-19 04:22:19
Edit Download
847 B lrw-r--r-- 2025-04-08 04:12:56
Edit Download
2.32 KB lrw-r--r-- 2025-05-01 09:00:37
Edit Download
1.77 KB lrw-r--r-- 2025-05-01 09:00:37
Edit Download
2.63 KB lrw-r--r-- 2025-05-01 09:00:37
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).