Hi Cole,

I think I've figured out the reason why attached file being corrupted.
To upload the file we need to pass Base64 encoded data.
Data has to encode as Base64 and data must be declared as Base64 encoded as well. Else it's passed as a string and is not decoded by Bugzilla.
I presume the following code of "attachfile" method needs to be modify in base.py

        data = f.read()
        if not isinstance(data, bytes):
            data = data.encode(locale.getpreferredencoding())
        kwargs['data'] = Binary(data)

I've tried like this but unfortunately it doesn't work.
     
#    data = f.read() 
#    encoded_data = base64.b64encode(data)
#    kwargs['data'] = encoded_data

Reference Bug: https://bugzilla.mozilla.org/show_bug.cgi?id=958559

Any Suggestion?


From: khokhar_cth@live.com
To: crobinso@redhat.com
Date: Thu, 19 Dec 2013 16:58:32 +0100
CC: python-bugzilla@lists.fedorahosted.org
Subject: Re: [python-bugzilla] How to attach file ?

Hi Cole,

Please see my reply in blue 

> Does the bugzilla command line tool work for adding an attachment?

I tried to use command line utility from latest Git but got 407 Client Error: Proxy Authentication Required

 

 

Date:                                    1 month ago

Commit hash:  e1a094dfc8d734be89cb1626435f69118157a225

 

 

Here is trace back history of error.

 

 

Logging in... Traceback (most recent call last):

  File "bugz", line 1173, in <module>

    main()

  File "bugz", line 1093, in main

    if bz.login(global_opt.user, global_opt.password):

  File "C:\cc_stg\Git\python-bugzilla\cgi-bin\bugzilla\base.py", line 483, in login

    r = self._login(self.user, self.password)

  File "C:\cc_stg\Git\python-bugzilla\cgi-bin\bugzilla\base.py", line 452, in _login

    return self._proxy.User.login({'login': user, 'password': password})

  File "C:\Python27\lib\xmlrpclib.py", line 1224, in __call__

    return self.__send(self.__name, args)

  File "C:\Python27\lib\xmlrpclib.py", line 1570, in __request

    verbose=self.__verbose

  File "C:\cc_stg\Git\python-bugzilla\cgi-bin\bugzilla\base.py", line 190, in request

    return self._request_helper(url, request_body)

  File "C:\cc_stg\Git\python-bugzilla\cgi-bin\bugzilla\base.py", line 167, in _request_helper

    response.raise_for_status()

  File "C:\Python27\lib\site-packages\requests\models.py", line 725, in raise_for_status

    raise HTTPError(http_error_msg, response=self)

requests.exceptions.HTTPError: 407 Client Error: Proxy Authentication Required

 


I already have set HTTP_proxy and https_proxy environment variables, so that's not the issue.



> What version is the bugzilla server?

I am using bugzilla version 4.2.3 and version 4.2.4 

> Is this server publicly accessible so I can try and reproduce on my side?

No, it’s not accessible publically. I've attached "lib.py" file from where I am using attachfile methods as patch from latest version of python-bugzilla.

Here you can see the arguments and how I call this function.


            kwards = {
                'filename': file_name
            } 
            
            lib.attachfile(bz_server, bug_id, attach_file, file_name, **kwards) 


Now I’ve two problems:

1.    How to get rid of Proxy Authentication problem?

2.    How can I successfully attach file so it shall not get corrupted after uploading on Bugzilla?


And Thanks for your detailed reply and inquiry about the problem and Thanks in advance as well.

//Khokhar



> Date: Tue, 19 Nov 2013 13:12:41 -0500
> From: crobinso@redhat.com
> To: khokhar_cth@live.com
> CC: python-bugzilla@lists.fedorahosted.org
> Subject: Re: [python-bugzilla] How to attach file ?
>
> On 11/19/2013 05:26 AM, Khokhar cth wrote:
> > Thanks for the information Cole,
> >
> > Installing pycurl was challenge ;-)
> >
> > However, I've installed python-request and latest python-bugzilla version from
> > Git.
> >
> > But still I have problem while attaching the file. File get corrupted (that
> > was my actual problem).
> >
> >
> > I am using "attachfile" method.
> >
> > def attachfile(self, idlist, attachfile, description, **kwargs):
> >
> > and _detect_filetype(fname) and "python magic" methods implemented in base.py but still file(s) (.zip, .jpeg etc) get corrupted after uploading on Bugzilla..
> >
> >
>
> Does the bugzilla command line tool work for adding an attachment?
> What version is the bugzilla server?
> Is this server publicly accessible so I can try and reproduce on my side?
>
> Can you reproduce with a patch like:
>
>
> diff --git a/bugzilla/base.py b/bugzilla/base.py
> index 3a5d65d..5eae7c0 100644
> --- a/bugzilla/base.py
> +++ b/bugzilla/base.py
> @@ -1226,6 +1226,7 @@ class BugzillaBase(object):
> ctype = 'application/octet-stream'
> kwargs['content_type'] = ctype
>
> + print kwargs
> ret = self._proxy.Bug.add_attachment(kwargs)
>
> if "attachments" in ret:
>
>
> So we can see exactly what arguments are being passed to the API call
>
> - Cole

_______________________________________________ python-bugzilla mailing list python-bugzilla@lists.fedorahosted.org https://lists.fedorahosted.org/mailman/listinfo/python-bugzilla