bratao / tus-flask

A flask filter for the TUS resumable upload protocol
MIT License
1 stars 0 forks source link

Support CORS maybe? #1

Open mirceaciu opened 6 years ago

mirceaciu commented 6 years ago

I'm trying to use a react app with Uppy Tus client and this tus-flask server.

Can something be made to support CORS protocol?

I'm trying to wrap this package with another class that add the header that js client continues to ask for, but it feels just like a big messy hack

class TusWrapped(TusFilter):
    def __init__(self, app, upload_path, api_base='', tmp_dir='/tmp/upload', expire=60 * 60 * 24 * 30, send_file=False,
                 max_size=2 ** 31, callback=None):
        super().__init__(app, upload_path, api_base=api_base, tmp_dir=tmp_dir, expire=expire, send_file=send_file, max_size=max_size, callback=callback)

    def __call__(self, environ, start_response):
        req = webob.Request(environ)

        resp = webob.Response()
        resp.headers.add('Access-Control-Allow-Origin', req.headers.environ.get('HTTP_ORIGIN'))
        resp.headers.add('Access-Control-Allow-Headers', '*')
        resp.headers.add('Access-Control-Expose-Headers', 'Location, Upload-Length, Upload-Offset')
        resp.headers.add('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE, OPTIONS')

        temp = dict(upload_finished=False, info_loaded=False)
        info = dict()
        env = Env(req=req, resp=resp, temp=temp, info=info)
        if not req.path.startswith(self.upload_path):
            return self.app(environ, start_response)
        try:
            self.handle(env)
        except Error as e:
            self.finish_error(env, e)

        if env.temp['upload_finished'] and (self.callback is not None):
            self.callback(env.temp['uid'])

        return resp(environ, start_response)
mirceaciu commented 6 years ago

Without I will get client browser errors like

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource

Request header field Tus-Resumable is not allowed by Access-Control-Allow-Headers in preflight response

Refused to get unsafe header "Location"

Method PATCH is not allowed by Access-Control-Allow-Methods in preflight response.

I have the flask app wrapped with flask-cors, but has not effect on the TUS functionality.

Not looking for any new implementation, maybe just some tips