Skip to content
This repository has been archived by the owner on Sep 4, 2023. It is now read-only.

Commit

Permalink
Fix exporters no longer working correctly (#88)
Browse files Browse the repository at this point in the history
* Temporarily fix paginator not working correctly

* Set format names to uppercase

* Fix stream being disposed before usage

* Fix homepage returning null when no images are present
  • Loading branch information
Typiqally authored Apr 25, 2023
1 parent f3db6d9 commit 74ee45a
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 19 deletions.
7 changes: 3 additions & 4 deletions Epsilon.Canvas/Service/OutcomeHttpService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Net.Http.Json;
using System.Text;
using Epsilon.Abstractions.Http;
using Epsilon.Canvas.Abstractions.Model;
using Epsilon.Canvas.Abstractions.Service;
Expand All @@ -26,10 +25,10 @@ public OutcomeHttpService(HttpClient client, IPaginatorHttpService paginator)

public async Task<OutcomeResultCollection?> GetResults(int courseId, IEnumerable<string> include)
{
var url = new StringBuilder($"v1/courses/{courseId}/outcome_results");
var query = $"?include[]={string.Join("&include[]=", include)}";
var url = $"v1/courses/{courseId}/outcome_results?include[]={string.Join("&include[]=", include)}";
var requestUri = new Uri(url, UriKind.Relative);

var responses = await _paginator.GetAllPages<OutcomeResultCollection>(HttpMethod.Get, new Uri(url + query));
var responses = await _paginator.GetAllPages<OutcomeResultCollection>(HttpMethod.Get, requestUri);
var responsesArray = responses.ToArray();

return new OutcomeResultCollection(
Expand Down
7 changes: 6 additions & 1 deletion Epsilon.Canvas/Service/PaginatorHttpService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@ public async Task<IEnumerable<TResult>> GetAllPages<TResult>(HttpMethod method,
var pages = new List<TResult>();
var page = "1";

var uriString = uri.OriginalString;
uriString += !uriString.Contains('?', StringComparison.InvariantCulture)
? "?"
: "&";

do
{
var offset = pages.Count * Limit;
using var request = new HttpRequestMessage(method, $"{uri}per_page={Limit}&offset={offset}&page={page}");
using var request = new HttpRequestMessage(method, $"{uriString}per_page={Limit}&offset={offset}&page={page}");
var response = await Client.SendAsync(request);
var value = await response.Content.ReadFromJsonAsync<TResult>();

Expand Down
3 changes: 2 additions & 1 deletion Epsilon/Export/Exporters/ConsoleModuleExporter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics.CodeAnalysis;
using System.Text;
using Epsilon.Abstractions.Export;
using Epsilon.Abstractions.Model;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -26,7 +27,7 @@ public ConsoleModuleExporter(ILogger<ConsoleModuleExporter> logger)
public async Task<Stream> Export(ExportData data, string format)
{
var stream = new MemoryStream();
await using var writer = new StreamWriter(stream);
await using var writer = new StreamWriter(stream, Encoding.UTF8, 1024, true);

foreach (var module in data.CourseModules)
{
Expand Down
3 changes: 2 additions & 1 deletion Epsilon/Export/Exporters/CsvModuleExporter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Data;
using System.Text;
using Epsilon.Abstractions.Export;
using Epsilon.Abstractions.Model;

Expand All @@ -16,7 +17,7 @@ public class CsvModuleExporter : ICanvasModuleExporter
public async Task<Stream> Export(ExportData data, string format)
{
var stream = new MemoryStream();
await using var writer = new StreamWriter(stream);
await using var writer = new StreamWriter(stream, Encoding.UTF8, 1024, true);

using var dt = CreateDataTable(data.CourseModules);
WriteHeader(writer, dt);
Expand Down
21 changes: 9 additions & 12 deletions Epsilon/Export/Exporters/WordModuleExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ public WordModuleExporter(IFileHttpService fileService)

public IEnumerable<string> Formats { get; } = new[]
{
"word",
"docx",
"WORD",
"DOCX",
};

public string FileExtension => "docx";

public async Task<Stream> Export(ExportData data, string format)
{
var stream = new MemoryStream();
using var document = WordprocessingDocument.Create(stream, WordprocessingDocumentType.Document);
var document = WordprocessingDocument.Create(stream, WordprocessingDocumentType.Document);

document.AddMainDocumentPart();
document.MainDocumentPart!.Document = new Document(new Body());
Expand All @@ -67,26 +67,23 @@ public async Task<Stream> Export(ExportData data, string format)

var personaHtml = await GetPersonaHtmlDocument(_fileService, data.PersonaHtml);

using var ms = new MemoryStream(new UTF8Encoding(true).GetPreamble()
.Concat(Encoding.UTF8.GetBytes($"<html>{personaHtml.Text}</html>")).ToArray());
using var personaHtmlStream = new MemoryStream(Encoding.UTF8.GetPreamble().Concat(Encoding.UTF8.GetBytes($"<html>{personaHtml.Text}</html>")).ToArray());
var formatImportPart = document.MainDocumentPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.Html, altChunkId);

var formatImportPart =
document.MainDocumentPart.AddAlternativeFormatImportPart(
AlternativeFormatImportPartType.Html, altChunkId);

formatImportPart.FeedData(ms);
formatImportPart.FeedData(personaHtmlStream);
var altChunk = new AltChunk
{
Id = altChunkId,
};

body?.Append(altChunk);
await ms.DisposeAsync();
body?.Append(new Paragraph(new Run(new Break
{
Type = BreakValues.Page,
})));

await personaHtmlStream.DisposeAsync();

foreach (var module in data.CourseModules)
{
body?.Append(CreateText(module.Name));
Expand Down Expand Up @@ -146,7 +143,7 @@ private static async Task<HtmlDocument> GetPersonaHtmlDocument(IFileHttpService
htmlDoc.LoadHtml(htmlString);
if (htmlDoc.DocumentNode.SelectNodes("//img") == null)
{
return null;
return htmlDoc;
}

foreach (var node in htmlDoc.DocumentNode.SelectNodes("//img"))
Expand Down

0 comments on commit 74ee45a

Please sign in to comment.