Ejemplo n.º 1
0
        public void Add(ImageItem item, bool forceRetry)
        {
            if (item.PostItemType == PostItemType.Thumbnail)
            {
                lock (ThumbnailItems)
                {
                    ImageItem _tmp = null;

                    foreach (ImageItem _item in ThumbnailItems)
                    {
                        if (_item.ThumbnailPath == item.ThumbnailPath)
                        {
                            _tmp = _item;
                            break;
                        }
                    }

                    if (_tmp != null)
                    {
                        ThumbnailItems.Remove(_tmp);
                    }

                    ThumbnailItems.Insert(0, item);
                }
            }
            else
            {
                lock (PhotoItems)
                {
                    ImageItem _tmp = null;

                    foreach (ImageItem _item in PhotoItems)
                    {
                        if (_item.PostItemType == PostItemType.Origin)
                        {
                            if (_item.LocalFilePath_Origin == item.LocalFilePath_Origin)
                            {
                                _tmp = _item;
                                break;
                            }
                        }

                        if (_item.PostItemType == PostItemType.Medium)
                        {
                            if (_item.LocalFilePath_Medium == item.LocalFilePath_Medium)
                            {
                                _tmp = _item;
                                break;
                            }
                        }
                    }

                    if (_tmp != null)
                    {
                        PhotoItems.Remove(_tmp);
                    }

                    PhotoItems.Insert(0, item);
                }
            }
        }
Ejemplo n.º 2
0
        private void DownloadThreadMethod(object state)
        {
            long   _count     = 0;
            string _localPath = string.Empty;
            string _url       = string.Empty;

            Thread.Sleep(3000);

            while (true)
            {
                ImageItem _item = null;

                Thread.Sleep(100);

                if ((ThumbnailItems.Count == 0) && (PhotoItems.Count == 0))
                {
                    Thread.Sleep(1000);
                    continue;
                }

                if ((_count++ % 3) != 2)
                {
                    if (ThumbnailItems.Count > 0)
                    {
                        _item = ThumbnailItems[0];
                    }
                }
                else
                {
                    if (PhotoItems.Count > 0)
                    {
                        _item = PhotoItems[0];
                    }
                }

                if (_item == null)
                {
                    continue;
                }

                switch (_item.PostItemType)
                {
                case PostItemType.Thumbnail:
                    _url       = _item.ThumbnailPath.Replace("[IP]", WService.HostIP);
                    _localPath = _item.LocalFilePath_Origin;
                    break;

                case PostItemType.Origin:
                    _url       = Main.Current.RT.StationMode ? _item.OriginPath : _item.CloudOriginPath;
                    _localPath = _item.LocalFilePath_Origin;
                    break;

                case PostItemType.Medium:
                    _url       = _item.MediumPath.Replace("[IP]", WService.HostIP);
                    _localPath = _item.LocalFilePath_Medium;
                    break;
                }

                if (File.Exists(_localPath))
                {
                    lock (PhotoItems)
                    {
                        if (PhotoItems.Contains(_item))
                        {
                            PhotoItems.Remove(_item);
                        }
                    }

                    continue;
                }

                bool _relpaceOriginToMedium = false;

                if (_item.PostItemType == PostItemType.Origin)
                {
                    if (File.Exists(_item.LocalFilePath_Medium))
                    {
                        if (!canGetOrigin(_item))
                        {
                            lock (PhotoItems)
                            {
                                PhotoItems.Remove(_item);

                                if (PhotoItems.Count == 0)
                                {
                                    PhotoItems.Insert(0, _item);
                                }
                                else
                                {
                                    PhotoItems.Insert(PhotoItems.Count - 1, _item);
                                }
                            }

                            continue;
                        }
                    }
                    else
                    {
                        _url       = _item.MediumPath.Replace("[IP]", WService.HostIP);
                        _localPath = _item.LocalFilePath_Medium;

                        _relpaceOriginToMedium = true;
                    }
                }

                try
                {
                    m_currentURL = _url;

                    WebRequest _wReq = WebRequest.Create(_url);
                    _wReq.Timeout = 10000;

                    WebResponse _wRep = _wReq.GetResponse();

                    Image _img = Image.FromStream(_wRep.GetResponseStream());

                    if (!File.Exists(_localPath))
                    {
                        _img.Save(_localPath);
                    }

                    _img = null;

                    s_logger.Trace("GetFile:" + _localPath);

                    if (_item.PostItemType == PostItemType.Thumbnail)
                    {
                        lock (ThumbnailItems)
                        {
                            if (ThumbnailItems.Contains(_item))
                            {
                                ThumbnailItems.Remove(_item);
                            }
                        }

                        if (ThumbnailEvent != null)
                        {
                            ThumbnailEvent(_item);
                        }
                    }
                    else
                    {
                        lock (PhotoItems)
                        {
                            if (_relpaceOriginToMedium)
                            {
                                PhotoItems.Remove(_item);

                                if (PhotoItems.Count == 0)
                                {
                                    PhotoItems.Insert(0, _item);
                                }
                                else
                                {
                                    PhotoItems.Insert(PhotoItems.Count - 1, _item);
                                }
                            }
                            else
                            {
                                if (PhotoItems.Contains(_item))
                                {
                                    PhotoItems.Remove(_item);
                                }
                            }
                        }

                        if (PhotoEvent != null)
                        {
                            PhotoEvent(_item);
                        }
                    }
                }
                catch (Exception _e)
                {
                    NLogUtility.Exception_Warn(s_logger, _e, "Download Image URL", m_currentURL);

                    try
                    {
                        _item.ErrorTry++;

                        if (_item.PostItemType == PostItemType.Thumbnail)
                        {
                            lock (ThumbnailItems)
                            {
                                if (ThumbnailItems.Contains(_item))
                                {
                                    ThumbnailItems.Remove(_item);

                                    if (_item.ErrorTry != ERROR_TRY)
                                    {
                                        if (ThumbnailItems.Count == 0)
                                        {
                                            ThumbnailItems.Insert(0, _item);
                                        }
                                        else
                                        {
                                            ThumbnailItems.Insert(ThumbnailItems.Count - 1, _item);
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            lock (PhotoItems)
                            {
                                if (_item.PostItemType == PostItemType.Origin)
                                {
                                    m_downlaodErrorOriginFiles.Add(_url, DateTime.Now);
                                }

                                if (PhotoItems.Contains(_item))
                                {
                                    PhotoItems.Remove(_item);

                                    if (_item.ErrorTry != ERROR_TRY)
                                    {
                                        if (PhotoItems.Count == 0)
                                        {
                                            PhotoItems.Insert(0, _item);
                                        }
                                        else
                                        {
                                            PhotoItems.Insert(PhotoItems.Count - 1, _item);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    catch
                    {
                    }
                }
            }
        }