public override void ProcessCommand() { logger.Info("Processing CommandRequest_DownloadImage: {0}", EntityID); string downloadURL = ""; try { ImageDownloadRequest req = null; switch (EntityTypeEnum) { case JMMImageType.AniDB_Cover: AniDB_AnimeRepository repAnime = new AniDB_AnimeRepository(); AniDB_Anime anime = repAnime.GetByID(EntityID); if (anime == null) return; req = new ImageDownloadRequest(EntityTypeEnum, anime, ForceDownload); break; case JMMImageType.TvDB_Episode: TvDB_EpisodeRepository repTvEp = new TvDB_EpisodeRepository(); TvDB_Episode ep = repTvEp.GetByID(EntityID); if (ep == null) return; if (string.IsNullOrEmpty(ep.Filename)) return; req = new ImageDownloadRequest(EntityTypeEnum, ep, ForceDownload); break; case JMMImageType.TvDB_FanArt: TvDB_ImageFanartRepository repFanart = new TvDB_ImageFanartRepository(); TvDB_ImageFanart fanart = repFanart.GetByID(EntityID); if (fanart == null) return; if (string.IsNullOrEmpty(fanart.BannerPath)) return; req = new ImageDownloadRequest(EntityTypeEnum, fanart, ForceDownload); break; case JMMImageType.TvDB_Cover: TvDB_ImagePosterRepository repPoster = new TvDB_ImagePosterRepository(); TvDB_ImagePoster poster = repPoster.GetByID(EntityID); if (poster == null) return; if (string.IsNullOrEmpty(poster.BannerPath)) return; req = new ImageDownloadRequest(EntityTypeEnum, poster, ForceDownload); break; case JMMImageType.TvDB_Banner: TvDB_ImageWideBannerRepository repBanners = new TvDB_ImageWideBannerRepository(); TvDB_ImageWideBanner wideBanner = repBanners.GetByID(EntityID); if (wideBanner == null) return; if (string.IsNullOrEmpty(wideBanner.BannerPath)) return; req = new ImageDownloadRequest(EntityTypeEnum, wideBanner, ForceDownload); break; case JMMImageType.MovieDB_Poster: MovieDB_PosterRepository repMoviePosters = new MovieDB_PosterRepository(); MovieDB_Poster moviePoster = repMoviePosters.GetByID(EntityID); if (moviePoster == null) return; if (string.IsNullOrEmpty(moviePoster.URL)) return; req = new ImageDownloadRequest(EntityTypeEnum, moviePoster, ForceDownload); break; case JMMImageType.MovieDB_FanArt: MovieDB_FanartRepository repMovieFanart = new MovieDB_FanartRepository(); MovieDB_Fanart movieFanart = repMovieFanart.GetByID(EntityID); if (movieFanart == null) return; if (string.IsNullOrEmpty(movieFanart.URL)) return; req = new ImageDownloadRequest(EntityTypeEnum, movieFanart, ForceDownload); break; case JMMImageType.Trakt_Poster: Trakt_ImagePosterRepository repTraktPosters = new Trakt_ImagePosterRepository(); Trakt_ImagePoster traktPoster = repTraktPosters.GetByID(EntityID); if (traktPoster == null) return; if (string.IsNullOrEmpty(traktPoster.ImageURL)) return; req = new ImageDownloadRequest(EntityTypeEnum, traktPoster, ForceDownload); break; case JMMImageType.Trakt_Fanart: Trakt_ImageFanartRepository repTraktFanarts = new Trakt_ImageFanartRepository(); Trakt_ImageFanart traktFanart = repTraktFanarts.GetByID(EntityID); if (traktFanart == null) return; if (string.IsNullOrEmpty(traktFanart.ImageURL)) return; req = new ImageDownloadRequest(EntityTypeEnum, traktFanart, ForceDownload); break; case JMMImageType.Trakt_Friend: Trakt_FriendRepository repFriends = new Trakt_FriendRepository(); Trakt_Friend friend = repFriends.GetByID(EntityID); if (friend == null) return; if (string.IsNullOrEmpty(friend.Avatar)) return; req = new ImageDownloadRequest(EntityTypeEnum, friend, ForceDownload); break; case JMMImageType.Trakt_Episode: Trakt_EpisodeRepository repTraktEpisodes = new Trakt_EpisodeRepository(); Trakt_Episode traktEp = repTraktEpisodes.GetByID(EntityID); if (traktEp == null) return; if (string.IsNullOrEmpty(traktEp.EpisodeImage)) return; req = new ImageDownloadRequest(EntityTypeEnum, traktEp, ForceDownload); break; case JMMImageType.AniDB_Character: AniDB_CharacterRepository repChars = new AniDB_CharacterRepository(); AniDB_Character chr = repChars.GetByID(EntityID); if (chr == null) return; req = new ImageDownloadRequest(EntityTypeEnum, chr, ForceDownload); break; case JMMImageType.AniDB_Creator: AniDB_SeiyuuRepository repCreator = new AniDB_SeiyuuRepository(); AniDB_Seiyuu creator = repCreator.GetByID(EntityID); if (creator == null) return; req = new ImageDownloadRequest(EntityTypeEnum, creator, ForceDownload); break; } if (req == null) return; List<string> fileNames = new List<string>(); List<string> downloadURLs = new List<string>(); string fileNameTemp = GetFileName(req, false); string downloadURLTemp = GetFileURL(req, false); fileNames.Add(fileNameTemp); downloadURLs.Add(downloadURLTemp); if (req.ImageType == JMMImageType.TvDB_FanArt) { fileNameTemp = GetFileName(req, true); downloadURLTemp = GetFileURL(req, true); fileNames.Add(fileNameTemp); downloadURLs.Add(downloadURLTemp); } for (int i = 0; i < fileNames.Count; i++) { string fileName = fileNames[i]; downloadURL = downloadURLs[i]; bool downloadImage = true; bool fileExists = File.Exists(fileName); if (fileExists) { if (!req.ForceDownload) downloadImage = false; else downloadImage = true; } else downloadImage = true; if (downloadImage) { string tempName = Path.Combine(ImageUtils.GetImagesTempFolder(), Path.GetFileName(fileName)); if (File.Exists(tempName)) File.Delete(tempName); try { if (fileExists) File.Delete(fileName); } catch (Exception ex) { Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(ServerSettings.Culture); string msg = string.Format(JMMServer.Properties.Resources.Command_DeleteError, fileName, ex.Message); logger.Warn(msg); return; } // download image using (WebClient client = new WebClient()) { client.Headers.Add("user-agent", "JMM"); //OnImageDownloadEvent(new ImageDownloadEventArgs("", req, ImageDownloadEventType.Started)); //BaseConfig.MyAnimeLog.Write("ProcessImages: Download: {0} *** to *** {1}", req.URL, fullName); if (downloadURL.Length > 0) { client.DownloadFile(downloadURL, tempName); string extension = ""; string contentType = client.ResponseHeaders["Content-type"].ToLower(); if (contentType.IndexOf("gif") >= 0) extension = ".gif"; if (contentType.IndexOf("jpg") >= 0) extension = ".jpg"; if (contentType.IndexOf("jpeg") >= 0) extension = ".jpg"; if (contentType.IndexOf("bmp") >= 0) extension = ".bmp"; if (contentType.IndexOf("png") >= 0) extension = ".png"; if (extension.Length > 0) { string newFile = Path.ChangeExtension(tempName, extension); if (!newFile.ToLower().Equals(tempName.ToLower())) { try { System.IO.File.Delete(newFile); } catch { //BaseConfig.MyAnimeLog.Write("DownloadedImage:Download() Delete failed:{0}", newFile); } System.IO.File.Move(tempName, newFile); tempName = newFile; } } } } // move the file to it's final location // check that the final folder exists string fullPath = Path.GetDirectoryName(fileName); if (!Directory.Exists(fullPath)) Directory.CreateDirectory(fullPath); System.IO.File.Move(tempName, fileName); logger.Info("Image downloaded: {0}", fileName); } } } catch (Exception ex) { logger.Warn("Error processing CommandRequest_DownloadImage: {0} ({1}) - {2}", downloadURL, EntityID, ex.Message); return; } }