-
Notifications
You must be signed in to change notification settings - Fork 1
/
modLibrary.vb
194 lines (187 loc) · 9.25 KB
/
modLibrary.vb
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
183
184
185
186
187
188
189
190
191
192
193
194
Module modLibrary
''' <summary>
''' Retrieves an ebook from the repository and emails it to the user
''' </summary>
''' <param name="strBarcode">Material barcode</param>
''' <returns>Temp: Type of material</returns>
Function CheckOutEbook(ByVal strBarcode As String, ByVal strRequestor As String)
If My.Settings.Library_Enable = True Then
If IsNumeric(strBarcode) = True Then
'File Selection Section
Dim SelectedFile As System.IO.FileInfo
Dim SelectedFolder As New System.IO.DirectoryInfo(My.Settings.Library_Repository & strBarcode)
If SelectedFolder.Exists Then
Dim SelectedFilesInFolder As String()
SelectedFilesInFolder = IO.Directory.GetFiles(SelectedFolder.FullName, "*.pdf", IO.SearchOption.TopDirectoryOnly)
If SelectedFilesInFolder.Count = 1 Then
SelectedFile = New System.IO.FileInfo(SelectedFilesInFolder(0))
ElseIf SelectedFilesInFolder.Count > 1 Then
My.Application.Log.WriteEntry("Multiple PDFs found in folder, remote checkout is not supported")
SelectedFile = Nothing
Else
SelectedFilesInFolder = IO.Directory.GetFiles(SelectedFolder.FullName, "*.epub", IO.SearchOption.TopDirectoryOnly)
If SelectedFilesInFolder.Count = 1 Then
SelectedFile = New System.IO.FileInfo(SelectedFilesInFolder(0))
ElseIf SelectedFilesInFolder.Count > 1 Then
My.Application.Log.WriteEntry("Multiple EPUBs found in folder, remote checkout is not supported")
SelectedFile = Nothing
Else
My.Application.Log.WriteEntry("Folder contains neither PDFs nor EPUBs, remote checkout is not supported")
SelectedFile = Nothing
End If
End If
Else
SelectedFile = New System.IO.FileInfo(My.Settings.Library_Repository & strBarcode & ".pdf")
If SelectedFile.Exists = False Then
SelectedFile = New System.IO.FileInfo(My.Settings.Library_Repository & strBarcode & ".epub")
If SelectedFile.Exists = False Then
My.Application.Log.WriteEntry("No folders, PDFs, or EPUBs were found, remote checkout is not supported")
SelectedFile = Nothing
End If
End If
End If
'File Transmission Section
If SelectedFile IsNot Nothing Then
My.Application.Log.WriteEntry("Requested file for barcode " & strBarcode & " found at " & SelectedFile.FullName & ". Size is " & SelectedFile.Length & " bytes.")
If SelectedFile.Length > (5 * 1024 * 1024) Then
Return "Requested file is too large to send" 'TODO: Support compressing larger files
Else
modMail.Send("Your library materials", "Thank you for checking out barcode " & strBarcode, SelectedFile.FullName, strRequestor)
Return "Requested file sent"
End If
Else
Return "Acceptable file to return was not found"
End If
Else
Return "Invalid barcode format"
End If
Else
My.Application.Log.WriteEntry("Library module is disabled")
Return "Disabled"
End If
End Function
''' <summary>
''' Strips the hyphens from an ISBN number, determines if it is ISBN10 or ISBN13, and then returns the converted value.
''' </summary>
''' <param name="strInputISBN">ISBN to convert</param>
''' <returns>ISBN converted to other format</returns>
Function ConvertISBN(ByVal strInputISBN As String) As String
strInputISBN = Replace(strInputISBN, "-", "")
If strInputISBN.Length = 10 Then
Return GetISBN13(strInputISBN)
ElseIf strInputISBN.Length = 13 Then
Return GetISBN10(strInputISBN)
Else
My.Application.Log.WriteEntry("Malformed ISBN entered", TraceEventType.Warning)
Return "BAD ISBN"
End If
End Function
Function Disable() As String
Unload()
My.Settings.Library_Enable = False
My.Application.Log.WriteEntry("Library module is disabled")
Return "Library module disabled"
End Function
Function Enable() As String
My.Settings.Library_Enable = True
My.Application.Log.WriteEntry("Library module is enabled")
Load()
Return "Library module enabled"
End Function
''' <summary>
''' Converts a 13-digit ISBN to a 10-digit ISBN.
''' </summary>
''' <param name="strISBN13">ISBN13 to convert</param>
''' <returns>ISBN10</returns>
Function GetISBN10(ByVal strISBN13 As String) As String
' Credit to jmaddalone at https://snipplr.com/view/5127/convert-isbn-13-to-10/
If strISBN13.Substring(0, 3) <> "978" Then
Return "NO ISBN10"
Else
Dim a As Integer
Dim b As Integer
Dim c As Integer
Dim d As Integer
Dim e As Integer
Dim f As Integer
Dim g As Integer
Dim h As Integer
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim l As Integer
Dim m As Integer
Dim n As Integer
Dim o As Object
Dim n2 As Integer
Dim isbnarr(12)
For i = 0 To 12
isbnarr(i) = CInt(Mid(strISBN13, i + 1, 1))
Next
a = isbnarr(0)
b = isbnarr(1)
c = isbnarr(2)
d = isbnarr(3)
e = isbnarr(4)
f = isbnarr(5)
g = isbnarr(6)
h = isbnarr(7)
i = isbnarr(8)
j = isbnarr(9)
k = isbnarr(10)
l = isbnarr(11)
m = isbnarr(12)
n = (d * 10) + (9 * e) + (8 * f) + (7 * g) + (6 * h) + (5 * i) + (4 * j) + (3 * k) + (2 * l)
n2 = Int((n / 11) + 1)
o = (11 * n2) - n
If o = 10 Then
o = "X"
ElseIf o = 11 Then
o = 0
End If
Return CStr(d & e & f & g & h & i & j & k & l & o)
End If
End Function
''' <summary>
''' Converts a 10-digit ISBN to a 13-digit ISBN.
''' </summary>
''' <param name="strISBN10">ISBN10 to convert</param>
''' <returns>ISBN13</returns>
Function GetISBN13(ByVal strISBN10 As String) As String
' Credit to Mehrdad, Asghari at https://www.codeproject.com/tips/75999/%2fTips%2f75999%2fConvert-ISBN10-To-ISBN-13
Dim isbn10 As String = "978" & strISBN10.Substring(0, 9)
Dim isbn10_1 As Integer = Convert.ToInt32(isbn10.Substring(0, 1))
Dim isbn10_2 As Integer = Convert.ToInt32(Convert.ToInt32(isbn10.Substring(1, 1)) * 3)
Dim isbn10_3 As Integer = Convert.ToInt32(isbn10.Substring(2, 1))
Dim isbn10_4 As Integer = Convert.ToInt32(Convert.ToInt32(isbn10.Substring(3, 1)) * 3)
Dim isbn10_5 As Integer = Convert.ToInt32(isbn10.Substring(4, 1))
Dim isbn10_6 As Integer = Convert.ToInt32(Convert.ToInt32(isbn10.Substring(5, 1)) * 3)
Dim isbn10_7 As Integer = Convert.ToInt32(isbn10.Substring(6, 1))
Dim isbn10_8 As Integer = Convert.ToInt32(Convert.ToInt32(isbn10.Substring(7, 1)) * 3)
Dim isbn10_9 As Integer = Convert.ToInt32(isbn10.Substring(8, 1))
Dim isbn10_10 As Integer = Convert.ToInt32(Convert.ToInt32(isbn10.Substring(9, 1)) * 3)
Dim isbn10_11 As Integer = Convert.ToInt32(isbn10.Substring(10, 1))
Dim isbn10_12 As Integer = Convert.ToInt32(Convert.ToInt32(isbn10.Substring(11, 1)) * 3)
Dim k As Integer = (isbn10_1 + isbn10_2 + isbn10_3 + isbn10_4 + isbn10_5 + isbn10_6 + isbn10_7 + isbn10_8 + isbn10_9 + isbn10_10 + isbn10_11 + isbn10_12)
Dim checkdigit As Integer = 10 - ((isbn10_1 + isbn10_2 + isbn10_3 + isbn10_4 + isbn10_5 + isbn10_6 + isbn10_7 + isbn10_8 + isbn10_9 + isbn10_10 + isbn10_11 + isbn10_12) Mod 10)
If checkdigit = 10 Then checkdigit = 0
Return isbn10 & checkdigit.ToString()
End Function
Function Load() As String
If My.Settings.Library_Enable = True Then
My.Application.Log.WriteEntry("Loading library module")
If My.Settings.Library_Repository = "" Then
My.Application.Log.WriteEntry("No library location set, asking for it")
My.Settings.Library_Repository = InputBox("Enter the folder location of your library data.", "Library Repository")
End If
Return "Library module loaded"
Else
My.Application.Log.WriteEntry("Library module is disabled, module not loaded")
Return "Library module is disabled, module not loaded"
End If
End Function
Function Unload() As String
My.Application.Log.WriteEntry("Unloading library module")
Return "Library module unloaded"
End Function
End Module