🔥 Clean up project, switch to base64 images

pull/3/head
Alexis Drai 2 years ago
parent e5aad86370
commit 47c4882ff3

@ -15,7 +15,8 @@ namespace blazor_lab.Factories
EnchantCategories = item.EnchantCategories,
MaxDurability = item.MaxDurability,
StackSize = item.StackSize,
ImageContent = imageContent
ImageContent = imageContent,
ImageBase64 = string.IsNullOrWhiteSpace(item.ImageBase64) ? Convert.ToBase64String(imageContent) : item.ImageBase64
};
}
@ -30,7 +31,8 @@ namespace blazor_lab.Factories
EnchantCategories = model.EnchantCategories,
MaxDurability = model.MaxDurability,
StackSize = model.StackSize,
CreatedDate = DateTime.Now
CreatedDate = DateTime.Now,
ImageBase64 = Convert.ToBase64String(model.ImageContent)
};
}
@ -43,6 +45,7 @@ namespace blazor_lab.Factories
item.MaxDurability = model.MaxDurability;
item.StackSize = model.StackSize;
item.UpdatedDate = DateTime.Now;
item.ImageBase64 = Convert.ToBase64String(model.ImageContent);
}
}
}

@ -1,9 +0,0 @@
namespace blazor_lab.Models
{
public class Cake
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Cost { get; set; }
}
}

@ -11,5 +11,6 @@
public List<string> RepairWith { get; set; }
public DateTime CreatedDate { get; set; }
public DateTime? UpdatedDate { get; set; }
public string ImageBase64 { get; set; }
}
}

@ -32,5 +32,6 @@ namespace blazor_lab.Models
[Required(ErrorMessage = "img mandatory")]
public byte[] ImageContent { get; set; }
public string ImageBase64 { get; set; }
}
}

@ -61,14 +61,7 @@
<p>
<label>
Current Item image:
@if (File.Exists($"{WebHostEnvironment.WebRootPath}/images/{itemModel.Name}.png"))
{
<img src="images/@(itemModel.Name).png" class="img-thumbnail" title="@itemModel.DisplayName" alt="@itemModel.DisplayName" style="max-width: 150px" />
}
else
{
<img src="images/default.png" class="img-thumbnail" title="@itemModel.DisplayName" alt="@itemModel.DisplayName" style="max-width: 150px" />
}
<img src="data:image/png;base64, @(itemModel.ImageBase64)" class="img-thumbnail" title="@itemModel.DisplayName" alt="@itemModel.DisplayName" style="min-width: 50px; max-width: 150px" />
</label>
</p>
<p>

@ -43,16 +43,7 @@ namespace blazor_lab.Pages
{
var item = await DataService.GetById(Id);
byte[] fileContent;
if (File.Exists($"{WebHostEnvironment.WebRootPath}/images/{itemModel.Name}.png"))
{
fileContent = await File.ReadAllBytesAsync($"{WebHostEnvironment.WebRootPath}/images/{item.Name}.png");
}
else
{
fileContent = await File.ReadAllBytesAsync($"{WebHostEnvironment.WebRootPath}/images/default.png");
}
var fileContent = await File.ReadAllBytesAsync($"{WebHostEnvironment.WebRootPath}/images/default.png");
// Set the model with the item
itemModel = ItemFactory.ToModel(item, fileContent);

@ -22,9 +22,9 @@
<DataGridColumn TItem="Item" Field="@nameof(Item.Id)" Caption="#" />
<DataGridColumn TItem="Item" Field="@nameof(Item.Id)" Caption="Image">
<DisplayTemplate>
@if (File.Exists($"{WebHostEnvironment.WebRootPath}/images/{context.Name}.png"))
@if (!string.IsNullOrWhiteSpace(context.ImageBase64))
{
<img src="images/@(context.Name).png" class="img-thumbnail" title="@context.DisplayName" alt="@context.DisplayName" style="max-width: 150px" />
<img src="data:image/png;base64, @(context.ImageBase64)" class="img-thumbnail" title="@context.DisplayName" alt="@context.DisplayName" style="min-width: 50px; max-width: 150px" />
}
else
{

@ -41,20 +41,7 @@ namespace blazor_lab.Services
// Add the item to the current data
currentData.Add(ItemFactory.Create(model));
// Save the image
var imagePathInfo = new DirectoryInfo($"{_webHostEnvironment.WebRootPath}/images");
// Check if the folder "images" exist
if (!imagePathInfo.Exists)
{
imagePathInfo.Create();
}
// Determine the image name
var fileName = new FileInfo($"{imagePathInfo}/{model.Name}.png");
// Write the file content
await File.WriteAllBytesAsync(fileName.FullName, model.ImageContent);
// Save the data
await _localStorageService.SetItemAsync("data", currentData);
@ -80,14 +67,6 @@ namespace blazor_lab.Services
var item = currentData.FirstOrDefault(w => w.Id == id);
currentData.Remove(item);
var imagePathInfo = new DirectoryInfo($"{_webHostEnvironment.WebRootPath}/images");
var fileInfo = new FileInfo($"{imagePathInfo}/{item.Name}.png");
if (fileInfo.Exists)
{
File.Delete(fileInfo.FullName);
}
await _localStorageService.SetItemAsync("data", currentData);
}
@ -139,21 +118,7 @@ namespace blazor_lab.Services
{
var currentData = await _localStorageService.GetItemAsync<List<Item>>("data");
var item = await GetById(id);
var imagePathInfo = new DirectoryInfo($"{_webHostEnvironment.WebRootPath}/images");
if (!imagePathInfo.Exists)
{
imagePathInfo.Create();
}
if (item.Name != model.Name)
{
var oldFileName = new FileInfo($"{imagePathInfo}/{item.Name}.png");
if (oldFileName.Exists)
{
File.Delete(oldFileName.FullName);
}
}
var fileName = new FileInfo($"{imagePathInfo}/{model.Name}.png");
await File.WriteAllBytesAsync(fileName.FullName, model.ImageContent);
ItemFactory.Update(item, model);

@ -1,15 +0,0 @@
@inherits LayoutComponentBase
<header><h1>Welcome to admin</h1></header>
<nav>
<a href="/admin">home</a>
<a href="/admin/users">users</a>
</nav>
<div class="container">
<main role="main" class="pb-3">
@Body
</main>
</div>

@ -1,16 +0,0 @@
<div class="alert alert-secondary mt-4">
<span class="oi oi-pencil me-2" aria-hidden="true"></span>
<strong>@Title</strong>
<span class="text-nowrap">
Please take our
<a target="_blank" class="font-weight-bold link-dark" href="https://go.microsoft.com/fwlink/?linkid=2149017">brief survey</a>
</span>
and tell us what you think.
</div>
@code {
// Demonstrates how a parent component can supply parameters
[Parameter]
public string? Title { get; set; }
}

@ -1,28 +0,0 @@
@typeparam TItem
@using System.Diagnostics.CodeAnalysis
<table class="table">
<thead>
<tr>@TableHeader</tr>
</thead>
<tbody>
@foreach (var item in Items)
{
if (RowTemplate is not null)
{
<tr>@RowTemplate(item)</tr>
}
}
</tbody>
</table>
@code {
[Parameter]
public RenderFragment? TableHeader { get; set; }
[Parameter]
public RenderFragment<TItem>? RowTemplate { get; set; }
[Parameter, AllowNull]
public IReadOnlyList<TItem> Items { get; set; }
}

@ -1,36 +0,0 @@
TP2
1
A
Le problème reader/writer est différent.
Dans notre cas, on veut que des clients et des patissiers accèdent à une ressource partagée ; l'une en "lecture", l'autre en "écriture"
Mais on veut aussi empêcher un client de prendre une patisserie s'il n'yen a aucune de prete, par exemple.
Ca ressemble beaucoup plus à un problème producer/consumer.
B
La boutique peut jouer le role de moniteur, comme elle gère les inputs outputs et connait le stock
Elle définit un buffer de taille n, puis
put() {
if(getstock() == n) {
notFull.await(); // ne pas ajouter si plein
}
/*
do the putting ...
*/
// stock++ if necessary
notEmpty.signal(); // annoncer que non vide
}
Symmétriquement,
get() {
if(getstock() == 0) {
notEmpty.await(); // ne pas prendre si vide
}
/*
do the getting ...
*/
// stock-- if necessary
notFull.signal(); // annoncer que non plein
}
Avec un arrayList, on pourra / devra implémenter un buffer circulaire, càd tracer la tete et la queue en indexes variables qu'on incrémente, modulo, tout le bazar
Depuis le main, on start() les Customers et PastryChefs qui héritent de Runnable
Loading…
Cancel
Save