agentleFS
gerard-labsgerard-labs/superpowers-api-platform/skills/api-platform-file-upload/SKILL.md

api-platform-file-upload

Skill2 starsChanged 6 months agoMITView on GitHub

Implement file uploads in API Platform 4.3 with the canonical pattern — VichUploaderBundle (`#[Vich\\Uploadable]`, `#[Vich\\UploadableField]`), the `multipart/form-data` format enabled on the `Post` operation (`inputFormats: ['multipart' => …]`), a `MediaObject` resource exposing `contentUrl` via a custom Normalizer that resolves the URL through `Vich\\Storage\\StorageInterface`, the OpenAPI request body annotation (`Model\\RequestBody` with `multipart/form-data` schema and a binary file property), an optional custom multipart `DecoderInterface` for JSON-in-multipart payloads, linking a `MediaObject` to another resource via a regular relation + IRI, Vich-Flysystem for S3/R2 storage, ClamAV antivirus via a Processor decorator, and the test pattern using `Symfony\\Component\\HttpFoundation\\File\\UploadedFile`. Trigger on "upload an image", "MediaObject", "multipart", or "VichUploader".

The file

We split this skill into the rules it gives an agent

2 rules. About 0 are common, 1 is distinctive, and 1 is very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Add a MediaObjectNormalizer that resolves contentUrl via Vich\Storage\StorageInterface::resolveUri().

    line 26
  2. Distinctive:

    Uploads only via POST: PUT/PATCH multipart is not supported by API Platform.

    line 31

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

API Platform 4.3 — File upload

Use when

  • The API needs to accept file uploads (images, documents, exports re-uploaded by users).
  • A resource has an image / file relation (e.g. Book::image).
  • Uploads must be authenticated and validated for size / MIME type.
  • Files need to live on S3 / R2 / Cloud storage instead of the local filesystem.

Default workflow

  1. composer require vich/uploader-bundle (+ vich/flysystem-bundle for cloud storage).
  2. Configure Vich mappings (uri_prefix, upload_destination, namer).
  3. Enable the multipart/form-data format on the Post operation only — never on PUT/PATCH (unsupported by API Platform for multipart).
  4. Model a MediaObject resource with #[Vich\Uploadable] + #[Vich\UploadableField(fileNameProperty: 'filePath')].
  5. Add a MediaObjectNormalizer that resolves contentUrl via Vich\Storage\StorageInterface::resolveUri().
  6. (Optional) Add a multipart DecoderInterface if you need JSON nested in a multipart part.
  7. Apply security: is_granted('ROLE_USER') minimum; consider antivirus and quota voters.

Guardrails

  • Uploads only via POST. PUT/PATCH multipart is not supported by API Platform.
  • Validate file size + MIME with #[Assert\File(maxSize: '5M', mimeTypes: ['image/jpeg', 'image/png'])].
  • Storage path must be public (/public/media/) or fronted by a CDN.
  • Antivirus for any app that accepts files from untrusted users — wire ClamAV via a Processor decorator.
  • No raw entity exposure — always normalize via contentUrl rather than the raw filePath.

Progressive disclosure

  • SKILL.md covers posture and rules.
  • reference.md carries the full pattern: prerequisites, configuration, the MediaObject resource, the MediaObjectNormalizer (ALREADY_CALLED pattern), optional multipart Decoder, linking to other resources, constraints, S3/R2 storage, antivirus integration, and the test recipe.

Output contract

  • A MediaObject resource accepting multipart/form-data on POST only.
  • A normalizer producing a contentUrl field (absolute, cacheable).
  • Validation on file (size, MIME).
  • Security on the upload operation.
  • Functional test using UploadedFile + schema assertion.

References

  • reference.md

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.