Skip to content

Releases: goetas/MultipartUploadBundle

1.5.0

06 Feb 15:58
0003cbb
Compare
Choose a tag to compare

What's Changed

  • add missing return type hint by @Guite in #20

Full Changelog: 1.4.0...1.5.0

1.4.0

17 Jan 23:23
bfb1af0
Compare
Choose a tag to compare

What's Changed

Full Changelog: 1.3.0...1.4.0

1.3.0

21 Sep 14:45
88f5f53
Compare
Choose a tag to compare

What's Changed

Full Changelog: 1.2.1...1.3.0

1.2.1

06 Jan 08:15
d417767
Compare
Choose a tag to compare

What's Changed

Full Changelog: 1.2.0...1.2.1

1.2.0

11 Oct 19:28
2050dd6
Compare
Choose a tag to compare
  • split actual service from event listener #15

1.1.0

08 Jul 17:49
8018d02
Compare
Choose a tag to compare
  • Allow Symfony5 and PHP8 #13

1.0.3

06 May 12:22
c2afc42
Compare
Choose a tag to compare
  • do not process multipart/form-data requests

1.0.2

13 Feb 11:54
48015c8
Compare
Choose a tag to compare

1.0.1

09 Jan 20:18
595f35d
Compare
Choose a tag to compare

Minor fixed on documentation and deprecation warning for symfony 4

1.0.0

02 Jan 12:27
c6a34a0
Compare
Choose a tag to compare

GoetasMultipartUploadBundle

Build Status
Latest Stable Version
Code Coverage
Scrutinizer Code Quality

Symfony multipart/related, multipart/alternative and multipart/mixed content type handler.

This bundle implements a subset of the https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html specifications
and allows you to deal with Content-Type: multipart/*; requests with Symfony.

Install

Run composer require goetas/multipart-upload-bundle

Add bundle to symfony (if not using symfony/flex)

Request format

A multipart/related request could look like this:

Host: localhost
Content-Type: multipart/related; boundary=19D523FB

--19D523FB
Content-Type: application/json

{
    "content": "Some JSON content"
}
--19D523FB
Content-Type: image/png
Content-Disposition: form-data; name="image"; filename="image.jpg"
Content-MD5: 314ca078416a9b27efbe338ac5a2f727

... binary content...
Content-Type: text/html
Content-Disposition: form-data; name="content"
Content-MD5: 314ca078416a9b27efbe338ac5a2f727

<a href="https://github.com/goetas/MultipartUploadBundle">HTML content</a>

--19D523FB
Content-Type: image/png
Content-Disposition: attachment; filename="image.jpg"
Content-MD5: 314ca078416a9b27efbe338ac5a2f727

... binary content...

--19D523FB
Content-Type: octet/stream
X-Custom-Header: header value

... binary content...

--19D523FB--

Usage

Controller

Body will not be decoded automatically, you can decode it by yourself or use FOSRestBundle to handle it transparently

public function (Request $request)
{
    if ('application/json' == $request->headers->get('content-type')) {
        $data = json_decode($request->getContent(), true);
    }
}

Form Fields

Parts with form-data; name= in Content-Disposition part's headers
will be treated like an regular uploaded file.

$html = $request->request->get('content');

Can be used with Symfony's form builder

$builder->add('content', TextAreaType::class);

Uploaded Files

Parts with form-data; name= and filename= in Content-Disposition part's headers
will be treated like an regular uploaded file.

$file = $request->files->get('image');

Can be used with Symfony's form builder

$builder->add('image', FileType::class);

Attachment Files

Parts with attachment; filename= in Content-Disposition part's headers
will be treated as an attachment file.

$attachment = $request->attributes->get('attachments')[0];

Related Parts

Parts without a filename will be treated as RelatedPart object.

$part = $request->attributes->get('related-parts')[0];
  • Get part's headers
$headers = $part->getHeaders()->all();
  • Get part's content
$content = $part->getContent();
  • Get part's content as resource
$content = stream_get_contents($part->getContent(true));
  • First part injected

By default, when a message is multipart/*, the first part will become the Symfony message content.
You can disable this by setting first_part_as_default to false.

$content = $request->getContent(); // content of the first part, not the whole message

Configurations

goetas_multipart_upload:
  first_part_as_default: true

Note

The code in this project is provided under the
MIT license.
For professional support
contact goetas@gmail.com
or visit https://www.goetas.com