Esempio n. 1
0
    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(
            data=request.data, context={"request": request}
        )
        if serializer.is_valid():
            dump = Dump.objects.create(
                index=str(uuid.uuid1()),
                author=request.user,
                upload=request.FILES["upload"],
                name=serializer.validated_data["name"],
                operating_system=serializer.validated_data["operating_system"],
            )

            os.mkdir("{}/{}".format(settings.MEDIA_ROOT, dump.index))
            Result.objects.bulk_create(
                [
                    Result(
                        plugin=up.plugin,
                        dump=dump,
                        result=5 if not up.automatic else 0,
                    )
                    for up in UserPlugin.objects.filter(
                        plugin__operating_system__in=[dump.operating_system, "Other"],
                        user=request.user,
                        plugin__disabled=False,
                    )
                ]
            )
            transaction.on_commit(lambda: index_f_and_f(dump.pk, request.user.pk))
            return Response(
                status=status.HTTP_200_OK,
                data=ShortDumpSerializer(dump, context={"request": request}).data,
            )
        return Response(status=status.HTTP_400_BAD_REQUEST, data=serializer.errors)
Esempio n. 2
0
    def import_local(self, request):
        local_path = Path(request.data["filepath"])
        media_path = "{}/{}".format(settings.MEDIA_ROOT, "uploads")

        uploaded_name = "{}/{}".format(media_path, local_path.name)

        if not local_path.exists():
            return Response(
                {"Error": "Filepath does not exists!"},
                status=status.HTTP_400_BAD_REQUEST,
            )

        if not Path(settings.MEDIA_ROOT) in Path(local_path).parents:
            return Response(
                {"Error": "Filepath must be under MEDIA PATH!"},
                status=status.HTTP_400_BAD_REQUEST,
            )

        # IF ALREADY UNDER RIGHT FOLDER OK, ELSE MOVE IT
        if local_path.parent.absolute() == media_path:
            uploaded_name = local_path
        else:
            local_path.rename(uploaded_name)

        operating_system = request.data["operating_system"]
        operating_system = operating_system.capitalize()
        if operating_system not in ["Linux", "Windows", "Mac"]:
            return Response(
                {
                    "Error":
                    "Option selected for OS is not valid [Linux, Windows, Mac]."
                },
                status=status.HTTP_400_BAD_REQUEST,
            )

        name = request.data["name"]
        operating_system = operating_system

        with transaction.atomic():
            dump = Dump(
                author=request.user,
                index=str(uuid.uuid1()),
                name=name,
                operating_system=operating_system,
            )
            dump.upload.name = str(uploaded_name)
            dump.save()
            Result.objects.bulk_create([
                Result(
                    plugin=up.plugin,
                    dump=dump,
                    result=5 if not up.automatic else 0,
                ) for up in UserPlugin.objects.filter(
                    plugin__operating_system__in=[
                        operating_system,
                        "Other",
                    ],
                    user=request.user,
                    plugin__disabled=False,
                )
            ])
            transaction.on_commit(
                lambda: index_f_and_f(dump.pk, request.user.pk))

        return Response(
            status=status.HTTP_200_OK,
            data=ShortDumpSerializer(dump, context={
                "request": request
            }).data,
        )
Esempio n. 3
0
    def handle(self, *args, **options):
        local_path = Path(options["filepath"])
        media_path = Path("{}/{}".format(settings.MEDIA_ROOT, "uploads"))

        uploaded_name = "{}/{}".format(media_path, local_path.name)

        if not local_path.exists():
            self.stdout.write(self.style.ERROR("Path does not exists"))
            return

        if Path(settings.MEDIA_ROOT) not in Path(local_path).parents:
            self.stdout.write(self.style.ERROR("Path not valid"))
            return

        # IF ALREADY UNDER RIGHT FOLDER OK, ELSE MOVE IT
        if local_path.parent.absolute() == media_path:
            self.stdout.write("File in correct path")
            uploaded_name = local_path
        else:
            local_path.rename(uploaded_name)
            self.stdout.write("File moved to upload folder")

        operating_system = options["os"]
        operating_system = operating_system.capitalize()
        if operating_system not in ["Linux", "Windows", "Mac"]:
            self.stdout.write(
                self.style.ERROR(
                    'Os not valid: options available "Linux", "Windows", "Mac"'
                ))
            return

        name = options["name"]
        author = get_user_model().objects.get(username=options["author"])

        with transaction.atomic():
            dump = Dump(
                author=author,
                index=str(uuid.uuid1()),
                name=name,
                operating_system=operating_system,
            )
            dump.upload.name = str(uploaded_name)
            dump.save()
            Result.objects.bulk_create([
                Result(
                    plugin=up.plugin,
                    dump=dump,
                    result=5 if not up.automatic else 0,
                ) for up in UserPlugin.objects.filter(
                    plugin__operating_system__in=[
                        operating_system,
                        "Other",
                    ],
                    user=author,
                    plugin__disabled=False,
                )
            ])
            transaction.on_commit(lambda: index_f_and_f(dump.pk, author.pk))

        self.stdout.write(
            self.style.SUCCESS("Dump {} created, file at {}!".format(
                dump.name, dump.upload.path)))