-
Notifications
You must be signed in to change notification settings - Fork 2
/
doc-downloader.functions.ps1
183 lines (163 loc) · 6.13 KB
/
doc-downloader.functions.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#----------------------------------------------------------------------------------------------------------
# Helper Functions
#----------------------------------------------------------------------------------------------------------
function CreateLogFile
{param([string]$Dir)
$CurrentTime = Get-Date -Format yyyy-MM-dd
$filename = ("Eleos-" + ($CurrentTime + ".log"))
$filepath = Join-Path -Path $Dir -ChildPath $filename
return $filepath
}
function WriteToLog
{param([string]$TextToWrite, [string]$file)
$powershellVersion = (Get-Host).Version.Major
if($powershellVersion -ge 7){
$TextToWrite | Out-File -FilePath $file -Append
}
else {
$TextToWrite | Out-File $file -Append
}
}
function CheckDirectory
{param([string]$Dir)
if(-not (Test-Path $Dir)){
new-item $Dir -itemtype directory
}
}
function MakeHttpGetCall
{param([string]$URI, [hashtable]$HEADERS, [string]$LOG_FILE)
$ProgressPreference = 'SilentlyContinue'
$powershellVersion = (Get-Host).Version.Major
if($powershellVersion -ge 7){
$response = Invoke-WebRequest -Uri $URI -Headers $HEADERS -MaximumRedirection 0 -ErrorAction SilentlyContinue -ErrorVariable $ProcessError -SkipHttpErrorCheck
}
else {
$response = Invoke-WebRequest -Uri $URI -Headers $HEADERS -MaximumRedirection 0 -ErrorAction SilentlyContinue -ErrorVariable $ProcessError
}
#$ProgressPreference = 'Continue'
if($ProcessError){
WriteToLog $ProcessError $LOG_FILE
}
return $response
}
function MakeHttpDeleteCall
{param([string]$URI, [hashtable]$HEADERS, [string]$LOG_FILE)
try{
$ProgressPreference = 'SilentlyContinue'
$powershellVersion = (Get-Host).Version.Major
if($powershellVersion -ge 7){
$response = Invoke-WebRequest -Uri $URI -Headers $HEADERS -Method Delete -SkipHttpErrorCheck -ErrorAction SilentlyContinue
}
else {
$response = Invoke-WebRequest -Uri $URI -Headers $HEADERS -Method Delete -ErrorAction SilentlyContinue
}
return $response
}
catch{
return $null
}
}
function DownloadFile
{param([string]$URI, [string]$FileName, [string]$OutFilePath, [string]$LOG_FILE)
try {
$response = Invoke-WebRequest -Uri $URI -OutFile $OutFilePath/$FileName
WriteToLog ("File " + $FileName + " downloaded successfully to " + $OutFilePath) $LOG_FILE
} catch {
if($_.Exception.Response.StatusCode.Value__.ToString() -like '4**') {
WriteToLog("Error while downloading document, status code: " + $_.Exception.Response.StatusCode.Value__.ToString() + ". The document may have already been purged, moving onto next document...") $LOG_FILE
}
else {
throw $_
}
}
}
function ExponentialDeleteRetry
{param([string]$URI, [hashtable]$HEADERS, [string]$LOG_FILE)
$MAX_ATTEMPTS = 5
$attempts = 1
$MAX_BACKOFF = 16
$curr_backoff = 1
$Timer = [System.Diagnostics.Stopwatch]::StartNew()
for($i = $attempts; $i -lt $MAX_ATTEMPTS; $i++){
$response = MakeHttpDeleteCall $URI $HEADERS $LOG_FILE
If ($response){
return $response
}
else{
$offset = (Get-Random -Maximum 3000) / 1000
Start-Sleep -Seconds ($curr_backoff + $offset)
}
If($curr_backoff -lt $MAX_BACKOFF){
$curr_backoff = $curr_backoff * 2
}
}
$Timer.Stop()
WriteToLog ("Process failed after " + $MAX_ATTEMPTS.ToString() + ' attempts' +"`r`n" + 'Time:' + $Timer.Elapsed.ToString() + "s " + "`r`n") $LOG_FILE
return $null
}
function GetFilename
{param ([string] $downloadURI, [int32]$file_count, [string]$log_file)
try{
return ExtractFilenameFromHeader $downloadURI $file_count $log_file
}
catch {
WriteToLog ($_."An exception has occured: " + $_.Exception.Message + "`r`n") $log_file
return CreateDownloadFile $downloadURI $file_count
}
}
function ExtractFilenameFromHeader
{param ([string]$downloadURI, [int32]$file_count, [string]$log_file)
$WebRequest = [System.Net.WebRequest]::Create($downloadURI)
$WebRequest.Timeout = 10000
$Response = $WebRequest.GetResponse()
$dispositionHeader = $Response.Headers['Content-Disposition']
$disposition = [System.Net.Mime.ContentDisposition]::new($dispositionHeader)
$Response.Dispose()
$file = $disposition.FileName
return $file
}
function CreateDownloadFile
{param([string] $downloadURI, [int32] $file_count)
$CurrentDate = Get-Date -Format "yyyy-MM-dd_HH:mm"
$index = $downloadURI.IndexOf("filename");
$sub = $downloadURI.Substring($index, ($downloadURI.Length - $index))
$subList = $sub.Split("%")
foreach ($element in $subList) {
if($element -like "*.*") {
$index = $element.lastIndexOf(".")
$extension = $element.Substring($index, $element.Length - $index)
break
}
}
$filename = "Eleos-" + $CurrentDate + "_" + $file_count + $extension
return $filename
}
#----------------------------------------------------------------------------------------------------------
# Eleos API Consumer Functions
#----------------------------------------------------------------------------------------------------------
function GetNextDoc
{param([string]$URI, [hashtable]$HEADERS, [string]$LOG_FILE)
$response = MakeHttpGetCall $URI $HEADERS $LOG_FILE
return $response
}
function GetDocFromQueue
{param([string]$URI, [hashtable]$HEADERS, [string]$LOG_FILE)
$response = MakeHttpGetCall $URI $HEADERS $LOG_FILE
return $response
}
function RemoveDocFromQueue
{ param([string]$URI, [hashtable]$HEADERS, [string]$LOG_FILE)
$response = MakeHttpDeleteCall $URI $HEADERS $LOG_FILE
if($response -eq $null){
WriteToLog ("Error Removing Document. Trying again... `r`n") $LOG_FILE
$retry = ExponentialDeleteRetry $redirect $HEADERS $LOG_FILE
If($retry){
WriteToLog ("Document Removed from Queue with Status Code: " + $retry.StatusCode + "`r`n") $LOG_FILE
}
Else{
WriteToLog ("Error Removing Document after retry`r`n") $LOG_FILE
}
return $retry
}
return $response
}