Esempio n. 1
0
        public void DownloadImage(ImageDownloadRequest req)
        {
            try
            {
                string fileName = GetFileName(req, false);
                BaseConfig.MyAnimeLog.Write("Image Path: " + fileName);

                int  entityID   = GetEntityID(req);
                bool fileExists = File.Exists(fileName);

                bool downloadImage = !fileExists || req.ForceDownload;

                if (downloadImage)
                {
                    lock (GetUniqueLock(req, false))
                    {
                        string tempName = Path.Combine(Utils.GetImagesTempFolder(), Path.GetFileName(fileName) ?? "");
                        if (File.Exists(tempName))
                        {
                            File.Delete(tempName);
                        }


                        OnImageDownloadEvent(new ImageDownloadEventArgs("", req, ImageDownloadEventType.Started));

                        if (LoadAndSaveImage(entityID, (int)req.ImageType, false, tempName))
                        {
                            MoveFile(fileExists, fileName, tempName);
                        }
                    }
                }


                // if the file is a tvdb fanart also get the thumbnail
                if (req.ImageType == ImageEntityType.TvDB_FanArt)
                {
                    fileName = GetFileName(req, true);
                    BaseConfig.MyAnimeLog.Write("Image Path: " + fileName);

                    entityID   = GetEntityID(req);
                    fileExists = File.Exists(fileName);

                    downloadImage = !fileExists || req.ForceDownload;

                    if (downloadImage)
                    {
                        lock (GetUniqueLock(req, true))
                        {
                            string tempName = Path.Combine(Utils.GetImagesTempFolder(), Path.GetFileName(fileName) ?? "");
                            if (File.Exists(tempName))
                            {
                                File.Delete(tempName);
                            }

                            OnImageDownloadEvent(new ImageDownloadEventArgs("", req, ImageDownloadEventType.Started));

                            if (LoadAndSaveImage(entityID, (int)req.ImageType, true, tempName))
                            {
                                MoveFile(fileExists, fileName, tempName);
                            }
                        }
                    }
                }
            }

            catch (Exception ex)
            {
                BaseConfig.MyAnimeLog.Write(ex.ToString());
            }
        }
Esempio n. 2
0
        public void DownloadImage(ImageDownloadRequest req)
        {
            try
            {
                lock (downloadsLock)
                {
                    string fileName   = GetFileName(req, false);
                    string entityID   = GetEntityID(req);
                    bool   fileExists = File.Exists(fileName);

                    bool downloadImage = !fileExists || req.ForceDownload;

                    if (downloadImage)
                    {
                        string tempName = Path.Combine(Utils.GetImagesTempFolder(), Path.GetFileName(fileName) ?? "");
                        if (File.Exists(tempName))
                        {
                            File.Delete(tempName);
                        }


                        OnImageDownloadEvent(new ImageDownloadEventArgs("", req, ImageDownloadEventType.Started));
                        if (fileExists)
                        {
                            File.Delete(fileName);
                        }

                        Stream imageArray = null;
                        try
                        {
                            imageArray = VM_ShokoServer.Instance.ShokoImages.GetImage(int.Parse(entityID), (int)req.ImageType, false);
                        }
                        catch
                        {
                            // ignored
                        }

                        if (imageArray == null)
                        {
                            return;
                        }

                        FileStream fw = File.OpenWrite(tempName);
                        imageArray.CopyTo(fw);
                        fw.Close();
                        imageArray.Close();

                        // move the file to it's final location
                        string fullPath = Path.GetDirectoryName(fileName);
                        if (fullPath != null)
                        {
                            if (!Directory.Exists(fullPath))
                            {
                                Directory.CreateDirectory(fullPath);
                            }
                        }

                        // move the file to it's final location
                        File.Move(tempName, fileName);
                    }


                    // if the file is a tvdb fanart also get the thumbnail
                    if (req.ImageType == ImageEntityType.TvDB_FanArt)
                    {
                        fileName   = GetFileName(req, true);
                        entityID   = GetEntityID(req);
                        fileExists = File.Exists(fileName);

                        downloadImage = !fileExists || req.ForceDownload;

                        if (downloadImage)
                        {
                            string tempName = Path.Combine(Utils.GetImagesTempFolder(), Path.GetFileName(fileName) ?? "");
                            if (File.Exists(tempName))
                            {
                                File.Delete(tempName);
                            }

                            OnImageDownloadEvent(new ImageDownloadEventArgs("", req, ImageDownloadEventType.Started));
                            if (fileExists)
                            {
                                File.Delete(fileName);
                            }

                            Stream imageArray = null;
                            try
                            {
                                imageArray = VM_ShokoServer.Instance.ShokoImages.GetImage(int.Parse(entityID), (int)req.ImageType, true);
                            }
                            catch
                            {
                                // ignored
                            }

                            if (imageArray == null)
                            {
                                return;
                            }

                            FileStream fw = File.OpenWrite(tempName);
                            imageArray.CopyTo(fw);
                            fw.Close();
                            imageArray.Close();

                            // move the file to it's final location
                            string fullPath = Path.GetDirectoryName(fileName);
                            if (fullPath != null)
                            {
                                if (!Directory.Exists(fullPath))
                                {
                                    Directory.CreateDirectory(fullPath);
                                }
                            }

                            // move the file to it's final location
                            File.Move(tempName, fileName);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                BaseConfig.MyAnimeLog.Write(ex.ToString());
            }
        }