Пример #1
0
    def post(self, request):
        if not request.FILES.get('attachment'):
            return HttpResponseBadRequest(
                'A file must be provided under the `attachment` POST parameter'
            )

        fr = request.FILES['attachment']

        mimetype, encoding = mimetypes.guess_type(fr.name)
        if not mimetype in settings.ALLOWED_FORMATS:
            return HttpResponseForbidden(
                'The provided file\'s mimetype of `%s` is not in the ALLOWED_FORMATS list'
                % mimetype)

        # Ugh, we should change this; needs to be a better way to get a unique ID
        # rather than relying on creating instance first
        image_identifier = ImageModel.generate_hash()

        storage_instance = STORAGE_LIBRARY(filename=image_identifier)
        storage_instance.store(fr, content_type=fr.content_type)

        image_instance = ImageModel(file_name=fr.name,
                                    content_type=fr.content_type)
        image_instance.hash = image_identifier
        image_instance.path = storage_instance.get_remote_path()
        image_instance.save()

        return HttpResponse(image_identifier)
Пример #2
0
    def post(self, request):
        if not request.FILES.get('attachment'):
            return HttpResponseBadRequest('A file must be provided under the `attachment` POST parameter')

        fr = request.FILES['attachment']

        mimetype, encoding = mimetypes.guess_type(fr.name)
        if not mimetype in settings.ALLOWED_FORMATS:
            return HttpResponseForbidden('The provided file\'s mimetype of `%s` is not in the ALLOWED_FORMATS list' % mimetype)


        # Ugh, we should change this; needs to be a better way to get a unique ID
        # rather than relying on creating instance first
        image_identifier = ImageModel.generate_hash()

        storage_instance = STORAGE_LIBRARY(filename=image_identifier)
        storage_instance.store(fr, content_type=fr.content_type)

        image_instance = ImageModel(file_name=fr.name, content_type=fr.content_type)
        image_instance.hash = image_identifier
        image_instance.path = storage_instance.get_remote_path()
        image_instance.save()

        return HttpResponse(image_identifier)