def process_new_dicom()

in python/de-identifier/research_pacs/de_identifier/main.py [0:0]


def process_new_dicom(msg):
  """
  Process incoming DICOM files as follows:
  
  If the new DICOM file is stored in Amazon S3 or a file system that is locally mounted to this 
  server, we send it to Orthanc so that we can leverage Orthanc native API for some of the 
  de-identification parts. A new SQS message will be trigger the de-identification for the 
  Orthanc instance.
  
  If the new DICOM file is stored in the source Orthanc server, check if the Orthanc instance is 
  associated with previous "instructions" (see below) and overwrite the current message if needed. 
  Then, process the new Orthanc DICOM instance with the function `process_new_dicom_orthanc` and 
  delete the instance from the source Orthanc server if the processing succeeded.
  
  Args:
    msg (dict): Content of the SQS message
  
  """
  try:
    dicom_source = msg['Source']
    logger.info(f"New DICOM file: Source={dicom_source}")
  except:
    logger.error(f'Attribute "Source" is missing in the message')
    return
  
  # If the DICOM instance is stored in a local file system or S3, upload it to Orthanc and store 
  # the original message in the database. The change pooler will detect that new Orthanc instance 
  # and send another message. That new message will be replaced by the "instructions" contained in 
  # the previous message, such as using a custom config file
  if not dicom_source.startswith('orthanc://'):
    try:
      dicom_file = rpacs_util.load_file(dicom_source, env.region, 'bytes')
      instance_id = client.src_orthanc.upload_instance(dicom_file)
      client.db_msg.upsert(instance_id, msg)
      logger.info(f"Uploaded the local DICOM file to Orthanc - Instance ID={instance_id}")
    except Exception as e:
      raise Exception(f'Failed to upload the local DICOM file to Orthanc - {e}')
    
  # If the DICOM file is stored in the source Orthanc server
  else:
    instance_id = dicom_source.replace('orthanc://', '')
    
    # Check if a message was previously stored in the database, with "instructions" on how to 
    # process this Orthanc instance
    try:
      previous_msg = client.db_msg.get(instance_id)
      if previous_msg != None:
        if 'Source' in previous_msg:
          previous_msg['OriginalSource'] = previous_msg['Source']
        previous_msg['Source'] = dicom_source
        msg = previous_msg
        logger.debug(f"Modified the message: {json.dumps(previous_msg)}")
    except Exception as e:
      raise Exception(f'Failed to check if a related message was previously stored in the database - {e}')
    
    # Skip the message if it has an attribute `Skip=True` and delete the associated Orthanc 
    # instance, because it was uploaded by the de-identifier
    if 'Skip' in msg and msg['Skip'] == True:
      logger.info(f'Skipping the Orthanc instance (Skip=True)')
      client.src_orthanc.delete_instance(instance_id)
      
    # Otherwise, process the message and delete the original DICOM file in Orthanc unless 
    # we need to preserve them
    else:
      process_new_dicom_orthanc(instance_id, msg)
      if env.preserve_files.lower() == 'no':
        client.src_orthanc.delete_instance(instance_id)