123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- @page "/docedit"
- @page "/docedit/{docID:int}"
- @using HyperCube.Models;
- @inject NavigationManager NavigationManager;
- @inject IJSRuntime JsRuntime;
- @attribute [Authorize]
- @*@attribute [Authorize(Roles = "admin")]*@
- <EditForm Model="@articleModel" OnValidSubmit="@HandleValidSubmit">
- <DataAnnotationsValidator />
- <ValidationSummary />
- <h1>@header</h1>
- <br>
- <div style="width: 50%;">
- @if (docID < 1)
- {
- <p><InputFile id="inputDefault" OnChange="@HandleSelection" accept="application/pdf" /></p>
- //accept="application/pdf, application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document"
- }
- else
- {
- //<p>Исходный документ: <a href="" @onclick="@DownloadDoc" @onclick:preventDefault>@articleModel.Filename</a></p>
- <p>Исходный документ: <a href=@FOLDER_NAME download="@articleModel.Filename" target="_top">@articleModel.Filename</a></p>
- }
- <p><InputText id="article_name" class="form-control" @bind-Value="articleModel.Name" placeholder="Наименование статьи" /></p>
- <p><InputDate id="date_publish" class="form-control" @bind-Value="articleModel.PublishDate" placeholder="Дата издания" /></p>
- <p><InputText id="author" class="form-control" @bind-Value="articleModel.Authors" placeholder="Автор" /></p>
- <p><InputTextArea id="keywords" class="form-control" @bind-Value="articleModel.Keywords" placeholder="Ключевые слова" /></p>
- <p><InputTextArea rows="5" id="annotation" class="form-control" @bind-Value="articleModel.Annotation" placeholder="Аннотация" /></p>
- <p><InputTextArea rows="10" id="text" class="form-control" @bind-Value="articleModel.Text" placeholder="Текст" /></p>
- @if (docID < 1)
- {
- <p><button class="btn btn-primary" type="submit">Загрузить</button></p>
- }
- else
- {
- <p>
- <button class="btn btn-danger" type="button" @onclick="@Cancel">Отклонить</button>
- <button class="btn btn-primary" type="submit">Утвердить</button>
- </p>
- }
- <p>Статус: @status</p>
- </div>
- </EditForm>
- @code {
- [Parameter]
- public int docID { get; set; }
- const string FOLDER_NAME = "articles_storage";
- const long MAX_FILE_SIZE = 5120000; //bytes
- ArticleModel articleModel = new ArticleModel();
- string status;
- string header;
- string storageFolderPath;
- MemoryStream memoryStream;
- protected override async Task OnInitializedAsync()
- {
- string path = AppDomain.CurrentDomain.BaseDirectory;
- storageFolderPath = (Path.Combine(path, FOLDER_NAME));
- if (docID > 0)
- {
- header = "Проверка материала";
- MySQLConnector dbCon = MySQLConnector.Instance();
- string stringSQL = $"SELECT id, filename, article_name, authors, date_publish, annotation, keywords FROM articles WHERE id={docID}";
- articleModel = await dbCon.SQLSelectArticle(stringSQL);
- status = $"Article ID={docID} loaded.";
- }
- else
- header = "Загрузка материала";
- }
- private async void HandleValidSubmit()
- {
- MySQLConnector dbCon = MySQLConnector.Instance();
- string stringSQL = $"INSERT INTO articles (filename, article_name, authors, date_publish, annotation, keywords)" +
- $"VALUES ('{articleModel.Filename}', '{articleModel.Name}', '{articleModel.Authors}', '{articleModel.PublishDate.ToString("yyyy-MM-dd")}'," +
- $"'{articleModel.Annotation}', '{articleModel.Keywords}')";
- long id = dbCon.SQLInsert(stringSQL);
- stringSQL = $"INSERT INTO actions_history (article_id, action_type, acc_id)" +
- $"VALUES ('{id}', {1}, {1})";
- dbCon.SQLInsert(stringSQL);
- dbCon.Close();
- //Console.WriteLine($"memorystream: {memoryStream.Length}, filename: {articleModel.Filename}");
- string fullpath = Path.Combine(storageFolderPath, $"{id}_{articleModel.Filename}");
- Directory.CreateDirectory(storageFolderPath);
- FileStream fs = new(fullpath, FileMode.Create, FileAccess.Write);
- memoryStream.Position = 0;
- await memoryStream.CopyToAsync(fs);
- Console.WriteLine($"User has saved new article data, {id}_{articleModel.Filename}, memory size:{memoryStream.Length}b, file size: {fs.Length}b");
- memoryStream.Close();
- fs.Close();
- status = "New article data saved.";
- bool confirmed = await JsRuntime.InvokeAsync<bool>("confirm", "Хотите загрузить еще статью?");
- if (confirmed)
- NavigationManager.NavigateTo("docedit", true);
- else
- NavigationManager.NavigateTo("");
- }
- private async Task HandleSelection(InputFileChangeEventArgs e)
- {
- IBrowserFile file = e.File;
- if (file != null)
- {
- Stream stream = file.OpenReadStream(MAX_FILE_SIZE);
- memoryStream = new();
- await stream.CopyToAsync(memoryStream);
- status = $"Finished loading {memoryStream.Length} bytes from {file.Name}";
- DocParse docParse = new DocParse();
- articleModel = DocParse.ReadPDF(memoryStream);
- articleModel.Filename = file.Name;
- }
- }
- private async void Cancel()
- {
- bool confirmed = await JsRuntime.InvokeAsync<bool>("confirm", "Вы уверены, что хотите отклонить статью?");
- if (confirmed)
- {
- ///какая-то логика отмены...
- NavigationManager.NavigateTo("");
- }
- }
- //private async void DownloadDoc()
- //{
- // string fullpath = Path.Combine(storageFolderPath, $"{docID}_{articleModel.Filename}");
- // if (File.Exists(fullpath))
- // {
- // //await JsRuntime.InvokeAsync<string>("downloadFile", fullpath);
- // }
- // else
- // {
- // Console.WriteLine($"File {fullpath} not found.");
- // status = $"File {fullpath} not found.";
- // }
- //}
- }
|