Code Monkey home page Code Monkey logo

multipartuploadbundle's Introduction

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 indexAction(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 Riverline\MultiPartParser\StreamedPart object. Will be possible to access as well all the parts trough the related-parts attribute.

$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 [email protected] or visit https://www.goetas.com

multipartuploadbundle's People

Contributors

bigfoot90 avatar goetas avatar guite avatar kunicmarko20 avatar nyholm avatar stof avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

multipartuploadbundle's Issues

Possibility to fetch raw input

Hi,
if the request content is changed using first_part_as_default there is no possibility to fetch the original payload.
Would be great if this would still be available using something like $request->attributes->get('original-content') or similar.

Move event listener logic into dedicated service

Hello,

it would be great if MultipartRequestListener would only react on the event but call an external (separate) class, e.g. RequestProcessor or similar, for doing the actual work.

Benefit: it could be easier reused, for example to process a given file containing a multipart payload, by injecting the service class and just calling its method(s) without any event coupling.

Current workaround is creating a fake request and calling MultipartRequestListener#processRequest using PHP's ReflectionMethod manually.

Throuble with v1.0.0 release

I'm having some throuble after the realease 1.0.0
Until now i was forcing composer to use "goetas/multipart-upload-bundle": "dev-master#dabfbb162c87838f2f81f4164ac20e3cf0b57f7e" as a workaround due to my lack of time to debug.

After upgrade to 1.0.0 Symfony is ending with error The file could not be uploaded.
Request body:

--delimiter
Content-Type: application/json

{"some JSON data": true}
--delimiter
Content-Disposition:form-data; name=content[images][]; filename=1px.jpg
Content-Type: image/jpeg

===binary data===
--delimiter--

Now I've a bit of time, and I'm trying to debug into more deeply.
I'll give you more details soon.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.