Skip to content

Commit

Permalink
Merge pull request #3 from skylab-tech/version-2.2
Browse files Browse the repository at this point in the history
Add check for valid outputPath, fix bgs not being reused for Download…
  • Loading branch information
kev-le authored Feb 23, 2024
2 parents 7d77b7e + b4cc857 commit 10ae18a
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions StudioClient/DownloadHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@

namespace SkylabStudio
{
public class PhotoOptions
{
public List<Image>? Bgs { get; set; }

// Default constructor (parameterless)
public PhotoOptions()
{
Bgs = new List<Image>();
}
}

public class DownloadAllPhotosResult
{
public List<string> SuccessPhotos { get; set; }
Expand Down Expand Up @@ -92,12 +103,20 @@ private async Task<bool> DownloadReplacedBackgroundImage(string fileName, Image

public async Task<DownloadAllPhotosResult> DownloadAllPhotos(JArray photosList, dynamic profile, string outputPath)
{
if (!Directory.Exists(outputPath))
{
throw new Exception("Invalid output path");
}

List<string> successPhotos = new List<string>();
List<string> erroredPhotos = new List<string>();
List<Image> bgs = new List<Image>();

try {
profile = await GetProfile(profile.id.Value);
List<Image> bgs = await DownloadBgImages(profile);
if (profile?.photos?.Count > 0) {
bgs = await DownloadBgImages(profile);
}

var httpClient = new HttpClient();

Expand All @@ -106,9 +125,13 @@ public async Task<DownloadAllPhotosResult> DownloadAllPhotos(JArray photosList,
// Use a semaphore to control access to the download operation
var semaphore = new SemaphoreSlim(_maxConcurrentDownloads);
List<Task<Tuple<string, bool>>> downloadTasks = new List<Task<Tuple<string, bool>>>();
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
Expand Down Expand Up @@ -140,8 +163,13 @@ public async Task<DownloadAllPhotosResult> DownloadAllPhotos(JArray photosList,
return downloadResults;
}
}
public async Task<Tuple<string,bool>> DownloadPhoto(long photoId, string outputPath, dynamic? profile = null, dynamic? options = null, SemaphoreSlim? semaphore = null)
public async Task<Tuple<string,bool>> 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;

Expand All @@ -157,7 +185,7 @@ public async Task<Tuple<string,bool>> 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<Image>? bgs = options?.bgs;
List<Image>? bgs = options?.Bgs;

// Load output image
byte[] imageBuffer = await DownloadImageAsync(photo.retouchedUrl.Value);
Expand Down

0 comments on commit 10ae18a

Please sign in to comment.