diff --git a/MP.FileData/Controllers/FileController.cs b/MP.FileData/Controllers/FileController.cs index 76c8245d..9ff7ef26 100644 --- a/MP.FileData/Controllers/FileController.cs +++ b/MP.FileData/Controllers/FileController.cs @@ -101,7 +101,11 @@ namespace MP.FileData.Controllers } else { - fileMod.Add(file); + // verifico se data modifica e dimensione siano cambiati... + if (currRecord.LastMod != file.LastWriteTime || currRecord.Size != file.Length) + { + fileMod.Add(file); + } } } @@ -182,7 +186,7 @@ namespace MP.FileData.Controllers { dbResult = localDbCtx .DbSetProgFile - .Where(x => x.Path.StartsWith(path) && ((onlyActive && x.Active) || !onlyActive)) + .Where(x => x.Path.StartsWith(path) && ((onlyActive == x.Active) || !onlyActive)) .OrderBy(x => x.Name) .ToList(); } @@ -198,7 +202,7 @@ namespace MP.FileData.Controllers .DbSetProgFile .Include(m => m.Macchina) .Include(a => a.Articolo) - .Where(x => ((x.IdxMacchina == IdxMacchina || IdxMacchina == "0") && (x.CodArticolo == CodArticolo || CodArticolo == "ND" || CodArticolo.StartsWith("##") || string.IsNullOrEmpty(CodArticolo))) && (x.Name.Contains(SearchVal) || string.IsNullOrEmpty(SearchVal)) && (!OnlyMod || x.Changed)) + .Where(x => ((x.IdxMacchina == IdxMacchina || IdxMacchina == "0") && (x.CodArticolo == CodArticolo || CodArticolo == "ND" || CodArticolo.StartsWith("##") || string.IsNullOrEmpty(CodArticolo))) && (x.Name.Contains(SearchVal) || string.IsNullOrEmpty(SearchVal)) && (!OnlyMod || x.DiskState != FileState.Unchanged)) .OrderBy(x => x.Name) .ToList(); } @@ -215,33 +219,44 @@ namespace MP.FileData.Controllers { bool answ = false; DateTime adesso = DateTime.Now; - using (MoonPro_ProgContext localDbCtx = new MoonPro_ProgContext(_configuration)) + // MD5 hash + using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create()) { - // converto - List newRec = newFiles.Select(o => new DatabaseModels.FileModel() + using (MoonPro_ProgContext localDbCtx = new MoonPro_ProgContext(_configuration)) { - Active = true, - CodArticolo = "ND", - Changed = false, - IdxMacchina = idxMacchina, - LastCheck = adesso, - LastMod = o.LastWriteTime, - MimeType = o.Extension, - Name = o.Name, - Path = o.FullName, - Rev = 0, - Size = o.Length, - FileContent = File.ReadAllBytes(o.FullName) - }).ToList(); + // converto + List newRec = newFiles.Select(o => new DatabaseModels.FileModel() + { + Active = true, + CodArticolo = "ND", + DiskState = FileState.New, + IdxMacchina = idxMacchina, + LastCheck = adesso, + LastMod = o.LastWriteTime, + MimeType = o.Extension, + Name = o.Name, + Path = o.FullName, + Rev = 0, + Size = o.Length, + FileContent = File.ReadAllBytes(o.FullName) + }).ToList(); - // aggiungo in blocco - localDbCtx - .DbSetProgFile - .AddRange(newRec); + // calcolo MD5 + foreach (var item in newRec) + { + var hash = md5.ComputeHash(item.FileContent); + item.MD5 = BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant(); + } - // salvo - localDbCtx.SaveChanges(); - answ = true; + // aggiungo in blocco + localDbCtx + .DbSetProgFile + .AddRange(newRec); + + // salvo + localDbCtx.SaveChanges(); + answ = true; + } } return answ; } @@ -265,12 +280,7 @@ namespace MP.FileData.Controllers .FirstOrDefault(); if (currData != null) { - //// se ho modificato data --> cambio codice ordine! - //if (!localDbCtx.Entry(updItem).OriginalValues["DtOrder"].Equals(localDbCtx.Entry(updItem).CurrentValues["DtOrder"])) - //{ - // updItem.OrderCode = $"O{updItem.Plant.PlantCode}{updItem.DtOrder:yyMMddHHmm}"; - // updItem.OrderDesc = $"Ordine {updItem.Plant.PlantDesc} - {updItem.DtOrder}"; - //} + updItem.DiskState = FileState.Changed; localDbCtx.Entry(updItem).State = EntityState.Modified; localDbCtx.SaveChanges(); } @@ -358,33 +368,44 @@ namespace MP.FileData.Controllers { bool answ = false; DateTime adesso = DateTime.Now; - using (MoonPro_ProgContext localDbCtx = new MoonPro_ProgContext(_configuration)) + // MD5 hash + using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create()) { - // converto - List newRec = updFiles.Select(o => new DatabaseModels.FileModel() + using (MoonPro_ProgContext localDbCtx = new MoonPro_ProgContext(_configuration)) { - Active = true, - CodArticolo = "ND", - Changed = false, - IdxMacchina = idxMacchina, - LastCheck = adesso, - LastMod = o.LastWriteTime, - MimeType = o.Extension, - Name = o.Name, - Path = o.FullName, - Rev = 0, - Size = o.Length, - FileContent = File.ReadAllBytes(o.FullName) - }).ToList(); + // converto + List newRec = updFiles.Select(o => new DatabaseModels.FileModel() + { + Active = true, + DiskState = FileState.Changed, + CodArticolo = "ND", + FileContent = File.ReadAllBytes(o.FullName), + IdxMacchina = idxMacchina, + LastCheck = adesso, + LastMod = o.LastWriteTime, + MimeType = o.Extension, + Name = o.Name, + Path = o.FullName, + Rev = 0, + Size = o.Length + }).ToList(); - // aggiungo in blocco - localDbCtx - .DbSetProgFile - .UpdateRange(newRec); + // calcolo MD5 + foreach (var item in newRec) + { + var hash = md5.ComputeHash(item.FileContent); + item.MD5 = BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant(); + } - // salvo - localDbCtx.SaveChanges(); - answ = true; + // aggiungo in blocco + localDbCtx + .DbSetProgFile + .UpdateRange(newRec); + + // salvo + localDbCtx.SaveChanges(); + answ = true; + } } return answ; } diff --git a/MP.FileData/DatabaseModels/FileModel.cs b/MP.FileData/DatabaseModels/FileModel.cs index ea53fc82..1ba32544 100644 --- a/MP.FileData/DatabaseModels/FileModel.cs +++ b/MP.FileData/DatabaseModels/FileModel.cs @@ -30,10 +30,13 @@ namespace MP.FileData.DatabaseModels public long Size { get; set; } = 0; public string Path { get; set; } = ""; public string MimeType { get; set; } = ""; - public bool Changed { get; set; } = false; + public string MD5 { get; set; } = ""; + public FileState DiskState { get; set; } = FileState.New; public DateTime LastCheck { get; set; } = DateTime.Now.AddYears(-1); public byte[] FileContent { get; set; } + public ICollection Tags { get; set; } + [NotMapped] public string FileStringContent { diff --git a/MP.FileData/DatabaseModels/TagModel.cs b/MP.FileData/DatabaseModels/TagModel.cs new file mode 100644 index 00000000..849cb577 --- /dev/null +++ b/MP.FileData/DatabaseModels/TagModel.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +// +// This is here so CodeMaid doesn't reorganize this document +// +namespace MP.FileData.DatabaseModels +{ + [Table("Tags")] + public partial class TagModel + { + + [Key] + public string TagId { get; set; } + + public ICollection Files { get; set; } + } +} diff --git a/MP.FileData/Enum.cs b/MP.FileData/Enum.cs new file mode 100644 index 00000000..a514cd02 --- /dev/null +++ b/MP.FileData/Enum.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MP.FileData +{ + /// + /// Stato File + /// + public enum FileState + { + ND = 0, + New, + Changed, + Deleted, + Unchanged + } +} \ No newline at end of file diff --git a/MP.FileData/Migrations/20210903153915_InitDb.Designer.cs b/MP.FileData/Migrations/20210907100115_InitDb.Designer.cs similarity index 77% rename from MP.FileData/Migrations/20210903153915_InitDb.Designer.cs rename to MP.FileData/Migrations/20210907100115_InitDb.Designer.cs index 9960c4e3..4886b82e 100644 --- a/MP.FileData/Migrations/20210903153915_InitDb.Designer.cs +++ b/MP.FileData/Migrations/20210907100115_InitDb.Designer.cs @@ -10,7 +10,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace MP.FileData.Migrations { [DbContext(typeof(MoonPro_ProgContext))] - [Migration("20210903153915_InitDb")] + [Migration("20210907100115_InitDb")] partial class InitDb { protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -22,6 +22,21 @@ namespace MP.FileData.Migrations .HasAnnotation("ProductVersion", "5.0.9") .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + modelBuilder.Entity("FileModelTagModel", b => + { + b.Property("FilesFileId") + .HasColumnType("int"); + + b.Property("TagsTagId") + .HasColumnType("nvarchar(450)"); + + b.HasKey("FilesFileId", "TagsTagId"); + + b.HasIndex("TagsTagId"); + + b.ToTable("FileModelTagModel"); + }); + modelBuilder.Entity("MP.FileData.DatabaseModels.ArticoloModel", b => { b.Property("CodArticolo") @@ -60,12 +75,12 @@ namespace MP.FileData.Migrations b.Property("Active") .HasColumnType("bit"); - b.Property("Changed") - .HasColumnType("bit"); - b.Property("CodArticolo") .HasColumnType("nvarchar(450)"); + b.Property("DiskState") + .HasColumnType("int"); + b.Property("FileContent") .HasColumnType("varbinary(max)"); @@ -78,6 +93,9 @@ namespace MP.FileData.Migrations b.Property("LastMod") .HasColumnType("datetime2"); + b.Property("MD5") + .HasColumnType("nvarchar(max)"); + b.Property("MimeType") .HasColumnType("nvarchar(max)"); @@ -146,6 +164,31 @@ namespace MP.FileData.Migrations }); }); + modelBuilder.Entity("MP.FileData.DatabaseModels.TagModel", b => + { + b.Property("TagId") + .HasColumnType("nvarchar(450)"); + + b.HasKey("TagId"); + + b.ToTable("Tags"); + }); + + modelBuilder.Entity("FileModelTagModel", b => + { + b.HasOne("MP.FileData.DatabaseModels.FileModel", null) + .WithMany() + .HasForeignKey("FilesFileId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MP.FileData.DatabaseModels.TagModel", null) + .WithMany() + .HasForeignKey("TagsTagId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + modelBuilder.Entity("MP.FileData.DatabaseModels.FileModel", b => { b.HasOne("MP.FileData.DatabaseModels.ArticoloModel", "Articolo") diff --git a/MP.FileData/Migrations/20210903153915_InitDb.cs b/MP.FileData/Migrations/20210907100115_InitDb.cs similarity index 71% rename from MP.FileData/Migrations/20210903153915_InitDb.cs rename to MP.FileData/Migrations/20210907100115_InitDb.cs index 46126bf6..3c346aac 100644 --- a/MP.FileData/Migrations/20210903153915_InitDb.cs +++ b/MP.FileData/Migrations/20210907100115_InitDb.cs @@ -39,6 +39,17 @@ namespace MP.FileData.Migrations table.PrimaryKey("PK_Macchine", x => x.IdxMacchina); }); + migrationBuilder.CreateTable( + name: "Tags", + columns: table => new + { + TagId = table.Column(type: "nvarchar(450)", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Tags", x => x.TagId); + }); + migrationBuilder.CreateTable( name: "Files", columns: table => new @@ -54,7 +65,8 @@ namespace MP.FileData.Migrations Size = table.Column(type: "bigint", nullable: false), Path = table.Column(type: "nvarchar(max)", nullable: true), MimeType = table.Column(type: "nvarchar(max)", nullable: true), - Changed = table.Column(type: "bit", nullable: false), + MD5 = table.Column(type: "nvarchar(max)", nullable: true), + DiskState = table.Column(type: "int", nullable: false), LastCheck = table.Column(type: "datetime2", nullable: false), FileContent = table.Column(type: "varbinary(max)", nullable: true) }, @@ -75,6 +87,30 @@ namespace MP.FileData.Migrations onDelete: ReferentialAction.Restrict); }); + migrationBuilder.CreateTable( + name: "FileModelTagModel", + columns: table => new + { + FilesFileId = table.Column(type: "int", nullable: false), + TagsTagId = table.Column(type: "nvarchar(450)", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_FileModelTagModel", x => new { x.FilesFileId, x.TagsTagId }); + table.ForeignKey( + name: "FK_FileModelTagModel_Files_FilesFileId", + column: x => x.FilesFileId, + principalTable: "Files", + principalColumn: "FileId", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_FileModelTagModel_Tags_TagsTagId", + column: x => x.TagsTagId, + principalTable: "Tags", + principalColumn: "TagId", + onDelete: ReferentialAction.Cascade); + }); + migrationBuilder.InsertData( table: "Articoli", columns: new[] { "CodArticolo", "DescArticolo", "Disegno", "Tipo" }, @@ -85,6 +121,11 @@ namespace MP.FileData.Migrations columns: new[] { "IdxMacchina", "BasePath", "CodMacchina", "Descrizione", "ImgUrl", "Nome", "Note", "ShowOrder" }, values: new object[] { "0", "", "0", "--- Tutte ---", "", "--- Tutte ---", "", 0 }); + migrationBuilder.CreateIndex( + name: "IX_FileModelTagModel_TagsTagId", + table: "FileModelTagModel", + column: "TagsTagId"); + migrationBuilder.CreateIndex( name: "IX_Files_CodArticolo", table: "Files", @@ -98,9 +139,15 @@ namespace MP.FileData.Migrations protected override void Down(MigrationBuilder migrationBuilder) { + migrationBuilder.DropTable( + name: "FileModelTagModel"); + migrationBuilder.DropTable( name: "Files"); + migrationBuilder.DropTable( + name: "Tags"); + migrationBuilder.DropTable( name: "Articoli"); diff --git a/MP.FileData/Migrations/MoonPro_ProgContextModelSnapshot.cs b/MP.FileData/Migrations/MoonPro_ProgContextModelSnapshot.cs index 7149d346..3ab6f202 100644 --- a/MP.FileData/Migrations/MoonPro_ProgContextModelSnapshot.cs +++ b/MP.FileData/Migrations/MoonPro_ProgContextModelSnapshot.cs @@ -20,6 +20,21 @@ namespace MP.FileData.Migrations .HasAnnotation("ProductVersion", "5.0.9") .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + modelBuilder.Entity("FileModelTagModel", b => + { + b.Property("FilesFileId") + .HasColumnType("int"); + + b.Property("TagsTagId") + .HasColumnType("nvarchar(450)"); + + b.HasKey("FilesFileId", "TagsTagId"); + + b.HasIndex("TagsTagId"); + + b.ToTable("FileModelTagModel"); + }); + modelBuilder.Entity("MP.FileData.DatabaseModels.ArticoloModel", b => { b.Property("CodArticolo") @@ -58,12 +73,12 @@ namespace MP.FileData.Migrations b.Property("Active") .HasColumnType("bit"); - b.Property("Changed") - .HasColumnType("bit"); - b.Property("CodArticolo") .HasColumnType("nvarchar(450)"); + b.Property("DiskState") + .HasColumnType("int"); + b.Property("FileContent") .HasColumnType("varbinary(max)"); @@ -76,6 +91,9 @@ namespace MP.FileData.Migrations b.Property("LastMod") .HasColumnType("datetime2"); + b.Property("MD5") + .HasColumnType("nvarchar(max)"); + b.Property("MimeType") .HasColumnType("nvarchar(max)"); @@ -144,6 +162,31 @@ namespace MP.FileData.Migrations }); }); + modelBuilder.Entity("MP.FileData.DatabaseModels.TagModel", b => + { + b.Property("TagId") + .HasColumnType("nvarchar(450)"); + + b.HasKey("TagId"); + + b.ToTable("Tags"); + }); + + modelBuilder.Entity("FileModelTagModel", b => + { + b.HasOne("MP.FileData.DatabaseModels.FileModel", null) + .WithMany() + .HasForeignKey("FilesFileId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MP.FileData.DatabaseModels.TagModel", null) + .WithMany() + .HasForeignKey("TagsTagId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + modelBuilder.Entity("MP.FileData.DatabaseModels.FileModel", b => { b.HasOne("MP.FileData.DatabaseModels.ArticoloModel", "Articolo")