-
Notifications
You must be signed in to change notification settings - Fork 0
Image Upload suggestions #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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("-", ""); | ||
|
@@ -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 } | ||
}); | ||
|
@@ -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(); | ||
|
@@ -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; | ||
|
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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. 🤷♀️