Skip to content

Commit

Permalink
Add ability to upload files
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveSandersonMS committed Nov 4, 2020
1 parent d657f4b commit 6d034ba
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 8 deletions.
20 changes: 20 additions & 0 deletions Api/KernelFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,26 @@ public async Task<IActionResult> GetCompletions(
return new OkObjectResult(result);
}

[FunctionName("UploadFile")]
public async Task<IActionResult> UploadFile(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "code/uploadfile")] HttpRequest req,
ILogger log)
{
using var requestData = new MemoryStream();
await req.Body.CopyToAsync(requestData);

// TODO: Is there a way to copy data into the kernel without stringification?
var requestDataBase64 = Convert.ToBase64String(new Span<byte>(requestData.GetBuffer(), 0, (int)requestData.Length));

var notebookId = req.Query["notebookId"].First();
var variable = req.Query["variable"].First();
var kernel = _kernels.GetKernelForNotebook(notebookId);

var code = $"var {variable} = Convert.FromBase64String(\"{requestDataBase64}\");";
var request = await kernel.SendAsync(new SubmitCode(code), CancellationToken.None);
return new OkResult();
}

// TODO: Avoid duplication by changing MonacoRazor to target 3.0 or moving shared types to seperate package.
public enum CompletionItemKind : int
{
Expand Down
10 changes: 7 additions & 3 deletions Client/Pages/Notebook/Cells.razor
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@
var cell = notebook.Cells[index];
if (cell.Type == CellType.Code)
{
<CodeCell @key="@cell" Cell="@cell" OnCodeChange="SaveNotebook" />
<CodeCell @key="@cell" Cell="@cell" OnCodeChange="SaveNotebook"></CodeCell>
}
else if (cell.Type == CellType.File)
{
<FileCell @key="@cell" Cell="@cell"></FileCell>
}
else
{
<TextCell @key="@cell" Cell="@cell" />
<TextCell @key="@cell" Cell="@cell"></TextCell>
}

<NewCell Position="@index" NotebookId="@NotebookId" />
<NewCell Position="@index" Notebook="@notebook"></NewCell>
}
}
</div>
Expand Down
25 changes: 25 additions & 0 deletions Client/Pages/Notebook/FileCell.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
@inject HttpClient Http

<div class="file">
<span>@Cell.Content</span>:
<InputFile OnChange="SupplyFileAsync" />
<div>@status</div>
</div>

@code {
[Parameter]
public blazoract.Shared.Cell Cell { get; set; }

private string status;

private async Task SupplyFileAsync(InputFileChangeEventArgs eventArgs)
{
using var fileStream = eventArgs.File.OpenReadStream(maxAllowedSize: 5*1024*1024);

status = $"Sending {eventArgs.File.Size} bytes...";
var url = $"api/code/uploadfile?notebookId={Cell.NotebookId}&variable={Cell.Content}";
await Http.PostAsync(url, new StreamContent(fileStream));

status = $"Finished sending {eventArgs.File.Size} bytes.";
}
}
14 changes: 14 additions & 0 deletions Client/Pages/Notebook/FileCell.razor.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.file {
display: table;
background-color: #eee;
padding: 1em 2em;
margin: 1em auto;
}

span {
font-family: monospace;
}

::deep input[type=file] {
margin-left: 3em;
}
38 changes: 34 additions & 4 deletions Client/Pages/Notebook/NewCell.razor
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,52 @@
</path>
</svg>
</button>
<button @onclick="AddFileCell">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" width="16" height="16">
<path d="M17.222,5.041l-4.443-4.414c-0.152-0.151-0.356-0.235-0.571-0.235h-8.86c-0.444,0-0.807,0.361-0.807,0.808v17.602c0,0.448,0.363,0.808,0.807,0.808h13.303c0.448,0,0.808-0.36,0.808-0.808V5.615C17.459,5.399,17.373,5.192,17.222,5.041zM15.843,17.993H4.157V2.007h7.72l3.966,3.942V17.993z"></path>
<path d="M5.112,7.3c0,0.446,0.363,0.808,0.808,0.808h8.077c0.445,0,0.808-0.361,0.808-0.808c0-0.447-0.363-0.808-0.808-0.808H5.92C5.475,6.492,5.112,6.853,5.112,7.3z"></path>
<path d="M5.92,5.331h4.342c0.445,0,0.808-0.361,0.808-0.808c0-0.446-0.363-0.808-0.808-0.808H5.92c-0.444,0-0.808,0.361-0.808,0.808C5.112,4.97,5.475,5.331,5.92,5.331z"></path>
<path d="M13.997,9.218H5.92c-0.444,0-0.808,0.361-0.808,0.808c0,0.446,0.363,0.808,0.808,0.808h8.077c0.445,0,0.808-0.361,0.808-0.808C14.805,9.58,14.442,9.218,13.997,9.218z"></path>
<path d="M13.997,11.944H5.92c-0.444,0-0.808,0.361-0.808,0.808c0,0.446,0.363,0.808,0.808,0.808h8.077c0.445,0,0.808-0.361,0.808-0.808C14.805,12.306,14.442,11.944,13.997,11.944z"></path>
<path d="M13.997,14.67H5.92c-0.444,0-0.808,0.361-0.808,0.808c0,0.447,0.363,0.808,0.808,0.808h8.077c0.445,0,0.808-0.361,0.808-0.808C14.805,15.032,14.442,14.67,13.997,14.67z"></path>
</svg>
</button>
</div>

@code {
[Parameter]
public int Position { get; set; }

[Parameter]
public string NotebookId { get; set; }
public blazoract.Shared.Notebook Notebook { get; set; }

public async Task AddCodeCell()
{
await content.AddCell(NotebookId, "", CellType.Code, Position + 1);
await content.AddCell(Notebook.NotebookId, "", CellType.Code, Position + 1);
}

public async Task AddTextCell()
{
await content.AddCell(NotebookId, "Add text here...", CellType.Text, Position + 1);
await content.AddCell(Notebook.NotebookId, "Add text here...", CellType.Text, Position + 1);
}

public async Task AddFileCell()
{
var fileIndex = ChooseAvailableFileVariableName();
await content.AddCell(Notebook.NotebookId, fileIndex, CellType.File, Position + 1);
}

private string ChooseAvailableFileVariableName()
{
var existingFileVariables = Notebook.Cells.Where(c => c.Type == CellType.File).ToDictionary(c => c.Content, c => c);

for (var i = 1; ; i++)
{
var candidateName = $"file{i}";
if (!existingFileVariables.ContainsKey(candidateName))
{
return candidateName;
}
}
}
}
}
3 changes: 2 additions & 1 deletion Shared/Cell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public Cell(string notebookId, string content, CellType type = CellType.Code)
public enum CellType
{
Code,
Text
Text,
File,
}
}

0 comments on commit 6d034ba

Please sign in to comment.