ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Derive MIME Type for file extension?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Derive MIME Type for file extension?

    I need to write a CGI program to return an IFS File for display in browser. That IFS file could be in a number of document/image formats (rtf, doc, docx, pdf, png, etc.)

    Is there a way I can automatically identify and use the appropriate MIME Type from the file extension? E.g. look up .pdf and get application/pdf? Short of creating my own mime type lookup table.

    The CGI Program will be written in RPGLE (Actually RPGsp unless there's a really good reason why this would be better as regular RPGLE), and I think we are using Apache

  • #2
    Good question. FWIW, I just compiled my own in a table.

    Comment


    • #3
      I'm no expert on this but I think as far as pdfs and images go, if you simply reference the IFS file in the anchor that is used to link it the browser will display it (or offer it for download etc.). Images are always going to display, and pdfs I believe are based on the browser's settings.

      I seem to recall back in the distant past when I used Windows <shudder> that there was an option for the browser to display word files etc. but that seems to have gone away (See https://stackoverflow.com/questions/...ing-javascript for a possible solution).

      Comment


      • Vectorspace
        Vectorspace commented
        Editing a comment
        Yes, but that's only if the file exists on an IFS folder that's within the root directory of the web instance, so it can be accessed by direct URL. I'm working with files in arbitrary locations, so (I think) I have to read them into my program and then write them to STDOUT all in RPGLE. So I have to specify the MIME Type myself.

    • #4
      You could find a list of mime-types online, and put them into a PF or similar. A quick Google search finds this list:
      MIME types are a way to specify the nature and format of a document, file, or assortment of bytes. Here's a list of common MIME types.


      The steps would be:

      1) Create a PF (i.e. database table) with two columns, one for the suffix, another for the mime type.
      2) Copy/paste the table from the link above into Excel
      3) SAve-As the Excel document into a CSV file
      4) Upload CSV to IBM i
      5) Use CPYFRMIMPF to copy the CSV into your PF

      Now you can look up the mime type for a file extension simply by looking it up in the database.

      Comment


      • #5
        Thanks all

        I figured I was going to have to have my own table (that's what my prototype is doing).

        Followup question. It seems like the webserver determines if the file is text or binary from the MIME type, and if text it character set converts it from job CCSID (37) to remote CCSID (ASCII 819 - I wish it was UTF-8 1208!!!).
        Is there a way to tell it not to do this? Pass some HTTP Header to tell the web server "I know this is text, but I've already converted it myself, so please don't convert it for me" while keeping the text MIME Type?

        Because, If it's a UTF-8 1208 text file, my program:
        • reads the text file in using SQL CLOB_FILE
        • Extracts each 32k bytes in turn
        • Converts each 32K from UTF-8 1208 to EBCDIC 37
        • writes each 32k to STDOUT via QtmhWrStdout.
        Then the webserver converts it to ASCII 819. Which could cause corruption if the text file contains non-ASCII characters.

        I wish we'd change the instance to UTF-8, but it's not up to me - the application is so large, the amount of work to test it all still works under UTF-8 isn't feasible for various reasons.

        I know I could use MIME type application/octet-stream to force it to not , but I'd rather use the actual MIME type if I can

        Comment


        • #6
          If you set CgiConvMode binary it will never convert files.

          Comment


          • #7
            Is that something I can specify on the fly? I can't change the instance config

            Comment


            • #8
              No, you'd need to specify it in the instance config.

              Comment


              • #9
                I guess I'm out of luck then. Never mind, thanks all for all the top advice

                Comment

                Working...
                X