Beispiel #1
0
def file_api(request, kind, filepath=None):
    """Basic API calls for working with non-plist Munki files"""
    if kind not in ["icons", "pkgs"]:
        return HttpResponse(status=404)
    if request.method == "GET":
        LOGGER.debug("Got API GET request for %s", kind)
        if filepath:
            fullpath = MunkiFile.get_fullpath(kind, filepath)
            if not os.path.exists(fullpath):
                return HttpResponse(
                    json.dumps(
                        {
                            "result": "failed",
                            "exception_type": "FileDoesNotExist",
                            "detail": "%s does not exist" % filepath,
                        }
                    ),
                    content_type="application/json",
                    status=404,
                )
            try:
                response = FileResponse(open(fullpath, "rb"), content_type=mimetypes.guess_type(fullpath)[0])
                response["Content-Length"] = os.path.getsize(fullpath)
                response["Content-Disposition"] = 'attachment; filename="%s"' % os.path.basename(filepath)
                return response
            except (IOError, OSError), err:
                return HttpResponse(
                    json.dumps({"result": "failed", "exception_type": str(type(err)), "detail": str(err)}),
                    content_type="application/json",
                    status=403,
                )
        else:
            response = MunkiFile.list(kind)
            return HttpResponse(json.dumps(response) + "\n", content_type="application/json")
Beispiel #2
0
def file_api(request, kind, filepath=None):
    '''Basic API calls for working with non-plist Munki files'''
    if kind not in ['icons', 'pkgs']:
        return HttpResponse(status=404)
    if request.method == 'GET':
        LOGGER.debug("Got API GET request for %s", kind)
        if filepath:
            fullpath = MunkiFile.get_fullpath(kind, filepath)
            if not os.path.exists(fullpath):
                return HttpResponse(
                    json.dumps({'result': 'failed',
                                'exception_type': 'FileDoesNotExist',
                                'detail': '%s does not exist' % filepath}),
                    content_type='application/json', status=404)
            try:
                response = FileResponse(
                    open(fullpath, 'rb'),
                    content_type=mimetypes.guess_type(fullpath)[0])
                response['Content-Length'] = os.path.getsize(fullpath)
                response['Content-Disposition'] = (
                    'attachment; filename="%s"' % os.path.basename(filepath))
                return response
            except (IOError, OSError), err:
                return HttpResponse(
                    json.dumps({'result': 'failed',
                                'exception_type': str(type(err)),
                                'detail': str(err)}),
                    content_type='application/json', status=403)
        else:
            response = MunkiFile.list(kind)
            return HttpResponse(json.dumps(response) + '\n',
                                content_type='application/json')