Limiter la taille des fichiers sauvegardés dans un SqlFileView

0000003199     -      02/01/2024

Le composant SqlFileView de Mercator dispose d'un événement UploadingFile permettant d'exécuter du code personnalisé quand le fichier va être sauvegardé dans la base de données. Il permet ces deux applications :

  • empêcher de sauvegarder dans la base de données un fichier qui dépasse une taille maximale fixée (dans notre exemple 1024 kb)
  • si ce fichier est une image, la redimensionner pour diminuer sa taille

Ces deux fonctionnalités sont prises en charge par le customizer repris ci-dessous. L'exemple est donné pour un SqlFileView se trouvant dans le signalétique des clients.

Zoom
using System;
using System.Collections.Generic;
using System.Text;
using MercatorApi;
using MercatorUi;
using System.Linq;
using System.Drawing;

// <CompileWithRoslyn />

namespace SigCli
{
    public class Customizer : MercatorUi.ICustomizers.IFormLoadCustomizer, MercatorUi.ICustomizers.IFormClosedCustomizer
    {

        public void FormLoadCustomize(System.Windows.Forms.Form form)
        {
            MercatorUi.Forms.Sig.SigForm sigForm = (MercatorUi.Forms.Sig.SigForm)form;
            MercatorUi.MovableControls.MovableSqlFileView movableSqlFileView = sigForm.MovableControls.Values.OfType<MercatorUi.MovableControls.MovableSqlFileView>().FirstOrDefault();
            if (movableSqlFileView != null)
                movableSqlFileView.UploadingFile += MovableSqlFileView_UploadingFile;
        }

        public void FormClosedCustomize(System.Windows.Forms.Form form)
        {
            MercatorUi.Forms.Sig.SigForm sigForm = (MercatorUi.Forms.Sig.SigForm)form;
            MercatorUi.MovableControls.MovableSqlFileView movableSqlFileView = sigForm.MovableControls.Values.OfType<MercatorUi.MovableControls.MovableSqlFileView>().FirstOrDefault();
            if (movableSqlFileView != null)
                movableSqlFileView.UploadingFile -= MovableSqlFileView_UploadingFile;
        }

        const int MAX_KB = 1024;
        const int MAX_WIDTH = 800;
        const int MAX_HEIGHT = 800;

        private void MovableSqlFileView_UploadingFile(object sender, MercatorUi._BaseClasses.SqlFileView.UploadingFileEventArgs e)
        {
            if (e.FileContent.Length / 1024 > MAX_KB)
            {
                if (e.FileName.EndsWith(".jpeg", StringComparison.InvariantCultureIgnoreCase)
                       || e.FileName.EndsWith(
".jpg", StringComparison.InvariantCultureIgnoreCase)
                       || e.FileName.EndsWith(
".png", StringComparison.InvariantCultureIgnoreCase))
                {
                    try
                    {
                        using (Bitmap bitmap = new Bitmap(e.Node.LocalFileName))
                        {
                            if ((bitmap.Width > MAX_WIDTH) || (bitmap.Height > MAX_HEIGHT))
                            {
                                var newImage = Api.ResizeImage(bitmap, new Size(MAX_WIDTH, MAX_HEIGHT));
                                
                                using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
                                {
                                    if (e.FileName.EndsWith(".jpeg", StringComparison.InvariantCultureIgnoreCase) || e.FileName.EndsWith(".jpg", StringComparison.InvariantCultureIgnoreCase))
                                        newImage.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                                    else
                                        newImage.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
                                    e.FileContent = memoryStream.ToArray();
                                }
                            }
                        }
                        Dialogs.Stop($"L'image \"{e.FileName}\" a été redimensionnée en {MAX_WIDTH}x{MAX_HEIGHT} !");
                    }
                    catch (Exception ex)
                    {
                        Dialogs.Stop(ex.Message);
                        e.Cancel = true;
                        return;
                    }
                }
                else
                {
                    Dialogs.Stop($"Le fichier \"{e.FileName}\" a une taille supérieure à {MAX_KB} kb !");
                    e.Cancel = true;
                }
            }
        }
    }
}