public string GetImagePath(string orgUrl) { string result = null; try { object o = _dbcon.ExecuteScalar(string.Format("select local_file from images where org_url = '{0}' limit 1", orgUrl.Replace("'", "''"))); if (o != null && o.GetType() != typeof(DBNull)) { result = System.IO.Path.Combine(new string[] { _imageFolder, IMG_SUBFOLDER, o as string }); } } catch (Exception e) { Core.ApplicationData.Instance.Logger.AddLog(this, e); } return(result); }
private bool InitDatabase() { CloseDatabase(); bool result = false; try { _dbcon = new DBConComSqlite(DatabaseFilename); object o = _dbcon.ExecuteScalar("SELECT name FROM sqlite_master WHERE type='table' AND name='images'"); if (o == null || o.GetType() == typeof(DBNull)) { _dbcon.ExecuteNonQuery("create table 'images' (org_url text, gccode text, local_file text)"); _dbcon.ExecuteNonQuery("create index idx_images on images (org_url)"); } result = true; } catch { } return(result); }
private void getImagesThreadMethod() { Framework.Data.Geocache gc = null; lock (_gcList) { if (_gcList.Count > 0) { gc = _gcList[0]; _gcList.RemoveAt(0); } } using (System.Net.WebClient wc = new System.Net.WebClient()) { string fnp = System.IO.Path.Combine(PluginSettings.Instance.ActiveDataPath, IMG_SUBFOLDER); bool grabOnlyNew = _grabOnlyNew; while (gc != null) { //todo: get images try { StringBuilder sb = new StringBuilder(); lock (_lockDBObject) { if (gc.ShortDescriptionInHtml && gc.ShortDescription != null) { sb.Append(gc.ShortDescription); } if (gc.LongDescriptionInHtml && gc.LongDescription != null) { sb.Append(gc.LongDescription); } } if (sb.Length > 0) { List <string> linkList = new List <string>(); Regex r = new Regex(@"</?\w+\s+[^>]*>", RegexOptions.Multiline); MatchCollection mc = r.Matches(sb.ToString()); foreach (Match m in mc) { string s = m.Value.Substring(1).Replace('\r', ' ').Replace('\n', ' ').Trim(); if (s.StartsWith("img ", StringComparison.OrdinalIgnoreCase)) { int pos = s.IndexOf(" src", StringComparison.OrdinalIgnoreCase); pos = s.IndexOfAny(new char[] { '\'', '"' }, pos); int pos2 = s.IndexOfAny(new char[] { '\'', '"' }, pos + 1); linkList.Add(s.Substring(pos + 1, pos2 - pos - 1)); } } List <Framework.Data.GeocacheImage> imgList = DataAccess.GetGeocacheImages(Core.GeocacheImages, gc.Code); if (imgList != null) { foreach (Framework.Data.GeocacheImage img in imgList) { if (!linkList.Contains(img.Url)) { linkList.Add(img.Url); } } } foreach (string link in linkList) { string fn = string.Format("{0}.jpg", Guid.NewGuid().ToString("N")); bool skipInsert = false; //if it fails, just ignore this image try { //check if link already is in database //if so, use this filename lock (_lockDBObject) { object o = _dbcon.ExecuteScalar(string.Format("select local_file from images where gccode='{0}' and org_url='{1}'", gc.Code.Replace("'", "''"), link.Replace("'", "''"))); if (o != null && o.GetType() != typeof(DBNull)) { fn = (string)o; skipInsert = true; } } if (grabOnlyNew && skipInsert) { if (System.IO.File.Exists(System.IO.Path.Combine(fnp, fn))) { continue; } } using (System.IO.TemporaryFile tmp = new System.IO.TemporaryFile(true)) { wc.DownloadFile(link, tmp.Path); using (System.Drawing.Image img = System.Drawing.Image.FromFile(tmp.Path)) { img.Save(System.IO.Path.Combine(fnp, fn), System.Drawing.Imaging.ImageFormat.Jpeg); if (!skipInsert) { lock (_lockDBObject) { _dbcon.ExecuteNonQuery(string.Format("insert into images (gccode, org_url, local_file) values ('{0}', '{1}', '{2}')", gc.Code.Replace("'", "''"), link.Replace("'", "''"), fn)); } } } } } catch { } } } } catch { } gc = null; lock (_gcList) { if (_gcList.Count > 0) { gc = _gcList[0]; _gcList.RemoveAt(0); } } } } }