BananaPB
BananaPB

Reputation: 41

Get client uploaded file and send it to Backblaze api?

I'm building an API with Symfony and using Backblaze as a cloud storage solution. I want to allow my users to upload images that they will be allowed to use later. All my users have a specific bucket for them. I already successfully made a route that upload TCPDF generated pdf. But I struggle manipulating the image file from the request.

Here's my route :

#[Route('/http/upload-file', name:'testHttp', methods:['POST'])]
    public function testHttp(HttpService $service,Request $request)
    {
        $user = $this->getUser();
        $path = $request->query->get('path');
        $file = $request->files->get('image');
        $mimeTypes = new MimeTypes();
        $mimeType = $mimeTypes->guessMimeType($file);

        $extension = $file->getClientOriginalExtension();
        $filename = $user->getUsername()."_".date("Y-m-d_His.").$extension;
        
        $upload = $service->uploadFile($user,$file,$path,$filename,$mimeType);
        return new JsonResponse($upload, 200, [], true);
    }

And here is my uploadFile() function :

public function uploadFile(User $user,$file,$path,$filename,$contentType)
    {   
        $upload = $this->getUploadUrl($user);
        if(is_string($file)){
            // $fileSize = strlen($file);
            // $sha1 = sha1($file);
        }else{
            $fileSize = $file->getSize();
            $sha1 = sha1($file);
        }

        $client = HttpClient::create([
            "headers"=>[
                'Authorization' => $upload["authorizationToken"],
                'X-Bz-File-Name' => $path.$filename,
                'X-Bz-Content-Sha1'=> $sha1,
                'Content-Length' => $fileSize,
                'Content-Type' => $contentType
            ]
        ]);
        
        $response = $client->request('POST',$upload["uploadUrl"],[
            'body'=>$file
        ]);

        $statusCode = $response->getStatusCode();
        $content = $response->toArray(false);
        if($statusCode != 200){
            $response = [
                "status"=>$statusCode,
                "message"=>$content["message"]
            ];
            return $response;
        }

        $response = [
            "status"=>$statusCode,
            "fileId"=>$content["fileId"]
        ];
        return $response;
    }

uploadFile() works well with pdf files, but when I use my route to send img files I get an error : stream_get_meta_data(): Argument #1 ($stream) must be of type resource, Symfony\\Component\\HttpFoundation\\File\\UploadedFile given

It looks like I'm struggling to get the file content sha1 and the filesize correctly. After looking online, I realised that $file seems to be an instance of UploadedFile object, so it just return the temporary path of the file. But I don't know how to access the file content then. What am I doing wrong ?

Note that as my API didn't have an UI yet, I'm using Postman to test my routes. So I set the body to "form-data" to send the image file. It's working as the route successfully get the original file name and mime type.

Upvotes: 0

Views: 144

Answers (0)

Related Questions