PageRenderTime 66ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Utilites/DownloadCache.vb

#
Visual Basic | 261 lines | 209 code | 43 blank | 9 comment | 0 complexity | 52103aa8dd1d309beb0a93a829d9b05a MD5 | raw file
Possible License(s): GPL-2.0
  1. Imports System.Security.Cryptography
  2. Imports System.Net
  3. Imports System.IO
  4. Imports System.IO.Compression
  5. Imports System.Text
  6. Imports System
  7. Imports System.Drawing
  8. Public Class DownloadCache
  9. Public Shared Property CacheFolder As String
  10. Public Shared Function GetCacheFileName(ByVal URL As String) As String
  11. Dim Buffer As Byte() = Utilities.ComputeHashValueToByte(URL)
  12. Dim Extention As String = URL.Split("?")(0)
  13. Extention = IO.Path.GetExtension(Extention)
  14. Dim Result As String = ""
  15. For Each Item As Byte In Buffer
  16. Result &= Conversion.Hex(Item)
  17. Next
  18. Return Result & Extention
  19. End Function
  20. Public Shared Function SaveImageToCacheAndPath(ByVal URL As String, Path As String, Optional ByVal ForceDownload As Boolean = False, _
  21. Optional ByVal resizeWidth As Integer = 0, Optional ByVal resizeHeight As Integer = 0, _
  22. Optional ByVal Overwrite As Boolean = True) As Boolean
  23. Dim CacheFileName As String = ""
  24. If Not SaveImageToCache(URL, Path, ForceDownload, CacheFileName) Then Return False
  25. Dim CachePath = IO.Path.Combine(CacheFolder, CacheFileName)
  26. If IfNotValidImage_Delete(CachePath) Then
  27. 'Resize cache image only if need to
  28. If Not (resizeWidth = 0 And resizeHeight = 0) Then CopyAndDownSizeImage(CachePath, CachePath, resizeWidth, resizeHeight)
  29. Utilities.EnsureFolderExists(Path)
  30. File.Copy(CachePath, Path, Overwrite)
  31. Else
  32. Return False
  33. End If
  34. Return True
  35. End Function
  36. Public Shared Function SaveImageToCacheAndPaths(ByVal URL As String, Paths As List(Of String), Optional ByVal ForceDownload As Boolean = False, _
  37. Optional ByVal resizeWidth As Integer = 0, Optional ByVal resizeHeight As Integer = 0, _
  38. Optional ByVal Overwrite As Boolean = True) As Boolean
  39. Dim verifiedPaths As New List(Of String)
  40. For each path in Paths
  41. If Not File.exists(path) OrElse Overwrite Then verifiedPaths.add(path)
  42. Next
  43. If verifiedPaths.Count = 0 Then Return True
  44. Dim CacheFileName = ""
  45. If Not SaveImageToCache(URL, verifiedPaths(0), ForceDownload, CacheFileName) Then Return False
  46. Dim CachePath = IO.Path.Combine(CacheFolder, CacheFileName)
  47. If IfNotValidImage_Delete(CachePath) Then
  48. 'Resize cache image only if need to
  49. If Not (resizeWidth = 0 And resizeHeight = 0) Then CopyAndDownSizeImage(CachePath, CachePath, resizeWidth, resizeHeight)
  50. For Each path In verifiedPaths
  51. Utilities.EnsureFolderExists(path)
  52. If Overwrite OrElse Not File.Exists(path) Then File.Copy(CachePath, path, Overwrite)
  53. Next
  54. Else
  55. Return False
  56. End If
  57. Return True
  58. End Function
  59. Public Shared Function IfNotValidImage_Delete(filename As String) As Boolean
  60. Dim ok As Boolean = True
  61. Try
  62. Dim testImage = new Drawing.Bitmap(filename)
  63. testImage.Dispose()
  64. Catch ex As Exception
  65. Try
  66. File.Delete(filename)
  67. ok = False
  68. Catch
  69. ok = False
  70. End Try
  71. Throw
  72. End Try
  73. Return ok
  74. End Function
  75. Public Shared Sub CopyAndDownSizeImage(ByVal src As String, ByVal dest As String, Optional ByVal resizeWidth As Integer = 0, Optional ByVal resizeHeight As Integer = 0)
  76. Try
  77. Dim img = Utilities.GetImage(src)
  78. Dim resized = False
  79. 'Down-size only
  80. If (resizeWidth <> 0 And img.Width > resizeWidth) Or img.Height > resizeHeight And Not (resizeWidth = 0 And resizeHeight = 0) Then
  81. 'Calc scaled height - width is passed in as zero for people pictures and posters.
  82. If resizeWidth = 0 Then
  83. resizeWidth = img.Width * resizeHeight / img.Height
  84. End If
  85. img = Utilities.ResizeImage(img, resizeWidth, resizeHeight)
  86. resized = True
  87. End If
  88. If resized Or src <> dest Then
  89. Utilities.SaveImage(img, dest)
  90. End If
  91. Catch
  92. End Try
  93. End Sub
  94. Public Shared Function DownloadFileAndCache(ByVal URL As String, Optional ByVal Path As String = "", _
  95. Optional ByVal ForceDownload As Boolean = False, _
  96. Optional ByVal resizeFanart As Integer = 0, _
  97. Optional ByVal retcachename As Boolean = False, _
  98. Optional ByRef strValue As String = "") As Boolean
  99. Dim CacheFileName As String = ""
  100. Dim returnCode As Boolean = SaveImageToCache(URL, Path, ForceDownload, CacheFileName)
  101. If returnCode Then
  102. Dim CachePath As String = IO.Path.Combine(CacheFolder, CacheFileName)
  103. If String.IsNullOrEmpty(Path) Then
  104. strValue = IO.File.ReadAllText(CachePath)
  105. Else
  106. If Not retcachename Then
  107. Utilities.copyImage(CachePath, Path, resizeFanart)
  108. Else
  109. strValue = CachePath
  110. End If
  111. End If
  112. End If
  113. DownloadFileAndCache = returnCode
  114. Return returncode
  115. End Function
  116. Public Shared Function SaveImageToCache(ByVal URL As String, ByVal Path As String, ByVal ForceDownload As Boolean, ByRef CacheFileName As String) As Boolean
  117. Dim returnCode As Boolean = True
  118. Dim CachePath As String = ""
  119. Try
  120. Utilities.EnsureFolderExists(CacheFolder)
  121. If URL = "" Then Return False
  122. CacheFileName = GetCacheFileName(URL)
  123. CachePath = IO.Path.Combine(CacheFolder, CacheFileName)
  124. If Not File.Exists(CachePath) OrElse ForceDownload Then
  125. 'Check to see if URL is actually a local file
  126. If Not URL.Contains("://") AndAlso File.Exists(URL) Then
  127. If CachePath <> URL Then
  128. If Utilities.SafeDeleteFile(CachePath) Then
  129. File.Copy(URL, CachePath)
  130. End If
  131. End If
  132. Return True
  133. End If
  134. If ForceDownload AndAlso File.Exists(CachePath) Then File.Delete(CachePath)
  135. Try
  136. Dim webReq As HttpWebRequest = DirectCast(WebRequest.Create(URL), HttpWebRequest)
  137. webReq.Proxy = Utilities.MyProxy
  138. webReq.AllowAutoRedirect = True
  139. webReq.AutomaticDecompression = DecompressionMethods.GZip Or DecompressionMethods.Deflate
  140. Using webResp As HttpWebResponse = webReq.GetResponse()
  141. Using responseStreamData As Stream = webResp.GetResponseStream()
  142. 'got a response - should probably put a Try...Catch in here for filesystem stuff, but I'll wing it for now.
  143. If String.IsNullOrEmpty(Path) Then
  144. IO.File.WriteAllText(CachePath, New StreamReader(responseStreamData, Encoding.UTF8).ReadToEnd)
  145. Else
  146. Utilities.SafeDeleteFile(CachePath)
  147. Using fileStream As New FileStream(CachePath, FileMode.OpenOrCreate, FileAccess.Write)
  148. Dim buffer(webResp.ContentLength) As Byte
  149. Dim bytesRead = responseStreamData.Read(buffer, 0, buffer.Length)
  150. While bytesRead > 0
  151. fileStream.Write(buffer, 0, bytesRead)
  152. bytesRead = responseStreamData.Read(buffer, 0, buffer.Length)
  153. End While
  154. End Using
  155. End If
  156. End Using
  157. End Using
  158. If (New IO.FileInfo(CachePath)).Length = 0 Then
  159. File.Delete(CachePath)
  160. returnCode = False
  161. End If
  162. Catch ex As WebException
  163. If ex.Message.Contains("could not be resolved") Then Return False : Exit Try
  164. Using errorResp As HttpWebResponse = DirectCast(ex.Response, HttpWebResponse)
  165. Using errorRespStream As Stream = errorResp.GetResponseStream()
  166. Dim errorText As String = New StreamReader(errorRespStream).ReadToEnd()
  167. 'Writing to TvLog! -> Poo -> To do anyone -> Raise event?
  168. returnCode = False
  169. 'Utilities.tvScraperLog &= String.Format("**** Scraper Error: Code {0} ****{3} {2}{3}", errorResp.StatusCode, vbCrLf)
  170. End Using
  171. End Using
  172. returnCode = False
  173. End Try
  174. End If
  175. Catch ex As Exception
  176. 'MsgBox(ex.Message.ToString & vbCrLf & "URL string =" & URL & vbCrLf & "cachefolder = " & CacheFolder & vbCrLf & "path = " & Path)
  177. returnCode = False
  178. End Try
  179. Return returnCode
  180. End Function
  181. Public Shared Function Savexmltopath(ByVal URL As String, ByVal Path As String, ByVal filename As String, ByVal ForceDownload As Boolean) As Boolean
  182. Dim returnCode As Boolean = True
  183. Dim Fullpath As String = Path & filename
  184. Try
  185. Utilities.EnsureFolderExists(Path)
  186. If URL = "" Then Return False
  187. If Not File.Exists(Fullpath) OrElse ForceDownload Then
  188. If ForceDownload AndAlso File.Exists(Fullpath) Then
  189. File.Delete(Fullpath)
  190. End If
  191. Try
  192. Dim webReq As HttpWebRequest = DirectCast(WebRequest.Create(URL), HttpWebRequest)
  193. webReq.Proxy = Utilities.MyProxy
  194. webReq.AllowAutoRedirect = True
  195. webReq.AutomaticDecompression = DecompressionMethods.GZip Or DecompressionMethods.Deflate
  196. Using webResp As HttpWebResponse = webReq.GetResponse()
  197. Using responseStreamData As Stream = webResp.GetResponseStream()
  198. IO.File.WriteAllText(Fullpath, New StreamReader(responseStreamData, Encoding.UTF8).ReadToEnd)
  199. End Using
  200. End Using
  201. Catch ex As WebException
  202. If ex.Message.Contains("could not be resolved") Then Return False : Exit Try
  203. Using errorResp As HttpWebResponse = DirectCast(ex.Response, HttpWebResponse)
  204. Using errorRespStream As Stream = errorResp.GetResponseStream()
  205. Dim errorText As String = New StreamReader(errorRespStream).ReadToEnd()
  206. returnCode = False
  207. End Using
  208. End Using
  209. returnCode = False
  210. End Try
  211. End If
  212. Catch ex As Exception
  213. MsgBox(ex.Message.ToString & vbCrLf & "URL string =" & URL & vbCrLf & "Filename = " & filename & vbCrLf & "path = " & Path)
  214. returnCode = False
  215. End Try
  216. Return returnCode
  217. End Function
  218. End Class