Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions SyncUp.Data/Models/Tenancy/Tenant.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using IntelliTect.SyncUp.Data.Services;
using SyncUp.Data.Models;
using System.ComponentModel;
using File = IntelliTect.Coalesce.Models.File;

namespace IntelliTect.SyncUp.Data.Models;

Expand All @@ -24,7 +25,41 @@
[ForeignKey(nameof(BannerImageId))]
public Image? BannerImage { get; set; }

[Coalesce]
public async Task<ItemResult<Image>> UploadImageFile(AppDbContext db, [Inject] ImageService imageService, File file)
Copy link
Collaborator Author

@meghanmae meghanmae Nov 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My code is verbose because I wrote this quickly, but the code given here can be reduced if you just make one shared function and 2 lambda functions. 🤷‍♀️

{
try
{
Image image = await imageService.AddImage(file);

BannerImageId = image.ImageId;
await db.SaveChangesAsync();

return image;
}
catch(Exception ex)
{
return ex.Message;
}
}

[Coalesce]
public async Task<ItemResult<Image>> UploadImageUrl(AppDbContext db, [Inject] ImageService imageService, string url)
{
try
{
Image image = await imageService.AddImage(url);

BannerImageId = image.ImageId;
await db.SaveChangesAsync();

return image;
}
catch (Exception ex)
{
return ex.Message;
}
}

[DefaultDataSource]
public class DefaultSource(CrudContext<AppDbContext> context) : AppDataSource<Tenant>(context)
Expand Down Expand Up @@ -70,7 +105,7 @@
}

[Coalesce]
public static async Task<ItemResult<bool>> IsMemberOf(

Check warning on line 108 in SyncUp.Data/Models/Tenancy/Tenant.cs

View workflow job for this annotation

GitHub Actions / build-and-test

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 108 in SyncUp.Data/Models/Tenancy/Tenant.cs

View workflow job for this annotation

GitHub Actions / build-and-test

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
AppDbContext db,
ClaimsPrincipal user,
string tenantId
Expand All @@ -86,13 +121,13 @@
db.ForceSetTenant(tenant.TenantId);

var isMember = db.TenantMemberships.Any(tm => tm.UserId == user.GetUserId());
db.ForceSetTenant(currentTenantId);

Check warning on line 124 in SyncUp.Data/Models/Tenancy/Tenant.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Possible null reference argument for parameter 'tenantId' in 'void AppDbContext.ForceSetTenant(string tenantId)'.

Check warning on line 124 in SyncUp.Data/Models/Tenancy/Tenant.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Possible null reference argument for parameter 'tenantId' in 'void AppDbContext.ForceSetTenant(string tenantId)'.

return new ItemResult<bool>(isMember, null);
}

[Coalesce]
public static async Task<ItemResult> ToggleMembership(

Check warning on line 130 in SyncUp.Data/Models/Tenancy/Tenant.cs

View workflow job for this annotation

GitHub Actions / build-and-test

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 130 in SyncUp.Data/Models/Tenancy/Tenant.cs

View workflow job for this annotation

GitHub Actions / build-and-test

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
AppDbContext db,
ClaimsPrincipal user,
string tenantId
Expand All @@ -116,7 +151,7 @@

db.TenantMemberships.Add(new()
{
UserId = user.GetUserId()

Check warning on line 154 in SyncUp.Data/Models/Tenancy/Tenant.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Possible null reference assignment.

Check warning on line 154 in SyncUp.Data/Models/Tenancy/Tenant.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Possible null reference assignment.
});
}
else
Expand Down
54 changes: 15 additions & 39 deletions SyncUp.Data/Services/ImageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,25 @@
using IntelliTect.SyncUp.Data;
using SyncUp.Data.Models;
using System.Net;
using File = IntelliTect.Coalesce.Models.File;

namespace IntelliTect.SyncUp.Data.Services;
[Coalesce, Service]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the coalesce service attribute because this never actually linked an image up with a tenant ( and it probably shouldn't, because we want to use this for groups, as well, and likely other models like comments... posts... etc)

This should just be an internal, non coalesce, service

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I fully undertstand...

public class ImageService(AppDbContext db, IOptions<AzureBlobStorageOptions> options)
{
[Coalesce]
[Execute(SecurityPermissionLevels.AllowAuthenticated, HttpMethod = IntelliTect.Coalesce.DataAnnotations.HttpMethod.Post)]
public async Task<ItemResult<Image>> Upload(byte[] content)
public async Task<Image> AddImage(string url)
{
try
{
return await AddImage(content);
}
catch (Exception ex)
byte[] bytes = [];
using (var client = new HttpClient())
{
return ex.Message;
bytes = await client.GetByteArrayAsync(url);
}

return await AddImage(new File(bytes));
}

public async Task<Image> AddImage(byte[] content)
public async Task<Image> AddImage(File file)
{
if (content.Length == 0) throw new Exception("No image provided");
if (file.Length == 0 || file.Content is null) throw new Exception("No image provided");
try
{
var imageId = Guid.NewGuid().ToString().Replace("-", "");
Expand All @@ -36,7 +33,7 @@ public async Task<Image> AddImage(byte[] content)

try
{
await cloudBlob.UploadAsync(new MemoryStream(content), new BlobUploadOptions
await cloudBlob.UploadAsync(file.Content, new BlobUploadOptions
{
Conditions = new() { IfNoneMatch = Azure.ETag.All }
});
Expand All @@ -46,7 +43,11 @@ public async Task<Image> AddImage(byte[] content)
throw new Exception("An Error occurred while trying to save the specified image to blob storage", ex);

}
var imageScan = new MagickImage(content);

// Reset stream position to 0 before reusing
file.Content.Position = 0;

var imageScan = new MagickImage(file.Content);
var colors = imageScan.Histogram();

string dominantColor = colors.MaxBy(kvp => kvp.Value).Key.ToHexString();
Expand All @@ -69,31 +70,6 @@ public async Task<Image> AddImage(byte[] content)
}
}

[Coalesce]
[Execute(SecurityPermissionLevels.AllowAuthenticated, HttpMethod = IntelliTect.Coalesce.DataAnnotations.HttpMethod.Post)]
public async Task<ItemResult<Image>> UploadFromUrl(string url)
{
// Download the file get a byte array
try
{
return await AddImage(url);
}
catch
{
// TODO: Log this
return $"An Error occurred while trying to download the specified image.";
}
}

public async Task<Image> AddImage(string url)
{
using (var client = new HttpClient())
{
var bytes = await client.GetByteArrayAsync(url);
return await AddImage(bytes);
}
}

private BlobClient GetBlobClient(string relativeLocation)
{
var connectionString = options.Value.ConnectionString;
Expand Down
162 changes: 0 additions & 162 deletions SyncUp.Web/Api/Generated/ImageServiceController.g.cs

This file was deleted.

Loading
Loading