From b4cc8578839361fbc3af9f6f420923ec1c81f59a Mon Sep 17 00:00:00 2001 From: kev-le Date: Thu, 22 Feb 2024 17:25:50 -0800 Subject: [PATCH] Add check for valid outputPath, fix bgs not being reused for DownloadAllPhotos --- StudioClient/DownloadHelper.cs | 36 ++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/StudioClient/DownloadHelper.cs b/StudioClient/DownloadHelper.cs index 2a79594..3552a19 100644 --- a/StudioClient/DownloadHelper.cs +++ b/StudioClient/DownloadHelper.cs @@ -4,6 +4,17 @@ namespace SkylabStudio { + public class PhotoOptions + { + public List? Bgs { get; set; } + + // Default constructor (parameterless) + public PhotoOptions() + { + Bgs = new List(); + } + } + public class DownloadAllPhotosResult { public List SuccessPhotos { get; set; } @@ -92,12 +103,20 @@ private async Task DownloadReplacedBackgroundImage(string fileName, Image public async Task DownloadAllPhotos(JArray photosList, dynamic profile, string outputPath) { + if (!Directory.Exists(outputPath)) + { + throw new Exception("Invalid output path"); + } + List successPhotos = new List(); List erroredPhotos = new List(); + List bgs = new List(); try { profile = await GetProfile(profile.id.Value); - List bgs = await DownloadBgImages(profile); + if (profile?.photos?.Count > 0) { + bgs = await DownloadBgImages(profile); + } var httpClient = new HttpClient(); @@ -106,9 +125,13 @@ public async Task DownloadAllPhotos(JArray photosList, // Use a semaphore to control access to the download operation var semaphore = new SemaphoreSlim(_maxConcurrentDownloads); List>> downloadTasks = new List>>(); + PhotoOptions photoOptions = new PhotoOptions + { + Bgs = bgs + }; foreach (string photoId in photoIds) { - downloadTasks.Add(DownloadPhoto(long.Parse(photoId), outputPath, profile, null, semaphore)); + downloadTasks.Add(DownloadPhoto(long.Parse(photoId), outputPath, profile, photoOptions, semaphore)); } // Wait for all download tasks to complete @@ -140,8 +163,13 @@ public async Task DownloadAllPhotos(JArray photosList, return downloadResults; } } - public async Task> DownloadPhoto(long photoId, string outputPath, dynamic? profile = null, dynamic? options = null, SemaphoreSlim? semaphore = null) + public async Task> DownloadPhoto(long photoId, string outputPath, dynamic? profile = null, PhotoOptions? options = null, SemaphoreSlim? semaphore = null) { + if (!Directory.Exists(outputPath)) + { + throw new Exception("Invalid output path"); + } + dynamic photo = await GetPhoto(photoId); long profileId = photo.job.profileId; @@ -157,7 +185,7 @@ public async Task> DownloadPhoto(long photoId, string output bool replaceBackground = Convert.ToBoolean(profile.replaceBackground.Value); bool isDualFileOutput = Convert.ToBoolean(profile.dualFileOutput.Value); bool enableStripPngMetadata = Convert.ToBoolean(profile.enableStripPngMetadata.Value); - List? bgs = options?.bgs; + List? bgs = options?.Bgs; // Load output image byte[] imageBuffer = await DownloadImageAsync(photo.retouchedUrl.Value);