Как я могу загрузить свои аудиофайлы из своего api с помощью postman, не получив сообщения «Файл не найден по пути»?

#api #postman #laravel-8 #php-8

Вопрос:

Каждый раз, когда я пытаюсь загрузить через почтальона, я получаю это сообщение об ошибке «сообщение»: «Файл не найден по пути: opt/lampp/htdocs/SSsound-laravel/storage/public/audio/clever_j_ft_fat_azy_manzi_wanani_official_video_mp3_72364_1627109365.mp3»

Вот мой filesystem.php

 <?php

return [

    /*
    |--------------------------------------------------------------------------
    | Default Filesystem Disk
    |--------------------------------------------------------------------------
    |
    | Here you may specify the default filesystem disk that should be used
    | by the framework. The "local" disk, as well as a variety of cloud
    | based disks are available to your application. Just store away!
    |
    */

    'default' => env('FILESYSTEM_DRIVER', 'local'),

    /*
    |--------------------------------------------------------------------------
    | Filesystem Disks
    |--------------------------------------------------------------------------
    |
    | Here you may configure as many filesystem "disks" as you wish, and you
    | may even configure multiple disks of the same driver. Defaults have
    | been setup for each driver as an example of the required options.
    |
    | Supported Drivers: "local", "ftp", "sftp", "s3"
    |
    */

    'disks' => [

        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
        ],

        'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'url' => env('APP_URL').'/storage',
            'visibility' => 'public',
        ],

        's3' => [
            'driver' => 's3',
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'region' => env('AWS_DEFAULT_REGION'),
            'bucket' => env('AWS_BUCKET'),
            'url' => env('AWS_URL'),
            'endpoint' => env('AWS_ENDPOINT'),
        ],

    ],

    /*
    |--------------------------------------------------------------------------
    | Symbolic Links
    |--------------------------------------------------------------------------
    |
    | Here you may configure the symbolic links that will be created when the
    | `storage:link` Artisan command is executed. The array keys should be
    | the locations of the links and the values should be their targets.
    |
    */

    'links' => [
        public_path('storage') => storage_path('app/public'),
    ],

];


 

Это мой метод загрузки

 public function createAudio(Request $req) {
        
        $user = Auth::user();
        $input = $req->all();
        $validated = $this->validateAudioCreationInput($input);
        if ($validated->passes()) {
            // save to db
            try {
              $audioModel = new Audio();

          if($req->file()) {
            //   get filename plus ext
            $fileNameExt = $req->file->getClientOriginalName();

            // get file name without ext
            $fileName = pathinfo($fileNameExt, PATHINFO_FILENAME);

            // get just ext
            $ext = $req->file('file')->getClientOriginalExtension();

            // file name to store
            $fileNameToStore = $fileName.'_'.time().'.'.$ext;

            //   upload file
              
              $filePath = $req->file('file')->storeAs('audio', $fileNameToStore, 'public');

              $audioModel->audio_name = $fileNameToStore;
              $audioModel->file_path = $filePath;
              $audioModel->artist = $req->artist;
              $audioModel->album = $req->album;
              $audioModel->genre = $req->genre;
              $audioModel->owner_user_id = Auth::user()->id;
              $audioModel->save();
          }
            }  catch (Exception $e) {
                $error = [
                    'status' => 'Error',
                    'status_code' => 400,
                    'message' => $e->getMessage()
                ];
                return response()->json($error, 400);
            }

            $success['audio'] = [
                'id'        => $audioModel->id,
                'audio_name' => $audioModel->audio_name,
                'artist' => $audioModel->artist,
                'album' => $audioModel->album,
                'genre' => $audioModel->genre,
                'likes' => $audioModel->likes ? $audioModel->likes : 0,
                'downloads' => $audioModel->downloads ? $audioModel->downloads : 0,
                'duration' => $audioModel->duration ? $audioModel->duration : 0,
                'owner_user_id'     => $audioModel->owner_user_id,
                'created_at'     => $audioModel->created_at,
                'updated_at'     => $audioModel->updated_at,
                'createdBy'  =>  $user->only(['first_name']),
                'role'  =>  $user->only(['account_type']),
                'file_path' => $audioModel->file_path,
            ];
            return response()->json($success, 201);
        } else {

            $error = [
                'error_message' => $validated->messages()
            ];
            return response()->json($error, 400);
        }
      }
 

Here is what i attempted

 public function downloadAudio($id)
    {
        $audio = Audio::where('id', $id)->firstOrFail();
        $filePath = $audio->file_path;
        $name = storage_path($audio->audio_name);
        $headers = array(
            'Content-type:audio/mp3'
        );
        return Storage::download($name);


    }
 

Большое вам спасибо за любую помощь заранее