AzhanIdris's avatar

Saving Audio, Images and Video into Google Drive and Fetch the URL into the database

I try to save image and audio into Google Drive. Here is my create.blade.php

<div class="row clearfix">
        <div class="col-lg-12">
            <div class="card">
                <div class="body">
                    <form action="{{route('music.store')}}" method="POST">
                    @csrf
                        <div class="row clearfix">
                            <div class="col-lg-6 col-md-6 col-sm-12">
                                <div class="form-group">
                                    <label for="formFile" class="form-label">Cover Image:</label>
                                    <input type="file" name="photo" id="dropify-event" data-default-file= "{{ asset('imgs/no_video.png') }}" onchange="previewFile()">
                                    <div id="previewPhoto"></div>
                                    <input type="hidden" id="photoHiddenInput" name="photo" value="">
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="title">Title:</label>
                            <input type="text" class="form-control"  id="title" name="title" placeholder="Song Title" required >
                        </div>
                        <div class="form-group">
                            <label for="title">After Like:</label>
                            <input type="text" class="form-control"  id="artist" name="artist" placeholder="Artist" required >
                        </div>
                        <div class="form-group">
                            <label>Upload Song: </label>
                            <div class="input-group mb-3">
                                <div class="input-group-prepend">
                                    <span class="input-group-text" style="padding: 0.175rem 0.75rem;">Upload</span>
                                </div>
                                <div class="custom-file">
                                    <input type="file" name="audio" accept="audio/*" class="custom-file-input" id="inputGroupFile01">
                                    <label class="custom-file-label" for="inputGroupFile01">Choose File</label>
                                    <div id="previewAudio"></div>
                                    <input type="hidden" id="audioHiddenInput" name="audio" value="">
                                </div>
                            </div>
                        </div>
                        <br>
                        <button type="submit" class="btn btn-outline-primary">Save</button>
                    </form>
                </div>
            </div>
        </div>
    </div>

Here I try to save the audio in the Google Drive and get the URL to save into the database

public function store($request) {
        try{
            Music::create([
                'photo'         => (!empty($request['photo']))?$this->convert($request['photo']):null,
                'title'         => $request['title'],
                'artist'        => $request['artist'],
                'audio'         => (!empty($request['audio']))?$this->convertAudio($request['audio']):null
            ]);

        } catch(\Exception $e){
            throw new \Exception('Failed to save Music. '.$e->getMessage());
        }
    }

As for image and audio, I use another method to store both files into Google Drive and retrieve the URL to be stored into the database.

public function convert($request) : string {
        try {
            $photo = $request;  // your base64 encoded
            $photo = str_replace('data:image/png;base64,', '', $photo);
            $photo = str_replace('data:image/jpeg;base64,', '', $photo);
            $photo = str_replace('data:image/jpg;base64,', '', $photo);
            $photo = str_replace(' ', '+', $photo);
            $filename = time() . Str::random(10) . '.' . 'png';
            $file = base64_decode($photo);
            $path = '/' . $filename;
            Storage::disk('google')->put($path, $file, 'public');
            $url = Storage::disk('google')->url($path);

            return $url;
        } catch (\Exception $e) {
            throw new \Exception('Failed to save Photo. '.$e->getMessage());
        }
    }

    public function convertAudio($request) : string {
        try {
            $audio = $request;  // your base64 encoded
            $filename = time() . Str::random(10) . '.' . 'mp3';
            $file = base64_decode($audio);
            $path = '/' . $filename;
            Storage::disk('google')->putFile($path, $file, 'public');
            $url = Storage::disk('google')->url($path);

            return $url;
        } catch (\Exception $e) {
            throw new \Exception('Failed to save Audio. '.$e->getMessage());
        }
    }

My problem is first; when I try to upload the audio only it says null and second; when I try to upload both at the same time, both files didnt appear in the Google Drive

0 likes
0 replies

Please or to participate in this conversation.