Skip to content

Commit

Permalink
add internal resized param, add photo buffer option to read image via…
Browse files Browse the repository at this point in the history
… buffer instead of path
  • Loading branch information
kev-le committed Oct 2, 2024
1 parent aae88c6 commit 7755339
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions StudioClient/Photo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ public async Task<dynamic> GetUploadUrl(long photoId, string md5 = "", bool useC
/// </summary>
/// <param name="photoPath">The path to the photo file.</param>
/// <param name="jobId">The ID of the job associated with the photo.</param>
/// <param name="photoBuffer">The photo buffer byte array (reads photo from byte array instead of photo path)</param>
/// <returns>A dynamic object representing the uploaded photo.</returns>
public async Task<dynamic?> UploadJobPhoto(string photoPath, long jobId)
public async Task<dynamic?> UploadJobPhoto(string photoPath, long jobId, byte[]? photoBuffer = null)
{
const int maxRetries = 3;
int attempt = 0;
Expand All @@ -107,7 +108,7 @@ public async Task<dynamic> GetUploadUrl(long photoId, string md5 = "", bool useC
{
try
{
return await UploadPhoto(photoPath, "job", jobId);
return await UploadPhoto(photoPath, "job", jobId, photoBuffer);
}
catch (Exception ex)
{
Expand All @@ -134,8 +135,9 @@ public async Task<dynamic> GetUploadUrl(long photoId, string md5 = "", bool useC
/// </summary>
/// <param name="photoPath">The path to the photo file.</param>
/// <param name="profileId">The ID of the profile associated with the photo.</param>
/// <param name="photoBuffer">The photo buffer byte array (reads photo from byte array instead of photo path)</param>
/// <returns>A dynamic object representing the uploaded photo.</returns>
public async Task<dynamic?> UploadProfilePhoto(string photoPath, long profileId)
public async Task<dynamic?> UploadProfilePhoto(string photoPath, long profileId, byte[] photoBuffer = null)

Check warning on line 140 in StudioClient/Photo.cs

View workflow job for this annotation

GitHub Actions / test

Cannot convert null literal to non-nullable reference type.
{
const int maxRetries = 3;
int attempt = 0;
Expand All @@ -144,7 +146,7 @@ public async Task<dynamic> GetUploadUrl(long photoId, string md5 = "", bool useC
{
try
{
return await UploadPhoto(photoPath, "profile", profileId);
return await UploadPhoto(photoPath, "profile", profileId, photoBuffer);
}
catch (Exception ex)
{
Expand Down Expand Up @@ -172,8 +174,9 @@ public async Task<dynamic> GetUploadUrl(long photoId, string md5 = "", bool useC
/// <param name="photoPath">The path to the photo file.</param>
/// <param name="modelName">The name of the model (job or profile).</param>
/// <param name="modelId">The ID of the model associated with the photo.</param>
/// <param name="photoBuffer">The photo buffer byte array (reads photo from byte array instead of photo path)</param>
/// <returns>A dynamic object representing the uploaded photo.</returns>
private async Task<dynamic> UploadPhoto(string photoPath, string modelName, long modelId)
private async Task<dynamic> UploadPhoto(string photoPath, string modelName, long modelId, byte[]? photoBuffer)
{
string[] availableModels = { "job", "profile" };
if (!availableModels.Contains(modelName)) throw new Exception("Invalid model name. Must be 'job' or 'profile'");
Expand All @@ -185,16 +188,7 @@ private async Task<dynamic> UploadPhoto(string photoPath, string modelName, long
throw new Exception("Photo has an invalid extension. Supported extensions (.jpg, .jpeg, .png, .webp)");
}

var photoObject = new JObject
{
{ "name", $"{Guid.NewGuid()}{fileExtension}" },
{ "path", photoPath }
};
if (modelName == "job") photoObject["job_id"] = modelId; else photoObject["profile_id"] = modelId;

dynamic photo = await CreatePhoto(photoObject);

byte[] photoData = File.ReadAllBytes(photoPath);
byte[] photoData = photoBuffer != null ? photoBuffer : File.ReadAllBytes(photoPath);
if (photoData.Length > 0 && ((photoData.Length / 1024 / 1024) > MAX_PHOTO_SIZE))
{
throw new Exception($"{photoPath} exceeds 27MB");
Expand All @@ -204,6 +198,16 @@ private async Task<dynamic> UploadPhoto(string photoPath, string modelName, long
Image image = Image.NewFromBuffer(photoData);
Image modifiedImage = ResizeImage(image, photoMetadata);

var photoObject = new JObject
{
{ "name", $"{Guid.NewGuid()}{fileExtension}" },
{ "path", photoPath },
{ "resized", image.Height != modifiedImage.Height }
};
if (modelName == "job") photoObject["job_id"] = modelId; else photoObject["profile_id"] = modelId;

dynamic photo = await CreatePhoto(photoObject);

string format = ".jpg";
if (photoMetadata.Format != null && photoMetadata.Format.Equals(ImageFormat.Png)) {
format = ".png";
Expand Down

0 comments on commit 7755339

Please sign in to comment.