169 lines
No EOL
5.3 KiB
Text
169 lines
No EOL
5.3 KiB
Text
@using System.ComponentModel.DataAnnotations
|
|
@using ImageBoardServerApp.Data.Repository
|
|
|
|
@inject NavigationManager navigationManager
|
|
@inject IWebHostEnvironment env
|
|
|
|
<div>
|
|
<span>[</span>
|
|
<a class="toggleOpened" onclick="@ToggleOpened">@toggleText</a>
|
|
<span>]</span>
|
|
</div>
|
|
@if (opened)
|
|
{
|
|
<div class="pd centered">
|
|
<span>Comment on @post.Title in /@post.Board/</span>
|
|
|
|
<div class="centered formContent">
|
|
<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">
|
|
<RadzenTextArea Placeholder="Comment..." @bind-Value="@postContent" Cols="30" Rows="6" Change=@(args => OnChange(args, "Content")) Class="w-100"/>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="pd centered marg">
|
|
<FormInfo/>
|
|
<InputFile OnChange="@SingleUpload" type="file" accept="image/*"/>
|
|
<RadzenButton class="pd" Click="@onPostClick" Text="Post!"></RadzenButton>
|
|
</div>
|
|
</div>
|
|
}
|
|
|
|
|
|
@code {
|
|
|
|
private bool opened = false;
|
|
|
|
private string toggleText = "Open Comment Formula";
|
|
|
|
private void ToggleOpened()
|
|
{
|
|
opened = !opened;
|
|
toggleText = opened ? "Close Comment Formula" : "Open Comment Formula";
|
|
}
|
|
|
|
[Parameter]
|
|
[Required]
|
|
public PostData post { get; set; }
|
|
|
|
string postUsername = "Anonymous";
|
|
string postContent = "";
|
|
|
|
void OnChange(string value, string name)
|
|
{
|
|
switch (name)
|
|
{
|
|
case "username":
|
|
postUsername = value;
|
|
if (value == "")
|
|
{
|
|
postUsername = "Anonymous";
|
|
}
|
|
break;
|
|
case "content":
|
|
postContent = value;
|
|
break;
|
|
default:
|
|
Console.WriteLine("not found.");
|
|
break;
|
|
}
|
|
Console.WriteLine($"Smth changed!: {value}");
|
|
}
|
|
|
|
private IBrowserFile selectedFile;
|
|
|
|
private async Task SingleUpload(InputFileChangeEventArgs e)
|
|
{
|
|
selectedFile = e.GetMultipleFiles()[0];
|
|
this.StateHasChanged();
|
|
}
|
|
|
|
private async Task onPostClick()
|
|
{
|
|
var userToCreate = new UserData
|
|
{
|
|
Email = "dev@limited-dev.de",
|
|
Password = "$2a$10$C/ZPY5aVGkImLGyIP0SySuQaEYIwnY0J99i/m6tqqf6tMkax89Eku",
|
|
PermissionInteger = 100,
|
|
TimeBanned = -1,
|
|
lastActionTimeStamp = DateTime.Now.Millisecond
|
|
};
|
|
|
|
int userID;
|
|
UserData foundusr = await UsersRepository.getUserByEmailAsync(userToCreate.Email);
|
|
if (foundusr == null)
|
|
{
|
|
userID = await UsersRepository.createUserAsync(userToCreate);
|
|
}
|
|
else
|
|
{
|
|
userID = foundusr.UserID;
|
|
if(foundusr.TimeBanned != -1)
|
|
return;
|
|
foundusr.lastActionTimeStamp = DateTimeOffset.UnixEpoch.ToUnixTimeMilliseconds();
|
|
await UsersRepository.updateUserAsync(foundusr);
|
|
}
|
|
|
|
bool hasImage = selectedFile != null;
|
|
|
|
CommentData commentToCreate;
|
|
if (hasImage)
|
|
{
|
|
Stream stream = selectedFile.OpenReadStream(maxAllowedSize: 512000 * 4); // max 2MB
|
|
var file = Path.GetRandomFileName() + "." + selectedFile.Name.Split(".")[selectedFile.Name.Split(".").Length - 1];
|
|
var path = $"{env.WebRootPath}\\img\\dynamic\\comment\\{@post.Board}\\{file}";
|
|
FileStream fs = File.Create(path);
|
|
await stream.CopyToAsync(fs);
|
|
stream.Close();
|
|
fs.Close();
|
|
|
|
|
|
var imageToUpload = new ImageData
|
|
{
|
|
Board = post.Board,
|
|
ImageLocation = $"\\img\\dynamic\\comment\\{post.Board}\\{file}"
|
|
};
|
|
int imageID = await ImagesRepository.createImageAsync(imageToUpload);
|
|
commentToCreate = new CommentData()
|
|
{
|
|
PostID = post.PostID,
|
|
UserID = userID,
|
|
ImageID = imageID,
|
|
Content = postContent,
|
|
Username = postUsername,
|
|
Board = post.Board,
|
|
CreatedAt = DateTimeOffset.Now.ToUnixTimeMilliseconds()
|
|
};
|
|
}
|
|
else
|
|
{
|
|
commentToCreate = new CommentData()
|
|
{
|
|
PostID = post.PostID,
|
|
UserID = userID,
|
|
Content = postContent,
|
|
Username = postUsername,
|
|
Board = post.Board,
|
|
CreatedAt = DateTimeOffset.Now.ToUnixTimeMilliseconds()
|
|
};
|
|
}
|
|
|
|
int commentId = await CommentsRepository.createCommentAsync(commentToCreate);
|
|
if (commentId == -1)
|
|
{
|
|
//Open comment unsucessfull
|
|
navigationManager.NavigateTo("/UnSuccessfulPost");
|
|
Console.WriteLine("Shit sucks and did not work.");
|
|
return;
|
|
}
|
|
//comment successfull
|
|
Console.WriteLine("Post created");
|
|
navigationManager.NavigateTo($"/{post.Board}/thread/{post.PostID}", true, true);
|
|
opened = false;
|
|
}
|
|
} |