@using System.ComponentModel.DataAnnotations
@using ImageBoardServerApp.Auth
@using ImageBoardServerApp.Data.Repository
@using ImageBoardServerApp.Util
@inject NavigationManager NavigationManager
@inject IWebHostEnvironment env
@inject AuthenticationStateProvider authStateProvider
[
]
@if (opened)
{
Post to /@board.Tag/ - @board.Topic
@if (hasErr)
{
@postErr
}
}
@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 { get; set; }
string postTitle { get; set; } = "";
string postContent { get; set; } = "";
bool postAnon { get; set; } = false;
protected override async Task OnAfterRenderAsync(bool firstRender)
{
var cauthStateProvder = (CustomAuthenticationStateProvider)authStateProvider;
var user = await cauthStateProvder.GetAuthenticationStateAsync();
var usr = user.User;
UserData foundusr = await UsersRepository.getUserByEmailRawAsync(usr.Identity.Name);
if (foundusr == null)
{
hasErr = true;
postErr = "You are not logged in.";
return;
}
postUsername = foundusr.LastUsedName;
if (!foundusr.ConfirmedEmail)
{
hasErr = true;
postErr = "You cannot post without an verified email.";
return;
}
await base.OnAfterRenderAsync(firstRender);
}
private IBrowserFile selectedFile;
private async Task SingleUpload(InputFileChangeEventArgs e)
{
selectedFile = e.GetMultipleFiles()[0];
this.StateHasChanged();
}
string postErr { get; set; }
bool hasErr { get; set; } = false;
private async Task onPostClick()
{
var cauthStateProvder = (CustomAuthenticationStateProvider)authStateProvider;
var user = await cauthStateProvder.GetAuthenticationStateAsync();
var usr = user.User;
UserData foundusr = await UsersRepository.getUserByEmailRawAsync(usr.Identity.Name);
if (foundusr == null)
{
hasErr = true;
postErr = "You are not logged in.";
return;
}
if (!foundusr.ConfirmedEmail)
{
hasErr = true;
postErr = "You cannot post without an verified email.";
return;
}
int userID = foundusr.UserID;
if (DateTimeOffset.Now.ToUnixTimeMilliseconds() - foundusr.TimeBanned > 0)
{
foundusr.TimeBanned = -1;
}
if (foundusr.TimeBanned != -1)
{
var dt = TheManager.ConvertToDateTime(foundusr.TimeBanned);
hasErr = true;
postErr = "You are banned for \"" + foundusr.BanReason + "\" and may not post until " + dt.Year + "." + dt.Month + "." + dt.Day + " " + dt.Hour + ":" + dt.Minute + "::" + dt.Second;
return;
}
BoardData b = await BoardsRepository.getBoardByTagAsync(board.Tag);
if (b.isLocked)
{
hasErr = true;
postErr = "This board is currently locked.";
return;
}
foundusr.lastActionTimeStamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
if (postUsername == null || postUsername == "")
{
postUsername = "Anonymous";
}
foundusr.LastUsedName = postUsername;
await UsersRepository.updateUserAsync(foundusr);
//TODO Add check if data is image
if (selectedFile == null || selectedFile.Size >= 512000 * 2 * 10)
{
hasErr = true;
postErr = "You did not attach a file or the selected file is bigger then the 10MiB file limit.";
return;
}
Stream stream = selectedFile.OpenReadStream(maxAllowedSize: 512000 * 2 * 10); // max 10MB
var file = Path.GetRandomFileName() + "." + selectedFile.Name.Split(".")[selectedFile.Name.Split(".").Length - 1];
var path = $"{env.WebRootPath}/img/dynamic/op/{@board.Tag}/{@file}";
FileStream fs = File.Create(path);
await stream.CopyToAsync(fs);
stream.Close();
fs.Close();
var imageToUpload = new ImageData
{
Board = board.Tag,
ImageLocation = $"/img/dynamic/op/{@board.Tag}/{@file}"
};
int imageID = await ImagesRepository.createImageAsync(imageToUpload);
int thisGET = b.NumberOfGETs + 1;
b.NumberOfGETs++;
await BoardsRepository.updateBoardAsync(b);
var postToPost = new PostData
{
UserID = userID,
ImageID = imageID,
Username = postUsername,
Title = postTitle,
Content = postContent,
Interactions = 0,
CreatedAt = DateTimeOffset.Now.ToUnixTimeMilliseconds(),
Board = board.Tag,
IsLocked = false,
IsSticky = false,
GET = thisGET,
shouldAnon = postAnon
};
int postId = await PostsRepository.createPostAsync(postToPost);
await TheManager.bumpThreads(board);
if (postId != -1)
{
//Open post successfull
NavigationManager.NavigateTo($"/{board.Tag}/thread/{postId}", true, true);
Console.WriteLine("Post created");
}
else
{
//Open post unsucessfull
hasErr = true;
postErr = "There was an error and the post could not be created. Please notify the admin.";
Console.WriteLine("Shit sucks and did not work.");
}
}
}