コード例 #1
0
        public void InsertManual(string url, byte[] data)
        {
            ImageCacheEntry e;

            lock (_entries) {
                if (!_entries.TryGetValue(url, out e))
                {
                    e             = new ImageCacheEntry();
                    e.Url         = url;
                    _entries[url] = e;
                }
            }
            e.Updated = DateTime.Now;
            if (String.IsNullOrWhiteSpace(e.File))
            {
                string prefix = url.GetHashCode().ToString("x");
                int    count  = 0;
                do
                {
                    e.File = prefix + count.ToString();
                    count++;
                } while (_usedFiles.Contains(e.File));
                _usedFiles.Add(e.File);
            }
            System.IO.File.WriteAllBytes(System.IO.Path.Combine(_folder, e.File), data);
        }
コード例 #2
0
        private void TriggerDownload(string url, Guid modID)
        {
            ImageCacheEntry e;

            lock (_entries) {
                if (!_entries.TryGetValue(url, out e))
                {
                    e             = new ImageCacheEntry();
                    e.Url         = url;
                    _entries[url] = e;
                }
            }
            e.Updated = DateTime.Now;
            string file = e.File;

            if (String.IsNullOrEmpty(file))
            {
                string prefix = url.GetHashCode().ToString("x");
                int    count  = 0;
                do
                {
                    file = prefix + count.ToString();
                    count++;
                } while(_usedFiles.Contains(file));
                _usedFiles.Add(file);
            }
            Sys.Downloads.Download("iros://Url/" + url.Replace("://", "$"), System.IO.Path.Combine(_folder, file + ".tmp"), "Downloading preview image", new Install.InstallProcedureCallback(ae => {
                if (ae.Error == null)
                {
                    lock (_entries) {
                        e.File   = file;
                        string f = System.IO.Path.Combine(_folder, file);
                        if (System.IO.File.Exists(f))
                        {
                            System.IO.File.Delete(f);
                        }
                        System.IO.File.Move(System.IO.Path.Combine(_folder, file + ".tmp"), f);
                    }
                }
                else
                {
                    System.Diagnostics.Debug.WriteLine("ImageCache:Download error: " + ae.Error.ToString());
                }
                Sys.Ping(modID);
            }), null);
        }
コード例 #3
0
ファイル: ImageCache.cs プロジェクト: rodriada000/7h
        public void InsertManual(string url, byte[] data)
        {
            ImageCacheEntry e;

            lock (_entryLock) {
                if (!_entries.TryGetValue(url, out e))
                {
                    e             = new ImageCacheEntry();
                    e.Url         = url;
                    _entries[url] = e;
                }
            }

            e.Updated = DateTime.Now;

            if (String.IsNullOrWhiteSpace(e.File))
            {
                string prefix = url.GetHashCode().ToString("x");
                int    count  = 0;
                do
                {
                    e.File = prefix + count.ToString();
                    count++;
                } while (_usedFiles.Contains(e.File));
                _usedFiles.Add(e.File);
            }

            try
            {
                File.WriteAllBytes(Path.Combine(_folder, e.File), data);
            }
            catch (IOException ioex)
            {
                // IOException can be thrown if image is in-use at the time e.g. being previewed
                Sys.Message(new WMessage($"Failed to update image cache file: {ioex.Message}", WMessageLogLevel.LogOnly));
            }
            catch (Exception ex)
            {
                Sys.Message(new WMessage("Failed to update image cache file", WMessageLogLevel.LogOnly, ex));
            }
        }
コード例 #4
0
ファイル: ImageCache.cs プロジェクト: rodriada000/7h
        private void TriggerDownload(string url, Guid modID)
        {
            ImageCacheEntry e;

            lock (_entryLock)
            {
                if (!_entries.TryGetValue(url, out e))
                {
                    e             = new ImageCacheEntry();
                    e.Url         = url;
                    _entries[url] = e;
                }
            }

            e.Updated = DateTime.Now;
            string file = e.File;

            if (string.IsNullOrWhiteSpace(file))
            {
                file = GetCacheFileName(url);
            }

            string pathToTempDownload = Path.Combine(_folder, file + ".tmp");

            DownloadItem download = new DownloadItem()
            {
                Links = new List <string> {
                    LocationUtil.FormatHttpUrl(url)
                },
                SaveFilePath = pathToTempDownload,
                ItemName     = "Downloading preview image",
                Category     = DownloadCategory.Image
            };

            download.IProc = new Install.InstallProcedureCallback(ae =>
            {
                if (ae.Error == null)
                {
                    lock (_entryLock)
                    {
                        e.File = file;
                    }

                    try
                    {
                        try
                        {
                            // delete existing cache file and rename .tmp file to match cache file name
                            string f = Path.Combine(_folder, file);

                            if (File.Exists(f))
                            {
                                File.Delete(f);
                            }

                            File.Move(pathToTempDownload, f);
                        }
                        catch (IOException)
                        {
                            // this happens while the file is in-use. the file will be renamed and image cache entry updated to point to new file instead
                            file = GetCacheFileName(url);
                            File.Move(pathToTempDownload, file);
                            e.File = file;
                        }

                        Sys.PingInfoChange(modID);
                    }
                    catch (Exception ex)
                    {
                        Sys.Message(new WMessage("failed to get preview image", WMessageLogLevel.Error, ex));
                    }
                }
                else
                {
                    Sys.Message(new WMessage("ImageCache Download error: " + ae.Error.ToString(), WMessageLogLevel.LogOnly, ae.Error));
                }
            });

            Sys.Downloads.AddToDownloadQueue(download);
        }