KHAN's avatar
Level 4

Laravel 5 / Amazon S3 - Handling File/Image Uploads

Hi,

I have an event resource. An admin can upload multiple files and also upload a separate image. This image will be used as a banner image to the event and a resized version for the event list view page. A user can download all these attachments on the event overview page. At the moment im using Amazon S3 to store everything

Would cloudfront be a good solution to use here, how can i use it with amazon s3? And could it be used to caching these images?

    public function download($eventID, $attachmentPath)
    {
        $event = EventModel::find($eventID);
        $file = $event->attachments->where('path', $attachmentPath)->first();

        $storedFile = Storage::get($event->getDirectoryPath() . $file->path);

        return response($storedFile, 200)
            ->header('Cache-Control', 'public')
            ->header('Content-Description', 'File Transfer')
            ->header('Content-Disposition', 'attachment; filename=' . $file->getFullName())
            ->header('Content-Type', $file->type)
            ->header('Content-Transfer-Encoding', 'binary');

    }

    public function show($eventID, $attachmentPath, $width, $height)
    {
        $event = EventModel::find($eventID);
        $file = $event->attachments->where('path', $attachmentPath)->first();

        $storedImage = Storage::get($event->getDirectoryPath() . $file->path);

        return Image::make($storedImage)->fit($width, $height)->response();

    }
    Route::get('event/{id}/overview/download/{attachment}', ['as' => 'event-attachment-download', 'uses' => 'AttachmentController@download']);
    Route::get('event/{id}/overview/image/{attachment}/{width}/{height}', ['as' => 'event-attachment-image', 'uses' => 'AttachmentController@show']);
//Bigger Banner
<img src="{{ route('event-attachment-image', [$event->id, $event->getHero()->path, 950, 400]) }}"
//Smaller Banner

<img src="{{ route('event-attachment-image', [$event->id, $event->getHero()->path, 125, 125]) }}"

Im conscious this is not an efficient way. Once i recieve the image content from amazon s3 im then resizing the images everytime using intervention. Im making a lot of calls to amazon s3 for the same thing.

0 likes
0 replies

Please or to participate in this conversation.