0 Replies - 633 Views - Last Post: 05 January 2022 - 06:33 PM Rate Topic: -----

#1 fearfulsc2   User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 371
  • Joined: 25-May 16

Cloudmersive Virus Scanning for file....File Name Too Long

Posted 05 January 2022 - 06:33 PM

Hi everyone, I am trying to add an AV scanner for our API that accepts a user upload of a file.

I am running into this issue
OSError: [Errno 63] File name too long:

I am not sure how to handle this since I can't write to disk, even for temp files.

In our API, we have something along the lines of this
    if 'file' not in request.files:
        raise AppException.bad_request(message='No file found in request')
    file = request.files['file']
    if not file.filename:
        raise AppException.bad_request(message='No filename found in request')
    return file



I tried adding this
    if 'file' not in request.files:
        raise AppException.bad_request(message='No file found in request')
    file = request.files['file']

    configuration = cloudmersive_virus_api_client.Configuration()
    configuration.api_key['Apikey'] = config.CLOUDMERSIVE_API_KEY

    api_instance = cloudmersive_virus_api_client.ScanApi(cloudmersive_virus_api_client.ApiClient(configuration))

    byte_io = io.BytesIO(file.read())
    # convert byte_io to bytes
    result_bytes = byte_io.read()

    try:
        api_response = api_instance.scan_file(result_bytes)
        print(api_response)
    except ApiException as e:
        print("Exception when calling ScanApi->scan_file_stream_post: %s\n" % e)

    if not file.filename:
        raise AppException.bad_request(message='No filename found in request')
    return file



However, I get the error above.

If I try doing it like this, I get this error TypeError: expected str, bytes or os.PathLike object, not FileStorage

api_response = api_instance.scan_file(file)



If I look in the package they provide, this is what I find
    def prepare_post_parameters(self, post_params=None, files=None):
        """Builds form parameters.

        :param post_params: Normal form parameters.
        :param files: File parameters.
        :return: Form parameters with files.
        """
        params = []

        if post_params:
            params = post_params

        if files:
            for k, v in six.iteritems(files):
                if not v:
                    continue
                file_names = v if type(v) is list else [v]
                for n in file_names:
                    with open(n, 'rb') as f:
                        filename = os.path.basename(f.name)
                        filedata = f.read()
                        mimetype = (mimetypes.guess_type(filename)[0] or
                                    'application/octet-stream')
                        params.append(
                            tuple([k, tuple([filename, filedata, mimetype])]))

        return params



Since I am not saving the file to disk, I am not sure of a way that allows me to take the file the user uploads and scan it for malicious content.

Any thoughts or suggestions?

Is This A Good Question/Topic? 0
  • +

Page 1 of 1