Ejemplo n.º 1
0
 def default(self, obj):
     try:
         if isinstance(obj, date):
             return obj.isoformat() + "Z"
         iterable = iter(obj)
     except TypeError:
         pass
     else:
         return list(iterable)
     return JSONEncoder.default(self, obj)
Ejemplo n.º 2
0
 def default(self, obj):
     try:
         if isinstance(obj, datetime):
             new_obj = obj.replace(tzinfo=None)
             return new_obj.isoformat() + "Z"
         if isinstance(obj, date):
             return obj.isoformat()
         iterable = iter(obj)
     except TypeError:
         pass
     else:
         return list(iterable)
     return JSONEncoder.default(self, obj)
Ejemplo n.º 3
0
async def simple_api(path):
    path = unquote('/' + path)
    fs = current_app.fs
    user = auth.user_from_request(fs, request)
    username = user['username'] if user else '*'

    if request.method == 'GET':
        if path.endswith('/'):
            enc = JSONEncoder()
            listiter = fs.listdir(path, owner=username)
            async def iterator():
                for p in listiter:
                    p1 = p.to_dict()
                    del p1['data']
                    del p1['history_key']
                    r = (enc.encode(p1) + '\n').encode('utf8')
                    yield r
            return Response(iterator(), status=200, content_type='application/json')

        try:
            rev = request.args.get('rev', None)
            if rev:
                rev = int(rev)
            fp = fs.open(path, owner=username, rev=rev)
        except (FileNotFoundError, ValueError):
            raise exceptions.NotFound()
        except PermissionError:
            raise exceptions.Forbidden()
        headers = {}

        return good_response(fp,
                            headers=headers)

    elif request.method == 'PUT':
        ctype = request.headers.get('content-type', '')
        print('UPLOADING', path, request.headers, ctype)
        try:
            with fs.open(path, mode='w', owner=username) as fp:
                fp.do_hash('md5')
                fp.content_type = ctype
                for metakey in request.headers:
                    if metakey.startswith('x-amz-meta-'):
                        metavalue = request.headers[metakey]
                        metakey = metakey[11:]
                        fp.meta[metakey] = metavalue
                expiration = request.headers.get('Expires', None)
                if expiration:
                    fp.meta['expiration'] = float(expiration)
                await aws.read_request(request, fp)
        except FileNotFoundError:
            raise exceptions.NotFound()
        except PermissionError:
            raise exceptions.Forbidden()
        return Response(fp.meta['md5'])

    elif request.method == 'DELETE':
        try:
            if fs.delete(path, owner=username):
                return Response('')
            else:
                raise exceptions.NotFound()
        except PermissionError:
            raise exceptions.Forbidden()
        return Response('')