Copy filedrop attachments to share

Discussion in 'ActionScripts' started by Aldo, Apr 15, 2022.

  1. Aldo

    Aldo New Member

    Joined:
    Jul 21, 2021
    Messages:
    4
    Likes Received:
    0
    I have a department that wants a copy of incoming attachments to go to a LiquidFiles share, not an external fileshare.
    I'm running LiquidFiles v3.6.7 on an Centos 7 VM in Azure. The script is using Powershell 7.2.2.

    Code:
    #!/usr/bin/env pwsh
    logger -p local0.info -t ActionScript_Copy_File_Script "ActionScript-Copy filedrop file to share: Begin"
    try {
        $message_object = Get-Content $args[0] | ConvertFrom-Json #convert the message into a PowerShell object
        if(!$message_object){throw "Error! No message data."} #if failed to get message data
        $attachments = $message_object.message.attachments #list of attachments
        switch ($message_object.message.recipients[0]) { # figure out share and folder from the filedrop recipient, defaults to root folder
            'branch01distro@email.com' {$share_id='CustomerFiles';$folder_id='01-Branch'}
            'branch02distro@email.com' {$share_id='CustomerFiles';$folder_id='02-Branch'}
            'branch03distro@email.com' {$share_id='CustomerFiles';$folder_id='03-Branch'}
            'branch04distro@email.com' {$share_id='CustomerFiles';$folder_id='04-Branch'}
            'branch05distro@email.com' {$share_id='CustomerFiles';$folder_id='05-Branch’}
            Default {$share_id='CustomerFiles';$folder_id = '947aaaa7-c51a-4214-8b9d-455aeaa3c30'}
        }
        $api_key = 'xxxxxxT1hl12xmzGeRxxxxxx' #api_key belongs to service account
        $api_password = ConvertTo-SecureString -String 'x' -AsPlainText #as per LF doc, password doesn't matter
        $api_credentials = [System.Management.Automation.PSCredential]::new($api_key,$api_password)
        ForEach ($attachment in $attachments) { #iterate through all attachments
            Try{
                logger -p local0.info -t ActionScript_Copy_File_Script "ActionScript-Copy filedrop file to share: Uploading $($attachment.filename) to share $share_id folder $folder_id"
                $response = $null
                $file_name = $message_object.message.subject + ' ' + $attachment.filename
                $file_bytes = Get-Content $attachment.system_file -Raw -AsByteStream -ErrorAction Stop #convert file to binary
                $api_request = @{ #api request as hashtable
                    'Method' = 'POST'
                    'Uri' = 'https://myLiquidFiles.myDomain.com/shares/{0}/folders/{1}/files/binary_upload?name={2}' -f $share_id,$folder_id,$file_name
                    'Authentication' = 'Basic'
                    'Credential' = $api_credentials
                    'ContentType' = 'application/json'
                    'Body' = $file_bytes
                    'Headers' = @{'Accept' = 'application/json'}
                    'ErrorAction' = 'Stop'
                }
                $response = Invoke-RestMethod @api_request
                if($response.file.processed){#upload success
                    logger -p local0.info -t ActionScript_Copy_File_Script "ActionScript-Copy filedrop file to share: Success to upload attachment $($attachment.filename) to share $share_id folder $folder_id"
                }
                else {Throw "upload response: {0}" -f ($response.file|ConvertTo-Json)}
            }
            catch{
                logger -p local0.info -t ActionScript_Copy_File_Script "ActionScript-Copy filedrop file to share: Failure to upload attachment $($attachment.filename) to share $share_id folder $folder_id! Error = $($error[0].Exception.Message)"
            }
        }
    }
    catch {
        logger -p local0.info -t ActionScript_Copy_File_Script "ActionScript-Copy filedrop file to share: Failure! Error = $($error[0].Exception.Message)"
    }
    logger -p local0.info -t ActionScript_Copy_File_Script "ActionScript-Copy filedrop file to share: End: Error count $($error.count)"
    Exit
     
    #1 Aldo, Apr 15, 2022
    Last edited: Apr 16, 2022

Share This Page