コード例 #1
0
ファイル: remotes.py プロジェクト: wikijm/mwdb-core
    def post(self, remote_name, identifier):
        """
        ---
        summary: Pulls file from remote to local instance
        description: |
            Pulls file from the remote instance to the local instance
        security:
            - bearerAuth: []
        tags:
            - remotes
        parameters:
            - in: path
              name: remote_name
              description: Name of remote instance
              schema:
                type: string
            - in: path
              name: identifier
              description: Object identifier (SHA256/SHA512/SHA1/MD5)
              schema:
                type: string
        responses:
            200:
                description: Information about pulled file
                content:
                  application/json:
                    schema: FileItemResponseSchema
            404:
                description: |
                    When the name of the remote instance is not figured
                    in the application config
            409:
                description: Object exists yet but has different type
        """
        remote = RemoteAPI(remote_name)
        response = remote.request("GET", f"file/{identifier}")
        file_name = response.json()["file_name"]
        response = remote.request("GET",
                                  f"file/{identifier}/download",
                                  stream=True)
        with SpooledTemporaryFile() as file_stream:
            for chunk in response.iter_content(chunk_size=2**16):
                file_stream.write(chunk)
            file_stream.seek(0)
            try:
                item, is_new = File.get_or_create(
                    file_name=file_name,
                    file_stream=file_stream,
                    share_with=[
                        group for group in g.auth_user.groups
                        if group.name != "public"
                    ],
                )
            except ObjectTypeConflictError:
                raise Conflict(
                    "Object already exists locally and is not a file")

        return self.create_pulled_object(item, is_new)
コード例 #2
0
 def _create_object(self, spec, parent, share_with, metakeys):
     try:
         return File.get_or_create(
             request.files["file"].filename,
             request.files["file"].stream,
             parent=parent,
             share_with=share_with,
             metakeys=metakeys,
         )
     except ObjectTypeConflictError:
         raise Conflict("Object already exists and is not a file")
     except EmptyFileError:
         raise BadRequest("File cannot be empty")
コード例 #3
0
ファイル: file.py プロジェクト: CERT-Polska/mwdb-core
 def _create_object(self, spec, parent, share_with, attributes, analysis_id, tags):
     try:
         return File.get_or_create(
             request.files["file"].filename,
             request.files["file"].stream,
             parent=parent,
             share_with=share_with,
             attributes=attributes,
             analysis_id=analysis_id,
             tags=tags,
         )
     except ObjectTypeConflictError:
         raise Conflict("Object already exists and is not a file")
     except EmptyFileError:
         raise BadRequest("File cannot be empty")
コード例 #4
0
    def post(self, remote_name, identifier):
        """
        ---
        summary: Pulls file from remote to local instance
        description: |
            Pulls file from the remote instance to the local instance
        security:
            - bearerAuth: []
        tags:
            - remotes
        parameters:
            - in: path
              name: remote_name
              description: Name of remote instance
              schema:
                type: string
            - in: path
              name: identifier
              description: Object identifier (SHA256/SHA512/SHA1/MD5)
              schema:
                type: string
        requestBody:
            required: false
            description: Additional options for object pull
            content:
              application/json:
                schema: RemoteOptionsRequestSchema
        responses:
            200:
                description: Information about pulled file
                content:
                  application/json:
                    schema: FileItemResponseSchema
            404:
                description: |
                    When the name of the remote instance is not figured
                    in the application config
            409:
                description: Object exists yet but has different type
            503:
                description: |
                    Request canceled due to database statement timeout.
        """
        remote = RemoteAPI(remote_name)
        response = remote.request("GET", f"file/{identifier}")
        file_name = response.json()["file_name"]
        response = remote.request("GET",
                                  f"file/{identifier}/download",
                                  stream=True)
        options = loads_schema(request.get_data(as_text=True),
                               RemoteOptionsRequestSchema())
        share_with = get_shares_for_upload(options["upload_as"])
        with SpooledTemporaryFile() as file_stream:
            for chunk in response.iter_content(chunk_size=2**16):
                file_stream.write(chunk)
            file_stream.seek(0)
            try:
                item, is_new = File.get_or_create(
                    file_name=file_name,
                    file_stream=file_stream,
                    share_with=share_with,
                )
            except ObjectTypeConflictError:
                raise Conflict(
                    "Object already exists locally and is not a file")

        return self.create_pulled_object(item, is_new)