1. Nathan Mitton
  2. PowerBuilder
  3. Wednesday, 21 February 2024 04:16 AM UTC

Hi everyone.

I am using PB 2019 R3 2703. We have a problem in our PB application with users being able to attach corrupt files. I want to be able to check whether a file is corrupt before I allow the attachment, but this is not as easy as I thought.
FileOpen succeeds (returns > 0) even when opening a corrupt file, so I can't use that to check. I've tried using ShellExecute, CreateProcess, etc but they also seem to indicate success on opening a corrupted file (I'm probably missing something). I'm playing around with OLE and that seems to be the most promising but even this has it's issues.

So my question is, does anyone out there know of an easy way to use PowerBuilder to determine whether a file is corrupted? If it makes any difference, I only need to check files of type .txt, .pdf, .jpg, .jpeg, .gif, .doc, .docx, .msg, .png, and .html. 

Any advice would be greatly appreciated! 

Thanks,

Nathan

(my first post to a PowerBuilder forum in many years. It is so good to be back on the PowerBuilder bus!)

 

Nathan Mitton Accepted Answer Pending Moderation
  1. Wednesday, 21 February 2024 21:26 PM UTC
  2. PowerBuilder
  3. # 1

Comment
There are no comments made yet.
Chris Pollach @Appeon Accepted Answer Pending Moderation
  1. Wednesday, 21 February 2024 15:18 PM UTC
  2. PowerBuilder
  3. # 2

Hi Nathan;

  There are no such features in PB to help you with this issue as PB FileXxxxx commands are just straight calls to the O/S's data stream file services. They assume that whatever file you open is formatted correctly for your Apps processing.

  Now IMHO, there are two question here: 1) Does the File contain a virus and 2) Your question about following a correct file format.

  For Q#1 I can check a file for an AV present by calling (via the RUN command - for example) Windows Defender to scan any file before I open it. For example: Start-MpScan -ScanPath <YourFile>  -ScanType CustomScan

  For Q#2, you would need to use an external 3rd party tool for that. For example like FITS:  https://projects.iq.harvard.edu/fits

HTH

Regards .. Chris

 

 

Comment
  1. Nathan Mitton
  2. Wednesday, 21 February 2024 21:08 PM UTC
Thanks for your response Chris.

The files that I've encountered so far that have caused an issue have been virus free, They have just been pdf files that have become damaged somehow.

My solution was to tell the users not to attach corrupt files, but that wasn't deemed an acceptable resolution ...
  1. Helpful
  1. Chris Pollach @Appeon
  2. Wednesday, 21 February 2024 21:29 PM UTC
Yes ..I can imagine the "wasn't deemed an acceptable resolution" response - LOL!
  1. Helpful
There are no comments made yet.
Francisco Martinez @Appeon Accepted Answer Pending Moderation
  1. Wednesday, 21 February 2024 14:30 PM UTC
  2. PowerBuilder
  3. # 3

Hi Nathan,

The definition of "corrupt file" depends on its type. In terms of what a file is, is just a collection of bytes. Whether or not it's valid (i.e. un-corrupted) depends on the application doing the interpretation of these bytes. FileOpen will allow you to open corrupted files because it doesn't care about the contents of the file. It will only read the bytes it contains without concerning itself with whether they make sense to their respective applications.

You will need to try to open each file type with its corresponding application and see if it can be opened or it throws an exception.

I'm not sure how you would define a text file (txt) to be corrupted, though. That will depend on how you define a valid txt to be (encoding? structure?).

Regards,
Francisco

Comment
  1. Nathan Mitton
  2. Wednesday, 21 February 2024 21:26 PM UTC
Hi Francisco,

Thanks for replying.

I take your point about corrupt txt files, I was already considering excluding them from the proposed check I'm trying to implement.

'You will need to try to open each file type with its corresponding application and see if it can be opened or it throws an exception' - yep that's what I'm trying to do using OLE ConnectToObject and it works well with pdfs, which is the only file type that has caused issues so far, but because the users can attach several different types, I can't really tell my manager I'm only going to check for pdfs and just hope everything else is OK.

The issues I have with ConnectToObject are

- it is always returning -7 for .txt files but as I said, I might exclude them anyway

- I get different results for .doc and .docx files. docx works exaclty as I want, returns -7 for files that can't be opened, 0 for valid files. It does the same for .doc files, eventually, but it pops up the dialog shown in my next response (I don't seem to be able to post a pic to this reply) in the background, which I do not want.

- it returns success for .png files that cannot be opened in drawing apps like paint etc.



  1. Helpful
  1. Francisco Martinez @Appeon
  2. Wednesday, 21 February 2024 21:32 PM UTC
For doc and docx files I think you could write a C# DLL that attempts to open the files using NPOI, and integrate that DLL into PowerBuilder.

For pictures, I've seen in this forum that Imagemagik is frequently used in PowerBuilder, maybe you could try opening or getting information from these files with Imagemagik and catch any errors. A problem would be that sometimes image file corruption cannot be detected by looking at the file itself, and it's only visible when opening the image and seeing visual artifacts. Not sure what kind of files behave like this though.
  1. Helpful
  1. Nathan Mitton
  2. Wednesday, 21 February 2024 23:19 PM UTC
Thanks for your help, Francisco. I appreciate your suggestions and it looks like I might have to head down the paths you've mentioned.

But, as I suspected, it seems the simple answer to my question 'is there an easy way to use PowerBuilder to determine whether a file is corrupted' is 'No'.

Cheers
  1. Helpful
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.