Skip to content

Commit

Permalink
PostImage streams from SQL to controller #359
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffputz committed Feb 2, 2024
1 parent a618ee8 commit 473f51a
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 9 deletions.
7 changes: 6 additions & 1 deletion src/PopForums.AzureKit/PostImage/PostImageRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public async Task DeletePostImageData(string id, string tenantID)
await container.DeleteBlobAsync(path, DeleteSnapshotsOption.IncludeSnapshots);
}

// The next two methods are not used when fetching images from Azure storage. The
// The next three methods are not used when fetching images from Azure storage. The
// default SQL implementation does use these.

public Task<Models.PostImage> GetWithoutData(string id)
Expand All @@ -63,4 +63,9 @@ public async Task DeletePostImageData(string id, string tenantID)
{
throw new NotImplementedException();
}

public Task<IStreamResponse> GetImageStream(string id)
{
throw new NotImplementedException();
}
}
9 changes: 6 additions & 3 deletions src/PopForums.Mvc/Areas/Forums/Controllers/ImageController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,12 @@ public async Task<ActionResult> PostImage(string id)
return Content(string.Empty);
}
}
var fullPostImage = await _postImageService.Get(id);
var stream = new MemoryStream(fullPostImage.ImageData);
return File(stream, "image/jpeg");

var streamResponse = await _postImageService.GetImageStream(id);
if (streamResponse == null)
return NotFound();
Response.RegisterForDispose(streamResponse);
return File(streamResponse.Stream, postImageSansData.ContentType);
}

[HttpPost]
Expand Down
17 changes: 17 additions & 0 deletions src/PopForums.Sql/Repositories/PostImageRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,28 @@ await _sqlObjectFactory.GetConnection().UsingAsync(connection =>
return await image;
}

[Obsolete("Use the combination of GetWithoutData(int) and GetImageStream(int) instead.")]
public async Task<PostImage> Get(string id)
{
Task<PostImage> image = null;
await _sqlObjectFactory.GetConnection().UsingAsync(connection =>
image = connection.QuerySingleOrDefaultAsync<PostImage>("SELECT * FROM pf_PostImage WHERE ID = @id", new {id}));
return await image;
}

public async Task<IStreamResponse> GetImageStream(string id)
{
var connection = (SqlConnection)_sqlObjectFactory.GetConnection();
var command = new SqlCommand("SELECT ImageData FROM pf_PostImage WHERE ID = @id", connection);
command.Parameters.AddWithValue("id", id);
connection.Open();
var reader = await command.ExecuteReaderAsync(CommandBehavior.SequentialAccess);
if (await reader.ReadAsync() && !await reader.IsDBNullAsync(0))
{
var stream = reader.GetStream(0);
var streamResponse = new StreamResponse(stream, connection, reader);
return streamResponse;
}
return default;
}
}
10 changes: 5 additions & 5 deletions src/PopForums/Models/PostImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

public class PostImage
{
public string ID { get; set; }
public DateTime TimeStamp { get; set; }
public string TenantID { get; set; }
public string ContentType { get; set; }
public byte[] ImageData { get; set; }
public string ID { get; init; }
public DateTime TimeStamp { get; init; }
public string TenantID { get; init; }
public string ContentType { get; init; }
public byte[] ImageData { get; init; }

}
2 changes: 2 additions & 0 deletions src/PopForums/Repositories/IPostImageRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ public interface IPostImageRepository
{
Task<PostImagePersistPayload> Persist(byte[] bytes, string contentType);
Task<PostImage> GetWithoutData(string id);
[Obsolete("Use the combination of GetWithoutData(int) and GetImageStream(int) instead.")]
Task<PostImage> Get(string id);
Task DeletePostImageData(string id, string tenantID);
Task<IStreamResponse> GetImageStream(string id);
}
8 changes: 8 additions & 0 deletions src/PopForums/Services/PostImageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ public interface IPostImageService
bool ProcessImageIsOk(byte[] bytes, string contentType);
Task<PostImagePersistPayload> PersistAndGetPayload();
Task<PostImage> GetWithoutData(string id);
[Obsolete("Use the combination of GetWithoutData(int) and GetImageStream(int) instead.")]
Task<PostImage> Get(string id);
Task<IStreamResponse> GetImageStream(string id);
Task DeleteTempRecord(string id);
Task DeleteTempRecords(string[] ids, string fullText);
Task DeleteOldPostImages();
Expand Down Expand Up @@ -77,12 +79,18 @@ public async Task<PostImage> GetWithoutData(string id)
return postImageSansData;
}

[Obsolete("Use the combination of GetWithoutData(int) and GetImageStream(int) instead.")]
public async Task<PostImage> Get(string id)
{
var postImageSansData = await _postImageRepository.Get(id);
return postImageSansData;
}

public async Task<IStreamResponse> GetImageStream(string id)
{
return await _postImageRepository.GetImageStream(id);
}

public async Task DeleteTempRecord(string id)
{
var guid = Guid.Parse(id);
Expand Down

0 comments on commit 473f51a

Please sign in to comment.