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
|
2023-02-02 17:50:50 +00:00
|
|
|
@using ImageBoardServerApp.Auth
|
2023-01-18 12:56:24 +00:00
|
|
|
@using ImageBoardServerApp.Data
|
|
|
|
@using ImageBoardServerApp.Data.Repository
|
|
|
|
|
|
|
|
@inject NavigationManager NavigationManager
|
2023-01-27 22:50:12 +00:00
|
|
|
@inject IWebHostEnvironment env
|
2023-02-02 17:50:50 +00:00
|
|
|
@inject AuthenticationStateProvider authStateProvider
|
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>
|
|
|
|
|
2023-01-27 21:09:16 +00:00
|
|
|
<div class="centered formContent">
|
|
|
|
<div>
|
|
|
|
<div class="pd centered marg">
|
2023-02-02 17:50:50 +00:00
|
|
|
<RadzenTextBox Placeholder="Username (Anonymous)" MaxLength="15" @bind-Value="@postUsername" Class="w-100"/>
|
2023-01-27 21:09:16 +00:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="pd centered marg">
|
2023-02-02 17:50:50 +00:00
|
|
|
<RadzenTextBox Placeholder="Title" MaxLength="20" @bind-Value="@postTitle" Class="w-100"/>
|
2023-01-27 21:09:16 +00:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="pd centered marg">
|
2023-02-02 17:50:50 +00:00
|
|
|
<RadzenTextArea Placeholder="Content..." @bind-Value="@postContent" Cols="30" Rows="6" Class="w-100"/>
|
2023-01-27 21:09:16 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2023-01-18 12:56:24 +00:00
|
|
|
</div>
|
2023-02-02 21:27:31 +00:00
|
|
|
@if (hasErr)
|
2023-02-02 21:13:03 +00:00
|
|
|
{
|
|
|
|
<span class="postError">@postErr</span>
|
|
|
|
}
|
2023-02-02 21:27:31 +00:00
|
|
|
<div class="pd centered marg">
|
|
|
|
<FormInfo/>
|
|
|
|
<InputFile OnChange="@SingleUpload" type="file" accept="image/*"/>
|
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();
|
|
|
|
|
2023-02-02 17:50:50 +00:00
|
|
|
string postUsername { get; set; } = "Anonymous";
|
|
|
|
string postTitle { get; set; } = "";
|
|
|
|
string postContent { get; set; } = "";
|
|
|
|
|
2023-01-18 12:56:24 +00:00
|
|
|
|
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
|
|
|
selectedFile = e.GetMultipleFiles()[0];
|
|
|
|
this.StateHasChanged();
|
2023-01-18 12:56:24 +00:00
|
|
|
}
|
|
|
|
|
2023-02-02 21:13:03 +00:00
|
|
|
string postErr { get; set; }
|
|
|
|
bool hasErr { get; set; } = false;
|
|
|
|
|
2023-01-18 12:56:24 +00:00
|
|
|
private async Task onPostClick()
|
|
|
|
{
|
2023-02-01 20:49:02 +00:00
|
|
|
|
2023-02-02 17:50:50 +00:00
|
|
|
var cauthStateProvder = (CustomAuthenticationStateProvider)authStateProvider;
|
|
|
|
var user = await cauthStateProvder.GetAuthenticationStateAsync();
|
|
|
|
var usr = user.User;
|
|
|
|
UserData foundusr = await UsersRepository.getUserByEmailAsync(usr.Identity.Name);
|
2023-02-02 21:13:03 +00:00
|
|
|
if (foundusr == null)
|
|
|
|
{
|
|
|
|
hasErr = true;
|
|
|
|
postErr = "You are not logged in.";
|
|
|
|
return;
|
|
|
|
}
|
2023-02-02 17:50:50 +00:00
|
|
|
int userID = foundusr.UserID;
|
2023-02-02 21:13:03 +00:00
|
|
|
if (foundusr.TimeBanned != -1)
|
|
|
|
{
|
2023-02-02 21:27:31 +00:00
|
|
|
hasErr = true;
|
|
|
|
postErr = "You are banned and may not post.";
|
|
|
|
//Maybe redirect to /banned?
|
2023-02-02 17:50:50 +00:00
|
|
|
return;
|
2023-02-02 21:13:03 +00:00
|
|
|
}
|
2023-02-02 17:50:50 +00:00
|
|
|
foundusr.lastActionTimeStamp = DateTimeOffset.UnixEpoch.ToUnixTimeMilliseconds();
|
|
|
|
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
|
|
|
|
2023-02-02 17:50:50 +00:00
|
|
|
if (selectedFile == null || selectedFile.Size >= 512000 * 4)
|
2023-01-27 22:50:12 +00:00
|
|
|
{
|
2023-02-02 21:27:31 +00:00
|
|
|
hasErr = true;
|
|
|
|
postErr = "You did not attach a file or the selected file is bigger then the 2MiB file limit.";
|
2023-01-27 22:50:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Stream stream = selectedFile.OpenReadStream(maxAllowedSize: 512000 * 4); // max 2MB
|
2023-02-01 13:18:36 +00:00
|
|
|
var file = Path.GetRandomFileName() + "." + selectedFile.Name.Split(".")[selectedFile.Name.Split(".").Length - 1];
|
2023-02-07 20:47:28 +00:00
|
|
|
var path = $"{env.WebRootPath}/img/dynamic/op/{@board.Tag}/{@file}";
|
2023-01-27 22:50:12 +00:00
|
|
|
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,
|
2023-02-07 20:47:28 +00:00
|
|
|
ImageLocation = $"/img/dynamic/op/{@board.Tag}/{@file}"
|
2023-01-18 12:56:24 +00:00
|
|
|
};
|
|
|
|
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
|
2023-01-26 12:04:55 +00:00
|
|
|
NavigationManager.NavigateTo($"/{board.Tag}/thread/{postId}", true, true);
|
2023-01-18 12:56:24 +00:00
|
|
|
Console.WriteLine("Post created");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//Open post unsucessfull
|
2023-02-02 21:27:31 +00:00
|
|
|
hasErr = true;
|
|
|
|
postErr = "There was an error and the post could not be created. Please notify the admin.";
|
2023-01-18 12:56:24 +00:00
|
|
|
Console.WriteLine("Shit sucks and did not work.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|