Ad

Upload A Blob Avatar In Octobercms

- 1 answer

I'm using October and developping a frontend app in Vue.js (Quasar framework) with JWT Auth. In Vue, I read a .jpg with a FileReader. As a result, I have a blob that I send to October with profile parameters with a POST axios request to update an existing user profile.

In October I have a plugin API with the following code in route.php. It seems I retrieve the blob, but when I try to store it as a file, I get inconsistent images stored or October error code like : enter image description here

Can I send raw image and how could I store them in October files ? Thanks

    Route::post('Change-Profile', function (Request $request) {
       $user = new User;
       $user->street_addr = $request->street_addr;

       $file = new System\Models\File;
       $file->data = Input::file( $request->avatar );
       $file->is_public = false;
       $file->save();

       $user->avatar = $file;
       $user->save();
Ad

Answer

as I spent too much time on this trivial problem, I'd like to share my final solution made with the help of Hardik. Thanks again !

        $bIsAvatar = true;
        $user = new User;
        $user_email = $request->email;

        $rules = [
            'email' => 'required|email|min:6',
            'username' => 'required|string|min:5',
            'zip' => 'string|min:1|max:5',
            'avatar' => 'image|mimes:jpeg,png,jpg,gif|max:10000'
        ];

        $validation = Validator::make(Input::all(), $rules);
        if ($validation->fails()) {
            return response()->json([
                'Error' => 'User : ' . $user_email ,
                'Champs invalides : ' . $validation->messages()
            ], Response::HTTP_BAD_REQUEST);
        }

        $user = User::findByEmail( $user_email );
        if (!$user || $user->isBanned() || !$user->is_activated) {
            return response()->json(
                ['Error' => 'User non trouvé ou non autorisé : ' . $user_email ], Response::HTTP_NOT_FOUND
            );
        }

        $user->name = $request->name;
        $user->surname = $request->surname;
        $user->email = $request->email;
        $user->street_addr = $request->street_addr;
        $user->city = $request->city;
        $user->zip = $request->zip;
        $user->phone = $request->phone;
        $user->mobile = $request->mobile;
        $user->about_me = $request->about_me;

        if ( $request->avatar == null || empty($request->avatar) ) $bIsAvatar = false;
        else {
            $user->avatar = Input::file('avatar');
            $user->avatar->is_public = false;
        }
        $user->save();
        if ($bIsAvatar) {
            $avatar_path = $user->avatar->getPath();
            //$resultat = "User " . $user->email . " : profil mis à jour avec l'avatar=" . $avatar_path;
            $resultat = "Profil mis à jour avec avatar" . $user;
        } else
            //$resultat = "User : " . $user->email . " : profil mis à jour sans avatar";
            $resultat = "Profil mis à jour sans avatar" . $user;

        return response()->json($resultat);
    });
Ad
source: stackoverflow.com
Ad