bulletboards/ImageBoardServerApp/Shared/Components/Forms/PostForm.razor

186 lines
5.4 KiB
Text
Raw Normal View History

2023-01-18 12:56:24 +00:00
@using Radzen
@using System.ComponentModel.DataAnnotations
@using System.IO.Pipelines
@using System.Net.Mime
@using System.Reflection
@using System.Runtime.CompilerServices
@using ImageBoardServerApp.Data
@using ImageBoardServerApp.Data.Repository
@inject NavigationManager NavigationManager
2023-01-27 22:50:12 +00:00
@inject IWebHostEnvironment env
2023-01-18 12:56:24 +00:00
<div>
<span>[</span>
<a class="toggleOpened" onclick="@ToggleOpened">@toggleText</a>
<span>]</span>
</div>
@if (opened)
{
<div class="pd centered">
<span>Post to /@board.Tag/ - @board.Topic</span>
<div class="centered formContent">
<div>
<div class="pd centered marg">
@if (image != null)
2023-01-27 22:50:12 +00:00
{
<img class="formImage" src="@($"data:image/png;base64,{Convert.ToBase64String(image)}")" alt="No Image"/>
}
</div>
<InputFile OnChange="@SingleUpload" type="file" accept="image/*"/>
</div>
<div>
<div class="pd centered marg">
<RadzenTextBox Placeholder="Username (Anonymous)" MaxLength="15" Change=@(args => OnChange(args, "username")) Class="w-100"/>
</div>
<div class="pd centered marg">
<RadzenTextBox Placeholder="Title" MaxLength="20" Change=@(args => OnChange(args, "title")) Class="w-100"/>
</div>
<div class="pd centered marg">
<RadzenTextArea Placeholder="Content..." @bind-Value="@postContent" Cols="30" Rows="6" Change=@(args => OnChange(args, "Content")) Class="w-100"/>
</div>
</div>
2023-01-18 12:56:24 +00:00
</div>
<div class="pd centered marg">
<FormInfo/>
2023-01-18 12:56:24 +00:00
<RadzenButton class="pd" Click="@onPostClick" Text="Post!"></RadzenButton>
</div>
</div>
}
@code {
private bool opened = false;
private string toggleText = "Open Post Formula";
private void ToggleOpened()
{
opened = !opened;
toggleText = opened ? "Close Post Formula" : "Open Post Formula";
}
[Parameter]
[Required]
public BoardData board { get; set; } = new BoardData();
string postUsername = "Anonymous";
string postTitle = "";
string postContent = "";
void OnChange(string value, string name)
{
switch (name)
{
case "title":
postTitle = value;
break;
case "username":
postUsername = value;
break;
case "content":
postContent = value;
break;
default:
Console.WriteLine("not found.");
break;
}
Console.WriteLine($"Smth changed!: {value}");
}
2023-01-27 22:50:12 +00:00
private IBrowserFile selectedFile;
2023-01-18 12:56:24 +00:00
private async Task SingleUpload(InputFileChangeEventArgs e)
{
2023-01-27 22:50:12 +00:00
/*
2023-01-18 12:56:24 +00:00
MemoryStream ms = new MemoryStream();
await e.File.OpenReadStream().CopyToAsync(ms);
var bytes = ms.ToArray();
image = bytes;
Console.WriteLine("File has been selected!");
2023-01-27 22:50:12 +00:00
ms.Close(); */
selectedFile = e.GetMultipleFiles()[0];
this.StateHasChanged();
2023-01-18 12:56:24 +00:00
}
private Byte[] image;
private async Task onPostClick()
{
var userToCreate = new UserData
{
Ipv4Address = "192.168.178.101",
Banned = false,
lastActionTimeStamp = DateTime.Now.Millisecond
};
int userID;
UserData foundusr = await UsersRepository.getUserByIPAsync(userToCreate.Ipv4Address);
if (foundusr == null)
{
userID = await UsersRepository.createUserAsync(userToCreate);
}
else
{
userID = foundusr.UserID;
if(foundusr.Banned)
return;
foundusr.lastActionTimeStamp = DateTime.Now.Millisecond;
await UsersRepository.updateUserAsync(foundusr);
}
2023-01-18 12:56:24 +00:00
//TODO Add check if data is image
2023-01-27 22:50:12 +00:00
if (selectedFile == null)
{
return;
}
Stream stream = selectedFile.OpenReadStream(maxAllowedSize: 512000 * 4); // max 2MB
var path = $"{env.WebRootPath}\\dynamic\\op\\{selectedFile.Name}";
FileStream fs = File.Create(path);
await stream.CopyToAsync(fs);
stream.Close();
fs.Close();
2023-01-18 12:56:24 +00:00
var imageToUpload = new ImageData
{
Board = board.Tag,
Image = image
};
int imageID = await ImagesRepository.createImageAsync(imageToUpload);
var postToPost = new PostData
{
UserID = userID,
ImageID = imageID,
Username = postUsername,
Title = postTitle,
Content = postContent,
2023-01-25 16:26:21 +00:00
Interactions = 0,
2023-01-18 12:56:24 +00:00
CreatedAt = DateTimeOffset.Now.ToUnixTimeMilliseconds(),
Board = board.Tag
};
int postId = await PostsRepository.createPostAsync(postToPost);
if (postId != -1)
{
//Open post successfull
NavigationManager.NavigateTo($"/{board.Tag}/thread/{postId}", true, true);
2023-01-18 12:56:24 +00:00
Console.WriteLine("Post created");
}
else
{
//Open post unsucessfull
NavigationManager.NavigateTo("/UnSuccessfulPost");
Console.WriteLine("Shit sucks and did not work.");
}
}
}