<?php

namespace App\Http\Controllers\AWS;


use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Response;


class AWSImageUploadController extends Controller

{

    public function imageUpload()
    {
        return view('pages.aws_image_upload');
    }


    public function imageUploadPost(Request $request)
    {
        $request->validate([
            'image' => 'required|image:allow_svg|mimes:jpeg,png,jpg,gif,svg|max:2048',
        ]);

        $imageName = time().'.'.$request->image->extension();

        $request->image->storeAs('images', $imageName);

        //checking if storage disk exist
        if (Storage::disk('documents')) {
            $check1 = "The disk documents exists.";
        } else {
            $check1 = "The disk documents does not exist.";
        }

        // $diskNames = Storage::diskNames();

        // if (count($diskNames) > 0) {
        //     $check2 = "The following disks are configured: " . implode(', ', $diskNames);
        // } else {
        //     $check2 = "No storage disks are configured.";
        // }


        if (Storage::disk('public')) {
            $check2 = "The disk public exists.";
        } else {
            $check2 = "The disk public does not exist.";
        }

        if (Storage::disk('local')) {
            $check3 = "The disk local exists.";
        } else {
            $check3 = "The disk local does not exist.";
        }


        return redirect()->back()
            ->with('success','You have successfully upload image.')
            ->with('image',$imageName)
            ->with('check1', $check1)
            ->with('check2', $check2)
            ->with('check3', $check3);
    }


    public function displayImage($fileName)
    {
        // dd($fileName);

        //1.
        // $path = 'images/'.$fileName;
        // $url = Storage::url($path);
        // $url = Storage::temporaryUrl('file.jpg', now()->addMinutes(5));
        // return $url;


        //2
        // $path = 'images/'.$fileName;
        // // if(Storage::disk('s3')->exists($path)){
        //     return Storage::disk('s3')->get($path);
        // // }
        // return null;


        //original
        $path = 'images/'.$fileName;
        $file = Storage::get($path);
        $type = Storage::mimeType($path);

        $response = Response::make($file, 200);
        $response->header("Content-Type", $type);
        return $response;

        // $path = 'images/'.$fileName;
        // return Storage::temporaryUrl($path, now()->addMinutes(5));
    }

}
