public async Task SaveImage() { var res = await _DiscogsClient.GetMaster(47813); res.Should().NotBeNull(); await _DiscogsClient.SaveImage(res.images[0], Path.GetTempPath(), "Ornette-TSOAJTC"); }
/// <summary> /// Downloads artist or album images from Discogs. /// </summary> /// <param name="discogsImages">Array of DiscogsImage of artist or album.</param> /// <param name="imageNamePattern">Name of album or artist generally.</param> /// <param name="imageData">List of image data from db.</param> /// <param name="ownerId">Id of imageDataCollection owner.</param> /// <param name="targetDirPath">Directory path to store images.</param> public void DownloadArtistOrAlbumImages(DiscogsImage[] discogsImages, string imageNamePattern, ICollection <ImageData> imageData, int ownerId, string targetDirPath) { var number = 1; //list of image names in target directory var imageNamesInTargetDirectory = _fileManager.GetFileNamesFromDirectory(targetDirPath); try { foreach (var discogsImage in discogsImages) { //check imageDataCollection for exact image copies with the same not null source if (!string.IsNullOrEmpty(discogsImage.uri) && imageData.Any(arg => arg.Source.Equals(discogsImage.uri))) { continue; } //generate name for new image var newImageNameAndNumber = _fileManager.GenerateNameForDownloadedImage(imageNamesInTargetDirectory, imageNamePattern, number); var newImageName = newImageNameAndNumber.Item1; imageNamesInTargetDirectory.Add(newImageName); number = newImageNameAndNumber.Item2 + 1; //save image to temp directory _discogsClient.SaveImage(discogsImage, _tempImageDirectoryPath, newImageName); //create new ImageData (important to add url to source property) var tempImagePath = Path.Combine(_tempImageDirectoryPath, newImageName); //move image to target directory _fileManager.MoveFile(tempImagePath, targetDirPath); //add new imageDataCollection to imageDataCollection var downloadedImageData = new ImageData() { Name = newImageName, Source = discogsImage.uri, Status = ImageStatus.InCollection }; imageData.Add(downloadedImageData); } //save image data for downloaded discogs images _repository.AddImagesData(imageData, ownerId); } //TODO: Add exception handling to ImageCollectionManager catch (Exception e) { Console.WriteLine(e); } finally { _fileManager.ClearDirectory(_tempImageDirectoryPath); } }