rbw / pysnow

ServiceNow API Client Library
MIT License
204 stars 90 forks source link

Images added to tickets not viewable #119

Closed aufbakanleitung closed 5 years ago

aufbakanleitung commented 5 years ago

Currently I'm adding images to Snow just like any other attachment:

def post_call(new_record):
    incident = c.resource(api_path='/table/new_call')
    result = incident.create(payload=new_record)
    sys_id = result.one().get('sys_id')
    att_list = new_record['attachment_list']
    for att in att_list:
        incident.attachments.upload(sys_id=sys_id, file_path=att)

However when I try to view an image that's added like this it just shows the raw image data: view image results If I download the image first it works just fine.

I assume this is because the images are not marked as images by PySnow. I looked through the PySnow documentation and I couldn't find anything regarding a setting, so thought it might be a bug.

rbw commented 5 years ago

Hello,

Yes, you're right. We need to detect and set the appropriate content type. Shouldn't be too hard to fix, I'll have this implemented shortly.

Thanks for reporting!

/Robert

aufbakanleitung commented 5 years ago

Thanks Robert!

Here's a ServiceNow article regarding image storage in de db, if that helps. It says ServiceNow only supports .gif, .jpg, .png or .bmp. But some places .jpeg is also mentioned, so I'm pretty sure that one works too.

aufbakanleitung commented 5 years ago

Hey Robert,

Sorry to bother you, I realize you're just doing this as a personal project, but when do you think you'll have time to look at this? I tried to see if I could fix it myself, but I'm a bit out of my depth.

rbw commented 5 years ago

Hello!

Been a little busy with another project this weekend.

I'll have this fixed later today or tomorrow.

-- Robert Wikman 0xf6feb506ae5d3762

On Mon, Aug 5, 2019 at 10:05 AM Herman van der Veer < notifications@github.com> wrote:

Hey Robert,

Sorry to bother you, I realize you're just doing this as a personal project, but when do you think you'll have time to look at this? I tried to see if I could fix it myself, but I'm a bit out of my depth.

— You are receiving this because you were assigned. Reply to this email directly, view it on GitHub https://github.com/rbw/pysnow/issues/119?email_source=notifications&email_token=AAJUMWAN2NZCWMMEWE4CQG3QC7NNXA5CNFSM4IH47SN2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD3RA2AQ#issuecomment-518130946, or mute the thread https://github.com/notifications/unsubscribe-auth/AAJUMWESOXTXP5MQCG4NWQLQC7NNXANCNFSM4IH47SNQ .

rbw commented 5 years ago

Did some quick testing on a New York developer instance, and it seems that type identification of uploaded files is automatically performed service-side.

Which version of ServiceNow are you using?

Also, I noticed you're sending "attachment_list" with the payload. This is not necessary, and may even be the cause of the issue.

Before I start working on this, could you try the following?:

attachments = ["image.png", "image.jpeg", "image.jpg"]
incident = c.resource(api_path="/table/new_call")
record = incident.create(payload=new_record)

for attachment in attachments:
    record.upload(attachment)
aufbakanleitung commented 5 years ago

I'm using Madrid. I don't quite understand what you mean with the code you're suggesting. Do you want the images to be in-memory? Because I've yet been unable to make that work, that's why I'm referring to the location of the images here

incident.attachments.upload(sys_id=sys_id, file_path=att)

Other that that, your example attachments variable is the same as my att_list

aufbakanleitung commented 5 years ago

Oh wait, I think I see the difference now. you're using record.upload whereas I'm using incident.attachments.upload. Alright, I'll give your suggestion a try.

aufbakanleitung commented 5 years ago

I've changed my code to this:

def post_call(new_record):
    incident = c.resource(api_path='/table/new_call')
    record = incident.create(payload=new_record)
    att_list = new_record['attachment_list']
    for att in att_list:
        try:
            record.upload(file_path=att)
        except FileNotFoundError:
            print("1 attachment is not added")

It works, but the issue remains. Both with .png and .jpeg

rbw commented 5 years ago

Could you try popping attachment_list from the payload prior to calling create? Just to make sure that's not causing this.

E.g.

def post_call(new_record):
    attachments = new_record.pop('attachment_list')
    incident = c.resource(api_path='/table/new_call')
    record = incident.create(payload=new_record)
    for att in attachments:
        try:
            record.upload(file_path=att)
        except FileNotFoundError:
            print("1 attachment is not added")
aufbakanleitung commented 5 years ago

Didn't change it's behavior.

image
rbw commented 5 years ago

Should be fixed in #121, could you test it out?

aufbakanleitung commented 5 years ago

Hey Robert,

Thanks! It solved the problem. However I was experiencing some issues with magic itself. It threw the error ImportError: failed to find libmagic. Check your installation. I had to apply this solution: https://github.com/Yelp/elastalert/issues/1927

pip uninstall python-magic
pip install python-magic-bin==0.4.14

Something to keep an eye out on, perhaps.

rbw commented 5 years ago

:+1:

Something to keep an eye out on, perhaps.

Definitely. Thanks.

aufbakanleitung commented 5 years ago

Are you planning to release a new version soon? I'm trying to assess whether to change the code by hand, or just to wait for an official release.

rbw commented 5 years ago

Yes, I'll release it later today.

rbw commented 5 years ago

@aufbakanleitung update released

thanks for reporting.

aufbakanleitung commented 5 years ago

Excellent!

However, I'm also using PySnow on a windows machine, and here Python-magic is also giving issues. It seems this library just doesn't work out of the box. image To be clear, it is installed. image

aufbakanleitung commented 5 years ago

Also on Windows using python-magic-bin==0.4.14 solved the issue. I think you could simply solve this by requiring version 0.4.14 instead of 0.4.15.

rbw commented 5 years ago

python-magic requires libmagic, which is available out of the box on most Linux distros. On Mac or Windows, you'll need to install this library manually to get python-magic to build--or use the python-magic-bin fork, which comes with a precompiled libmagic. Problem is, python-magic-bin is not available on Linux.

So, with this in mind, I implemented a conditional import of the python-magic library, and If it fails, the warning with hints about python-magic-bin is shown (seen in your first screenshot), and content-type becomes "text/plain" for all uploads.