Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- diff --git a/mediaportal/Core/guilib/DownloadedImage.cs b/mediaportal/Core/guilib/DownloadedImage.cs
- index e29e5a0..acaa310 100644
- --- a/mediaportal/Core/guilib/DownloadedImage.cs
- +++ b/mediaportal/Core/guilib/DownloadedImage.cs
- @@ -22,6 +22,7 @@ using System;
- using System.IO;
- using System.Net;
- using MediaPortal.Configuration;
- +using System.Text.RegularExpressions;
- namespace MediaPortal.GUI.Library
- {
- @@ -34,31 +35,56 @@ namespace MediaPortal.GUI.Library
- private string _url;
- private DateTime _dateDownloaded = DateTime.MinValue;
- private int _cacheMinutes = 60 * 30; //30minutes
- + private bool _shouldSave = false;
- - public DownloadedImage(string url)
- + public DownloadedImage(string url, bool shouldsave=false)
- {
- URL = url;
- int pos = url.LastIndexOf("/");
- +
- + if (shouldsave)
- + {
- + _shouldSave = true;
- + _cacheMinutes = 60 * 60 * 24 * 3; // 3 days
- + }
- _fileName = GetTempFileName();
- }
- private string GetTempFileName()
- {
- - int x = 0;
- - while (true)
- + Regex rgx = new Regex(@"[^\w]+", RegexOptions.IgnoreCase);
- + string tempURL = rgx.Replace(_url, "");
- + if (tempURL.Length > 100)
- + {
- + tempURL = tempURL.Substring(tempURL.Length - 100);
- + }
- + string filePrefix = "MPTemp-";
- + if (_shouldSave)
- {
- - string tempFile = Config.GetFile(Config.Dir.Thumbs, String.Format("MPTemp{0}.gif", x));
- - string tempFile2 = Config.GetFile(Config.Dir.Thumbs, String.Format("MPTemp{0}.jpg", x));
- - string tempFile3 = Config.GetFile(Config.Dir.Thumbs, String.Format("MPTemp{0}.bmp", x));
- - if (!File.Exists(tempFile) &&
- - !File.Exists(tempFile2) &&
- - !File.Exists(tempFile3))
- + filePrefix = @"Web\";
- + string thumbspath = Config.GetSubFolder(Config.Dir.Thumbs, @"Web\");
- + if (!Directory.Exists(thumbspath))
- {
- - return tempFile;
- + Directory.CreateDirectory(Config.GetSubFolder(Config.Dir.Thumbs, @"Web\"));
- }
- - ++x;
- }
- +
- + foreach (string ext in new string[] { "gif", "jpg", "png", "bmp" })
- + {
- + string tempFile = Config.GetFile(Config.Dir.Thumbs, String.Format("{0}{1}.{2}", filePrefix, tempURL, ext));
- + if (File.Exists(tempFile))
- + {
- + if (_dateDownloaded == DateTime.MinValue)
- + {
- + _dateDownloaded = DateTime.Now;
- + }
- + System.IO.File.SetLastWriteTimeUtc(tempFile, DateTime.UtcNow);
- + return tempFile;
- + }
- + }
- +
- + return Config.GetFile(Config.Dir.Thumbs, String.Format("{0}{1}.gif", filePrefix, tempURL));
- }
- @@ -85,7 +111,7 @@ namespace MediaPortal.GUI.Library
- get
- {
- TimeSpan ts = DateTime.Now - _dateDownloaded;
- - if (ts.TotalSeconds > CacheTime)
- + if (ts.TotalSeconds > CacheTime && (!_shouldSave || _dateDownloaded == DateTime.MinValue))
- {
- return true;
- }
- @@ -126,6 +152,10 @@ namespace MediaPortal.GUI.Library
- {
- extension = ".jpg";
- }
- + if (contentType.IndexOf("png") >= 0)
- + {
- + extension = ".png";
- + }
- if (contentType.IndexOf("bmp") >= 0)
- {
- extension = ".bmp";
- diff --git a/mediaportal/Core/guilib/GUIImage.cs b/mediaportal/Core/guilib/GUIImage.cs
- index 08a252f..2aae4a7 100644
- --- a/mediaportal/Core/guilib/GUIImage.cs
- +++ b/mediaportal/Core/guilib/GUIImage.cs
- @@ -82,7 +82,8 @@ namespace MediaPortal.GUI.Library
- [XMLSkin("texture", "flipY")] protected bool _flipY = false;
- [XMLSkin("texture", "diffuse")] protected string _diffuseFileName = "";
- [XMLSkin("texture", "overlay")] protected string _overlayFileName = "";
- - [XMLSkin("texture", "mask")] protected string _maskFileName = "";
- + [XMLSkin("texture", "mask")] protected string _maskFileName = "";
- + [XMLSkin("texture", "save")] protected bool _websave = false; // Hint from the skin to look/save this image locally if it is downloaded
- [XMLSkinElement("filtered")] protected bool _filterImage = true;
- [XMLSkinElement("align")] protected Alignment _imageAlignment = Alignment.ALIGN_LEFT;
- [XMLSkinElement("valign")] protected VAlignment _imageVAlignment = VAlignment.ALIGN_TOP;
- @@ -453,7 +454,24 @@ namespace MediaPortal.GUI.Library
- {
- get { return _imagePath; }
- set { _imagePath = value; }
- + }
- +
- + /// <summary>
- + /// Get/Set if the web image should be saved between MP sessions.
- + /// </summary>
- + public bool WebSave
- + {
- + get { return _websave; }
- + set
- + {
- + if (_websave != value)
- + {
- + _websave = value;
- + _reCalculate = true;
- + }
- + }
- }
- +
- /// <summary>
- /// Get the transparent color.
- @@ -714,7 +732,7 @@ namespace MediaPortal.GUI.Library
- }
- else
- {
- - frameCount = GUITextureManager.Load(fileName, m_dwColorKey, m_iRenderWidth, _textureHeight, _shouldCache);
- + frameCount = GUITextureManager.Load(fileName, m_dwColorKey, m_iRenderWidth, _textureHeight, _shouldCache, _websave);
- }
- if (frameCount == 0)
- diff --git a/mediaportal/Core/guilib/GUITextureManager.cs b/mediaportal/Core/guilib/GUITextureManager.cs
- index 45fdc68..1a81833 100644
- --- a/mediaportal/Core/guilib/GUITextureManager.cs
- +++ b/mediaportal/Core/guilib/GUITextureManager.cs
- @@ -88,6 +88,32 @@ namespace MediaPortal.GUI.Library
- {
- }
- }
- + }
- +
- +
- + files = null;
- +
- + try
- + {
- + files = Directory.GetFiles(Config.GetFolder(Config.Dir.Thumbs), @"Web\*.*");
- + }
- + catch { }
- +
- + if (files != null)
- + {
- + foreach (string file in files)
- + {
- + try
- + {
- + if ((DateTime.Now - File.GetLastWriteTime(file)).TotalDays >= 3) // Delete web cached files untouched for 3 or more days
- + {
- + File.Delete(file);
- + }
- + }
- + catch (Exception)
- + {
- + }
- + }
- }
- }
- }
- @@ -147,7 +173,7 @@ namespace MediaPortal.GUI.Library
- return result;
- }
- - private static string GetFileName(string fileName)
- + private static string GetFileName(string fileName, bool shouldsave = false)
- {
- if (fileName.Length == 0)
- {
- @@ -157,13 +183,13 @@ namespace MediaPortal.GUI.Library
- {
- return "";
- }
- - string lowerFileName = fileName.ToLowerInvariant().Trim();
- - if (lowerFileName.IndexOf(@"http:") >= 0)
- + string lowerFileName = fileName.ToLowerInvariant().Trim();
- + if (lowerFileName.IndexOf(@"http:") >= 0 || lowerFileName.IndexOf(@"https:") >= 0)
- {
- DownloadedImage image;
- if (!_cacheDownload.TryGetValue(lowerFileName, out image))
- {
- - image = new DownloadedImage(fileName);
- + image = new DownloadedImage(fileName, shouldsave);
- _cacheDownload[lowerFileName] = image;
- }
- @@ -190,9 +216,9 @@ namespace MediaPortal.GUI.Library
- return Load(fileNameOrg, lColorKey, iMaxWidth, iMaxHeight, false);
- }
- - public static int Load(string fileNameOrg, long lColorKey, int iMaxWidth, int iMaxHeight, bool persistent)
- + public static int Load(string fileNameOrg, long lColorKey, int iMaxWidth, int iMaxHeight, bool persistent, bool shouldsave=false)
- {
- - string fileName = GetFileName(fileNameOrg);
- + string fileName = GetFileName(fileNameOrg, shouldsave);
- string cacheKey = fileName.ToLowerInvariant();
- if (String.IsNullOrEmpty(fileName))
- {
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement