hivesolutions / netius

Readable, simple and fast asynchronous non-blocking network apps
http://netius.hive.pt
Apache License 2.0
118 stars 5 forks source link

Get content of uploaded file in easier way than by slicing 'wsgi.input' #11

Closed radek-anuszewski closed 8 years ago

radek-anuszewski commented 8 years ago

Hi!

At first I want to thank you for great working library, which is easy to use and play nice with use cases in our projects. When file is sent to server (using Cordova File Transfer plugin) from our hybrid app, content of environment['wsgi.input'].getvalue() is:

--+++++
Content-Disposition: form-data; name="file"; filename="image.jpg"
Content-Type: application/octet-stream

(Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
--+++++--

And when I need to get text content of uploaded file, I have to manually slice environment['wsgi.input'] to get content (to get dispose of boundaries, Content info etc..):

uploadedFileContent = inputWithFile.getvalue()
uploadedFile=StringIO(uploadedFileContent[uploadedFileContent.index(START_OF_FILE_CONTENT):len(uploadedFileContent)])
uploadedFile=StringIO(uploadedFile.getvalue([0:uploadedFile.getvalue().index(BEGINNING_OF_ADDITIONAL_INFO_AT_END_OF_FILE)])
content = str(uploadedFile.getvalue())

Is there a way in your library to get content of uploaded file out of the box, without the need to slice environment['wsgi.input'] maually?

Thank you in advance for reply, Radek from Techmetria.

joamag commented 8 years ago

The idea behind the netius WSGIServer is just to provide a thin layer of compatibility/conformance with the WSGI standard. To be able to (more easily) interact with the contents of the HTTP request you should use a framework like (our) Appier Framework (https://github.com/hivesolutions/appier/).

Here's an example of an appier app to read the file contents of a multipart post request.

import appier

class HelloFileApp(appier.App):

    @appier.route("/", "POST")
    def file_example(self):
        file = self.field("file")
        return file.read()

HelloFileApp().serve()

BTW by default Appier tries to use netius as the WSGI server :)