Advertisement
Guest User

Untitled

a guest
Jan 9th, 2017
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 7.82 KB | None | 0 0
  1. diff --git a/mediaportal/Core/guilib/DownloadedImage.cs b/mediaportal/Core/guilib/DownloadedImage.cs
  2. index e29e5a0..acaa310 100644
  3. --- a/mediaportal/Core/guilib/DownloadedImage.cs
  4. +++ b/mediaportal/Core/guilib/DownloadedImage.cs
  5. @@ -22,6 +22,7 @@ using System;
  6.  using System.IO;
  7.  using System.Net;
  8.  using MediaPortal.Configuration;
  9. +using System.Text.RegularExpressions;
  10.  
  11.  namespace MediaPortal.GUI.Library
  12.  {
  13. @@ -34,31 +35,56 @@ namespace MediaPortal.GUI.Library
  14.      private string _url;
  15.      private DateTime _dateDownloaded = DateTime.MinValue;
  16.      private int _cacheMinutes = 60 * 30; //30minutes
  17. +    private bool _shouldSave = false;
  18.  
  19. -    public DownloadedImage(string url)
  20. +    public DownloadedImage(string url, bool shouldsave=false)
  21.      {
  22.        URL = url;
  23.        int pos = url.LastIndexOf("/");
  24. +      
  25. +      if (shouldsave)
  26. +      {
  27. +        _shouldSave = true;
  28. +        _cacheMinutes = 60 * 60 * 24 * 3; // 3 days
  29. +      }
  30.  
  31.        _fileName = GetTempFileName();
  32.      }
  33.  
  34.      private string GetTempFileName()
  35.      {
  36. -      int x = 0;
  37. -      while (true)
  38. +      Regex rgx = new Regex(@"[^\w]+", RegexOptions.IgnoreCase);
  39. +      string tempURL = rgx.Replace(_url, "");
  40. +      if (tempURL.Length > 100)
  41. +      {
  42. +        tempURL = tempURL.Substring(tempURL.Length - 100);
  43. +      }
  44. +      string filePrefix = "MPTemp-";
  45. +      if (_shouldSave)
  46.        {
  47. -        string tempFile = Config.GetFile(Config.Dir.Thumbs, String.Format("MPTemp{0}.gif", x));
  48. -        string tempFile2 = Config.GetFile(Config.Dir.Thumbs, String.Format("MPTemp{0}.jpg", x));
  49. -        string tempFile3 = Config.GetFile(Config.Dir.Thumbs, String.Format("MPTemp{0}.bmp", x));
  50. -        if (!File.Exists(tempFile) &&
  51. -            !File.Exists(tempFile2) &&
  52. -            !File.Exists(tempFile3))
  53. +        filePrefix = @"Web\";
  54. +        string thumbspath = Config.GetSubFolder(Config.Dir.Thumbs, @"Web\");
  55. +        if (!Directory.Exists(thumbspath))
  56.          {
  57. -          return tempFile;
  58. +          Directory.CreateDirectory(Config.GetSubFolder(Config.Dir.Thumbs, @"Web\"));
  59.          }
  60. -        ++x;
  61.        }
  62. +
  63. +      foreach (string ext in new string[] { "gif", "jpg", "png", "bmp" })
  64. +      {
  65. +        string tempFile = Config.GetFile(Config.Dir.Thumbs, String.Format("{0}{1}.{2}", filePrefix, tempURL, ext));
  66. +        if (File.Exists(tempFile))
  67. +        {
  68. +          if (_dateDownloaded == DateTime.MinValue)
  69. +          {
  70. +            _dateDownloaded = DateTime.Now;
  71. +          }
  72. +          System.IO.File.SetLastWriteTimeUtc(tempFile, DateTime.UtcNow);
  73. +          return tempFile;
  74. +        }
  75. +      }
  76. +
  77. +      return Config.GetFile(Config.Dir.Thumbs, String.Format("{0}{1}.gif", filePrefix, tempURL));
  78.      }
  79.  
  80.  
  81. @@ -85,7 +111,7 @@ namespace MediaPortal.GUI.Library
  82.        get
  83.        {
  84.          TimeSpan ts = DateTime.Now - _dateDownloaded;
  85. -        if (ts.TotalSeconds > CacheTime)
  86. +        if (ts.TotalSeconds > CacheTime && (!_shouldSave || _dateDownloaded == DateTime.MinValue))
  87.          {
  88.            return true;
  89.          }
  90. @@ -126,6 +152,10 @@ namespace MediaPortal.GUI.Library
  91.              {
  92.                extension = ".jpg";
  93.              }
  94. +            if (contentType.IndexOf("png") >= 0)
  95. +            {
  96. +              extension = ".png";
  97. +            }
  98.              if (contentType.IndexOf("bmp") >= 0)
  99.              {
  100.                extension = ".bmp";
  101. diff --git a/mediaportal/Core/guilib/GUIImage.cs b/mediaportal/Core/guilib/GUIImage.cs
  102. index 08a252f..2aae4a7 100644
  103. --- a/mediaportal/Core/guilib/GUIImage.cs
  104. +++ b/mediaportal/Core/guilib/GUIImage.cs
  105. @@ -82,7 +82,8 @@ namespace MediaPortal.GUI.Library
  106.      [XMLSkin("texture", "flipY")] protected bool _flipY = false;
  107.      [XMLSkin("texture", "diffuse")] protected string _diffuseFileName = "";
  108.      [XMLSkin("texture", "overlay")] protected string _overlayFileName = "";
  109. -    [XMLSkin("texture", "mask")] protected string _maskFileName = "";
  110. +    [XMLSkin("texture", "mask")] protected string _maskFileName = "";
  111. +    [XMLSkin("texture", "save")] protected bool _websave = false; // Hint from the skin to look/save this image locally if it is downloaded
  112.      [XMLSkinElement("filtered")] protected bool _filterImage = true;
  113.      [XMLSkinElement("align")] protected Alignment _imageAlignment = Alignment.ALIGN_LEFT;
  114.      [XMLSkinElement("valign")] protected VAlignment _imageVAlignment = VAlignment.ALIGN_TOP;
  115. @@ -453,7 +454,24 @@ namespace MediaPortal.GUI.Library
  116.      {
  117.        get { return _imagePath; }
  118.        set { _imagePath = value; }
  119. +    }
  120. +
  121. +    /// <summary>
  122. +    /// Get/Set if the web image should be saved between MP sessions.
  123. +    /// </summary>
  124. +    public bool WebSave
  125. +    {
  126. +      get { return _websave; }
  127. +      set
  128. +      {
  129. +        if (_websave != value)
  130. +        {
  131. +          _websave = value;
  132. +          _reCalculate = true;
  133. +        }
  134. +      }
  135.      }
  136. +  
  137.  
  138.      /// <summary>
  139.      /// Get the transparent color.
  140. @@ -714,7 +732,7 @@ namespace MediaPortal.GUI.Library
  141.          }
  142.          else
  143.          {
  144. -          frameCount = GUITextureManager.Load(fileName, m_dwColorKey, m_iRenderWidth, _textureHeight, _shouldCache);
  145. +          frameCount = GUITextureManager.Load(fileName, m_dwColorKey, m_iRenderWidth, _textureHeight, _shouldCache, _websave);
  146.          }
  147.  
  148.          if (frameCount == 0)
  149. diff --git a/mediaportal/Core/guilib/GUITextureManager.cs b/mediaportal/Core/guilib/GUITextureManager.cs
  150. index 45fdc68..1a81833 100644
  151. --- a/mediaportal/Core/guilib/GUITextureManager.cs
  152. +++ b/mediaportal/Core/guilib/GUITextureManager.cs
  153. @@ -88,6 +88,32 @@ namespace MediaPortal.GUI.Library
  154.              {
  155.              }
  156.            }
  157. +        }
  158. +
  159. +        
  160. +        files = null;
  161. +
  162. +        try
  163. +        {
  164. +          files = Directory.GetFiles(Config.GetFolder(Config.Dir.Thumbs), @"Web\*.*");
  165. +        }
  166. +        catch { }
  167. +
  168. +        if (files != null)
  169. +        {
  170. +          foreach (string file in files)
  171. +          {
  172. +            try
  173. +            {
  174. +              if ((DateTime.Now - File.GetLastWriteTime(file)).TotalDays >= 3) // Delete web cached files untouched for 3 or more days
  175. +              {
  176. +                File.Delete(file);
  177. +              }
  178. +            }
  179. +            catch (Exception)
  180. +            {
  181. +            }
  182. +          }
  183.          }
  184.        }
  185.      }
  186. @@ -147,7 +173,7 @@ namespace MediaPortal.GUI.Library
  187.        return result;
  188.      }
  189.  
  190. -    private static string GetFileName(string fileName)
  191. +    private static string GetFileName(string fileName, bool shouldsave = false)
  192.      {
  193.        if (fileName.Length == 0)
  194.        {
  195. @@ -157,13 +183,13 @@ namespace MediaPortal.GUI.Library
  196.        {
  197.          return "";
  198.        }
  199. -      string lowerFileName = fileName.ToLowerInvariant().Trim();
  200. -      if (lowerFileName.IndexOf(@"http:") >= 0)
  201. +      string lowerFileName = fileName.ToLowerInvariant().Trim();
  202. +      if (lowerFileName.IndexOf(@"http:") >= 0 || lowerFileName.IndexOf(@"https:") >= 0)
  203.        {
  204.          DownloadedImage image;
  205.          if (!_cacheDownload.TryGetValue(lowerFileName, out image))
  206.          {
  207. -          image = new DownloadedImage(fileName);
  208. +          image = new DownloadedImage(fileName, shouldsave);
  209.            _cacheDownload[lowerFileName] = image;
  210.          }
  211.  
  212. @@ -190,9 +216,9 @@ namespace MediaPortal.GUI.Library
  213.        return Load(fileNameOrg, lColorKey, iMaxWidth, iMaxHeight, false);
  214.      }
  215.  
  216. -    public static int Load(string fileNameOrg, long lColorKey, int iMaxWidth, int iMaxHeight, bool persistent)
  217. +    public static int Load(string fileNameOrg, long lColorKey, int iMaxWidth, int iMaxHeight, bool persistent, bool shouldsave=false)
  218.      {
  219. -      string fileName = GetFileName(fileNameOrg);
  220. +      string fileName = GetFileName(fileNameOrg, shouldsave);
  221.        string cacheKey = fileName.ToLowerInvariant();
  222.        if (String.IsNullOrEmpty(fileName))
  223.        {
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement