TNO Intern

Commit 83635e2d authored by Arjo Segers's avatar Arjo Segers
Browse files

Use mimetype of downloaded product to decide on postprocessing.

parent 839e8472
Loading
Loading
Loading
Loading
+29 −24
Original line number Diff line number Diff line
@@ -43,6 +43,9 @@
# 2025-04, Arjo Segers
#   Changed imports for python packaging.
#
# 2025-10, Arjo Segers
#   Use mimetype of downloaded product to decide on postprocessing.
#


########################################################################
@@ -775,6 +778,7 @@ class CSO_DataSpace_Downloader(object):
        import os
        import time
        import requests
        import magic
        import zipfile
        import shutil

@@ -822,9 +826,25 @@ class CSO_DataSpace_Downloader(object):
                # rename:
                os.rename(tmpfile, product_file)
                
                # try to open product file;
                # first try if it is a zipfile:
                try:
                # file type:
                mimetype = magic.from_file( product_file, mime=True )
                # switch:
                #~ nc file:
                if mimetype == "application/x-hdf":

                    # info ..
                    logging.info(f"{indent}product is netcdf file, store ...")
                    # this is the target netcdf file already;
                    # create target dir if necessary:
                    cso_file.CheckDir(output_file, dmode=dmode)
                    # rename to destination:
                    shutil.move(product_file, output_file)

                #~ zip file:
                elif mimetype == "application/zip":

                    # info ..
                    logging.info(f"{indent}product is zip file, unpack ...")
                    # open as zipfile:
                    arch = zipfile.ZipFile(product_file, mode="r")
                    # loop over members, probably two files in a directory:
@@ -859,27 +879,12 @@ class CSO_DataSpace_Downloader(object):
                    logging.info(f"{indent}remove product file ...")
                    # remove package:
                    os.remove(product_file)
                #
                except Exception as err:
                    # info ..
                    msg = str(err)
                    # logging.error("from download; message received:")
                    # logging.error("  %s" % msg)
                    # catch known problem ...
                    if "File is not a zip file" in msg:
                        # logging.warning(f"{indent}maybe download was interrupted, try again  ...")
                        # info ..
                        logging.info(f"{indent}product is no zipfile, rename to output file ...")
                        # this is probably the target file already;
                        # create target dir if necessary:
                        cso_file.CheckDir(output_file, dmode=dmode)
                        # rename to destination:
                        shutil.move(product_file, output_file)
                
                #~ unknown ...
                else:
                        # quit with error:
                        raise
                    logger.error( f"unsupported mimetype '{mimetype}'" )
                    raise Exception
                #endif
                # endtry

                # all ok, leave retry loop:
                break