<?php namespace Concore\Foundation\Http\Controllers;

use App, Response;
use Concore\Foundation\Services\MediaService;

class MediaController extends \Illuminate\Routing\Controller {

    public function showImage(MediaService $media_service, $size, $filename) {
        if(setting_or_config('filesystems.default') == 'local') {
            $image = $media_service->getImage($size, $filename);
        }
        else {
            $image = $media_service->getMedia('image', $filename);
        }

        if(!$image) {
            return App::abort(404);
        }

        if(setting_or_config('filesystems.default') == 'local') {
            return $image;
        }

        return redirect($image->getUrl($size, null, true));
    }

    public function show(MediaService $media_service, $type, $filename) {
        $media = $media_service->getMedia($type, $filename);

        if(!$media) {
            return App::abort(404);
        }

        if(setting_or_config('filesystems.default') == 'local') {
            return response($media_service->getFile($media),200)->header('Content-Type', $media->mimetype);
        }

        return redirect($media->getUrl(null, null, true));
    }

    public function showImageDirect(MediaService $media_service, $size, $filename) {
        $image = $media_service->getImage($size, $filename);

        if(!$image) {
            return App::abort(404);
        }

        return $image;
    }

    public function showDirect(MediaService $media_service, $type, $filename) {
        $media = $media_service->getMedia($type, $filename);

        if(!$media) {
            return App::abort(404);
        }

        return response($media_service->getFile($media),200)->header('Content-Type', $media->mimetype);
    }
}