def extract_and_validate_upload(cls, upload): """Validate and extract manifest from a FileUpload instance. Can raise ParseError.""" with private_storage.open(upload.path) as file_obj: # The file will already have been uploaded at this point, so force # the content type to make the ExtensionValidator happy. We just # need to validate the contents. file_obj.content_type = 'application/zip' manifest_contents = ExtensionValidator(file_obj).validate() return manifest_contents
def create(self, request, *args, **kwargs): file_obj = request.FILES.get('file', None) if not file_obj: raise exceptions.ParseError(_('Missing file in request.')) user = request.user if request.user.is_authenticated() else None # ExtensionValidator will raise ParseError exceptions if appropriate. ExtensionValidator(file_obj).validate() upload = FileUpload.from_post( file_obj, file_obj.name, file_obj.size, user=user) upload.update(valid=True) serializer = self.get_serializer(upload) return Response(serializer.data, status=status.HTTP_202_ACCEPTED)