dustin10 / VichUploaderBundle

A simple Symfony bundle to ease file uploads with ORM entities and ODM documents.
MIT License
1.83k stars 519 forks source link

Issue with VichUploader: unsupported MIME type "image/jpeg" #1435

Closed Mirieri closed 6 months ago

Mirieri commented 6 months ago

I am currently facing an issue with the VichUploader bundle in my Symfony project, specifically when attempting to upload an image using the multipart/form-data content type.

Here are the details of the error I encountered:

{
    "@context": "/api/contexts/Error",
    "@type": "hydra:Error",
    "hydra:title": "An error occurred",
    "hydra:description": "The content-type \"image/jpeg\" is not supported. Supported MIME types are \"application/ld+json\", \"application/json\", \"text/html\".",
    "trace": [
        // Error trace details
    ]
}

It seems that the content type "image/jpeg" is not recognized or supported, and the application is expecting one of the following MIME types: "application/ld+json", "application/json", "text/html".

I have verified my VichUploader configuration, and it appears to be correctly set up. Here is a snippet of my configuration:

vich_uploader:
    db_driver: orm
    mappings:
        imageFile:
            uri_prefix: /images/mob_image
            upload_destination: '%kernel.project_dir%/public/images/mob_image'
            namer: Vich\UploaderBundle\Naming\SmartUniqueNamer

My Entity model is

<?php

namespace App\Entity;

use ApiPlatform\Metadata\ApiResource;
use App\Repository\MobImagesRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use ApiPlatform\Core\Annotation\ApiProperty;

/**
 * @ORM\Table(name="mob_images")
 * @ORM\Entity(repositoryClass=MobImagesRepository::class)
 * @Vich\Uploadable
 */
#[ApiResource]
class MobImages
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(name="source_id", type="integer")
     */
    private $sourceId;

    /**
     * @var UploadedFile|null
     * @Vich\UploadableField(mapping="imageFile", fileNameProperty="imageName")
     */
    private $imageFile;

    /**
     * @var string|null
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $imageName;

    /**
     * @var \DateTimeImmutable|null
     * @ORM\Column(type="datetime_immutable", nullable=true)
     */
    private $createdAt;

    /**
     * @ORM\Column(name="description", type="string", length=255)
     */
    private $description;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getSourceId(): ?int
    {
        return $this->sourceId;
    }

    public function setSourceId(int $sourceId): self
    {
        $this->sourceId = $sourceId;

        return $this;
    }

    public function getImageName(): ?string
    {
        return $this->imageName;
    }

    public function setImageName(?string $imageName): self
    {
        $this->imageName = $imageName;

        return $this;
    }

    public function setImageFile(?UploadedFile $imageFile = null): void
    {
        $this->imageFile = $imageFile;

        if ($imageFile) {
            $this->createdAt = new \DateTimeImmutable();
        }
    }

    public function getImageFile(): ?UploadedFile
    {
        return $this->imageFile;
    }

    public function getCreatedAt(): ?\DateTimeImmutable
    {
        return $this->createdAt;
    }

    public function setDescription(?string $description): self
    {
        $this->description = $description;

        return $this;
    }

    public function getDescription(): ?string
    {
        return $this->description;
    }

}

under config /config/api_platform/

App\Entity\MobImages:
  collectionOperations:
    get:
      method: get
      path: /images
      filters: [ 'mobimages.search_filter', 'property_filter' ]
      normalization_context:
        groups: [ 'mobimages:collection:get' ]
      openapi_context:
        description:
          ''
        parameters:
          - name: page
            in: query
            description: 'The collection page number'
            required: false
            deprecated: false
            allowEmptyValue: false
            schema:
              type: integer
              default: 1
            style: form
            explode: false
            allowReserved: false
          - name: pagination
            in: query
            description: 'Enable or disable pagination'
            required: false
            deprecated: false
            allowEmptyValue: false
            schema:
              type: boolean
            style: form
            explode: false
            allowReserved: false
    post:
      method: post
      path: /images
      denormalization_context:
        groups: [ 'mobimages:collection:post' ]
      openapi_context:
        description:
          '<h3>Creates a Mob Images resource</h3>'
  itemOperations:
    get:
      method: get
      path: /images/{id}
      normalization_context:
        groups: [ 'mobimages:item:get' ]
    put:
      method: put
      path: /images/{id}
      denormalization_context:
        groups: [ 'mobimages:item:put' ]
      openapi_context:
        description:
          '<h3>Replaces the mobile images resource specified by the `id` parameter</h3><p>The following properties are **required** and need to be provided in the request body:
           </p>`country`<p>`farmerIsHhHead`</p>`isActive`<p>`isDeleted`</p>`name`<p>All other properties are **optional**.</p>'
    patch:
      method: patch
      path: /images/{id}
      denormalization_context:
        groups: [ 'mobimages:item:patch' ]
      openapi_context:
        description:
          '<h3>Updates the mobile images resource specified by the `id` parameter</h3><p>The following properties are **required** and need to be provided in the request body:
          </p>`farmerIsHhHead`<p>All other properties are **optional**.</p>'
garak commented 6 months ago

Is your trace pointing to a VichUploaderBundle location? If so, please provide all the relevant information

Mirieri commented 6 months ago

Yes it is

vich_uploader:
    db_driver: orm
    mappings:
        imageFile:
            uri_prefix: /images/mob_image
            upload_destination: '%kernel.project_dir%/public/images/mob_image'
            namer: Vich\UploaderBundle\Naming\SmartUniqueNamer
garak commented 6 months ago

Your original comment (before the editing) was more relevant and didn't show any mention of this bundle. What makes you think the problem is here?

Mirieri commented 6 months ago

here is the trace:

{"type":"https:\/\/tools.ietf.org\/html\/rfc2616#section-10","title":"An error occurred","detail":"The content-type \u0022multipart\/form-data; boundary=--------------------------975666414905954377383152\u0022 is not supported. Supported MIME types are \u0022application\/json\u0022.","trace":[{"namespace":"","short_class":"","class":"","type":"","function":"","file":"\/Users\/mirierimogaka\/Downloads\/rest_api\/adgg-zoetis-api\/vendor\/api-platform\/core\/src\/Symfony\/EventListener\/DeserializeListener.php","line":166,"args":[]},{"namespace":"ApiPlatform\\Symfony\\EventListener","short_class":"DeserializeListener","class":"ApiPlatform\\Symfony\\EventListener\\DeserializeListener","type":"-\u003E","function":"getFormat","file":"\/Users\/mirierimogaka\/Downloads\/rest_api\/adgg-zoetis-api\/vendor\/api-platform\/core\/src\/Symfony\/EventListener\/DeserializeListener.php","line":129,"args":[["object","Symfony\\Component\\HttpFoundation\\Request"],["array",{"json":["array",[["string","application\/json"]]]}]]},{"namespace":"ApiPlatform\\Symfony\\EventListener","short_class":"DeserializeListener","class":"ApiPlatform\\Symfony\\EventListener\\DeserializeListener","type":"-\u003E","function":"onKernelRequest","file":"\/Users\/mirierimogaka\/Downloads\/rest_api\/adgg-zoetis-api\/vendor\/symfony\/event-dispatcher\/Debug\/WrappedListener.php","line":117,"args":[["object","Symfony\\Component\\HttpKernel\\Event\\RequestEvent"],["string","kernel.request"],["object","Symfony\\Component\\HttpKernel\\Debug\\TraceableEventDispatcher"]]},{"namespace":"Symfony\\Component\\EventDispatcher\\Debug","short_class":"WrappedListener","class":"Symfony\\Component\\EventDispatcher\\Debug\\WrappedListener","type":"-\u003E","function":"__invoke","file":"\/Users\/mirierimogaka\/Downloads\/rest_api\/adgg-zoetis-api\/vendor\/symfony\/event-dispatcher\/EventDispatcher.php","line":230,"args":[["object","Symfony\\Component\\HttpKernel\\Event\\RequestEvent"],["string","kernel.request"],["object","Symfony\\Component\\HttpKernel\\Debug\\TraceableEventDispatcher"]]},{"namespace":"Symfony\\Component\\EventDispatcher","short_class":"EventDispatcher","class":"Symfony\\Component\\EventDispatcher\\EventDispatcher","type":"-\u003E","function":"callListeners","file":"\/Users\/mirierimogaka\/Downloads\/rest_api\/adgg-zoetis-api\/vendor\/symfony\/event-dispatcher\/EventDispatcher.php","line":59,"args":[["array",[["object","Symfony\\Component\\EventDispatcher\\Debug\\WrappedListener"],["object","Symfony\\Component\\EventDispatcher\\Debug\\WrappedListener"],["object","Symfony\\Component\\EventDispatcher\\Debug\\WrappedListener"],["object","Symfony\\Component\\EventDispatcher\\Debug\\WrappedListener"],["object","Symfony\\Component\\EventDispatcher\\Debug\\WrappedListener"],["object","Symfony\\Component\\EventDispatcher\\Debug\\WrappedListener"],["object","Symfony\\Component\\EventDispatcher\\Debug\\WrappedListener"],["object","Symfony\\Component\\EventDispatcher\\Debug\\WrappedListener"],["object","Symfony\\Component\\EventDispatcher\\Debug\\WrappedListener"],["object","Symfony\\Component\\EventDispatcher\\Debug\\WrappedListener"],["object","Symfony\\Component\\EventDispatcher\\Debug\\WrappedListener"],["object","Symfony\\Component\\EventDispatcher\\Debug\\WrappedListener"],["object","Symfony\\Component\\EventDispatcher\\Debug\\WrappedListener"],["object","Symfony\\Component\\EventDispatcher\\Debug\\WrappedListener"],["object","Symfony\\Component\\EventDispatcher\\Debug\\WrappedListener"],["object","Symfony\\Component\\EventDispatcher\\Debug\\WrappedListener"],["object","Symfony\\Component\\EventDispatcher\\Debug\\WrappedListener"],["object","Symfony\\Component\\EventDispatcher\\Debug\\WrappedListener"]]],["string","kernel.request"],["object","Symfony\\Component\\HttpKernel\\Event\\RequestEvent"]]},{"namespace":"Symfony\\Component\\EventDispatcher","short_class":"EventDispatcher","class":"Symfony\\Component\\EventDispatcher\\EventDispatcher","type":"-\u003E","function":"dispatch","file":"\/Users\/mirierimogaka\/Downloads\/rest_api\/adgg-zoetis-api\/vendor\/symfony\/event-dispatcher\/Debug\/TraceableEventDispatcher.php","line":151,"args":[["object","Symfony\\Component\\HttpKernel\\Event\\RequestEvent"],["string","kernel.request"]]},{"namespace":"Symfony\\Component\\EventDispatcher\\Debug","short_class":"TraceableEventDispatcher","class":"Symfony\\Component\\EventDispatcher\\Debug\\TraceableEventDispatcher","type":"-\u003E","function":"dispatch","file":"\/Users\/mirierimogaka\/Downloads\/rest_api\/adgg-zoetis-api\/vendor\/symfony\/http-kernel\/HttpKernel.php","line":133,"args":[["object","Symfony\\Component\\HttpKernel\\Event\\RequestEvent"],["string","kernel.request"]]},{"namespace":"Symfony\\Component\\HttpKernel","short_class":"HttpKernel","class":"Symfony\\Component\\HttpKernel\\HttpKernel","type":"-\u003E","function":"handleRaw","file":"\/Users\/mirierimogaka\/Downloads\/rest_api\/adgg-zoetis-api\/vendor\/symfony\/http-kernel\/HttpKernel.php","line":79,"args":[["object","Symfony\\Component\\HttpFoundation\\Request"],["integer",1]]},{"namespace":"Symfony\\Component\\HttpKernel","short_class":"HttpKernel","class":"Symfony\\Component\\HttpKernel\\HttpKernel","type":"-\u003E","function":"handle","file":"\/Users\/mirierimogaka\/Downloads\/rest_api\/adgg-zoetis-api\/vendor\/symfony\/http-kernel\/Kernel.php","line":195,"args":[["object","Symfony\\Component\\HttpFoundation\\Request"],["integer",1],["boolean",true]]},{"namespace":"Symfony\\Component\\HttpKernel","short_class":"Kernel","class":"Symfony\\Component\\HttpKernel\\Kernel","type":"-\u003E","function":"handle","file":"\/Users\/mirierimogaka\/Downloads\/rest_api\/adgg-zoetis-api\/public\/index.php","line":20,"args":[["object","Symfony\\Component\\HttpFoundation\\Request"]]}]}

Mirieri commented 6 months ago

Your original comment (before the editing) was more relevant and didn't show any mention of this bundle. What makes you think the problem is here?

Is it possible to set content-type format with this bundle?

garak commented 6 months ago

Which content type?

l0r3nc335 commented 6 months ago

Try to watch these videos. he has the same issues as you have and he fixed it.

https://www.youtube.com/watch?v=DE244fDABxE&t=589s https://www.youtube.com/watch?v=E8hdiWtLKLU&t=1054s