From 5b00f23b28e3a09a4120101a8be8802d009e5d84 Mon Sep 17 00:00:00 2001 From: Jake Mannens Date: Mon, 14 Aug 2023 00:29:38 +1000 Subject: Added functionality for ingest tagging and implicit tags --- DbMedia.cs | 4 + HyperBooruContext.cs | 5 + MediaController.cs | 8 +- Migrations/20230811002826_Initial.Designer.cs | 221 ----------------------- Migrations/20230811002826_Initial.cs | 180 ------------------- Migrations/20230812095148_Initial.Designer.cs | 239 +++++++++++++++++++++++++ Migrations/20230812095148_Initial.cs | 200 +++++++++++++++++++++ Migrations/HyperBooruDbContextModelSnapshot.cs | 18 ++ Pages/TagDefinitions.cshtml.cs | 2 +- Pages/ViewMedia.cshtml | 27 ++- Pages/ViewMedia.cshtml.cs | 25 ++- Pages/ViewMedia.cshtml.css | 4 + TagController.cs | 40 +++++ wwwroot/styles/global.css | 36 ++-- 14 files changed, 584 insertions(+), 425 deletions(-) delete mode 100644 Migrations/20230811002826_Initial.Designer.cs delete mode 100644 Migrations/20230811002826_Initial.cs create mode 100644 Migrations/20230812095148_Initial.Designer.cs create mode 100644 Migrations/20230812095148_Initial.cs diff --git a/DbMedia.cs b/DbMedia.cs index 311e412..016e7f7 100644 --- a/DbMedia.cs +++ b/DbMedia.cs @@ -13,6 +13,10 @@ public class DbMedia : DbObject { public string? LongDescription { get; set; } public virtual List UploadedFiles { get; set; } = new(); + public bool IsIngest => Tags + .Select(t => t.TagDefinition) + .Any(td => td.Source == TagSource.Internal && td.Name == "ingest"); + public DbMedia() => base.ObjectType = ObjectType.Media; diff --git a/HyperBooruContext.cs b/HyperBooruContext.cs index fecbc4c..3a9aa0f 100644 --- a/HyperBooruContext.cs +++ b/HyperBooruContext.cs @@ -28,5 +28,10 @@ public class HyperBooruDbContext : DbContext { modelBuilder.Entity().ToTable("Tags"); modelBuilder.Entity().ToTable("Media"); modelBuilder.Entity().ToTable("UploadedFiles"); + + modelBuilder.Entity().HasData(new DbTagDefinition[] { + new() { ObjectId = -1, Source = TagSource.Internal, Name = "nsfw" }, + new() { ObjectId = -2, Source = TagSource.Internal, Name = "ingest" } + }); } } \ No newline at end of file diff --git a/MediaController.cs b/MediaController.cs index 29a8f88..620b3ee 100644 --- a/MediaController.cs +++ b/MediaController.cs @@ -64,7 +64,7 @@ public class MediaController : Controller { if(w > image.Width || h > image.Height) return BadRequest("Requested thumbnail size is larger than original media"); - int width = (int) (w is not null ? w : image.Width * h / image.Height); + int width = (int) (w is not null ? w : image.Width * h / image.Height); int height = (int) (h is not null ? h : image.Height * w / image.Width); var thumbPath = config.GetPath(media, width, height); @@ -166,11 +166,17 @@ public class MediaController : Controller { .FirstOrDefault(m => m.Checksum == hash); if(media is null) { + var ingestTagDef = db.TagDefinitions + .First(td => td.Source == TagSource.Internal && td.Name == "ingest"); + media = new() { Checksum = hash, MimeType = mime, UploadedFiles = new() { fileRecord + }, + Tags = new() { + new() { TagDefinition = ingestTagDef } } }; diff --git a/Migrations/20230811002826_Initial.Designer.cs b/Migrations/20230811002826_Initial.Designer.cs deleted file mode 100644 index 9db9176..0000000 --- a/Migrations/20230811002826_Initial.Designer.cs +++ /dev/null @@ -1,221 +0,0 @@ -// -using System; -using HyperBooru; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; - -#nullable disable - -namespace HyperBooru.Migrations -{ - [DbContext(typeof(HyperBooruDbContext))] - [Migration("20230811002826_Initial")] - partial class Initial - { - /// - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder - .HasAnnotation("ProductVersion", "7.0.10") - .HasAnnotation("Proxies:ChangeTracking", false) - .HasAnnotation("Proxies:CheckEquality", false) - .HasAnnotation("Proxies:LazyLoading", true); - - modelBuilder.Entity("HyperBooru.DbObject", b => - { - b.Property("ObjectId") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("Guid") - .HasColumnType("TEXT"); - - b.Property("ObjectType") - .HasColumnType("INTEGER"); - - b.HasKey("ObjectId"); - - b.HasIndex("Guid"); - - b.ToTable("Objects", (string)null); - - b.UseTptMappingStrategy(); - }); - - modelBuilder.Entity("HyperBooru.DbUploadedFile", b => - { - b.Property("UploadedFileId") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("CreateTime") - .HasColumnType("TEXT"); - - b.Property("Filename") - .HasColumnType("TEXT"); - - b.Property("LastAccessTime") - .HasColumnType("TEXT"); - - b.Property("LastWriteTime") - .HasColumnType("TEXT"); - - b.Property("MediaObjectId") - .HasColumnType("INTEGER"); - - b.Property("OriginalChecksum") - .IsRequired() - .HasColumnType("TEXT"); - - b.Property("UploadTime") - .HasColumnType("TEXT"); - - b.HasKey("UploadedFileId"); - - b.HasIndex("MediaObjectId"); - - b.ToTable("UploadedFiles", (string)null); - }); - - modelBuilder.Entity("HyperBooru.DbMedia", b => - { - b.HasBaseType("HyperBooru.DbObject"); - - b.Property("Checksum") - .IsRequired() - .HasColumnType("TEXT"); - - b.Property("LongDescription") - .HasColumnType("TEXT"); - - b.Property("MimeType") - .IsRequired() - .HasColumnType("TEXT"); - - b.Property("ShortDescription") - .HasColumnType("TEXT"); - - b.ToTable("Media", (string)null); - }); - - modelBuilder.Entity("HyperBooru.DbTag", b => - { - b.HasBaseType("HyperBooru.DbObject"); - - b.Property("CreateTime") - .HasColumnType("TEXT"); - - b.Property("TagDefinitionObjectId") - .HasColumnType("INTEGER"); - - b.Property("TargetObjectId") - .HasColumnType("INTEGER"); - - b.HasIndex("TagDefinitionObjectId"); - - b.HasIndex("TargetObjectId"); - - b.ToTable("Tags", (string)null); - }); - - modelBuilder.Entity("HyperBooru.DbTagDefinition", b => - { - b.HasBaseType("HyperBooru.DbObject"); - - b.Property("DbTagDefinitionObjectId") - .HasColumnType("INTEGER"); - - b.Property("Name") - .IsRequired() - .HasColumnType("TEXT"); - - b.Property("Namespace") - .HasColumnType("TEXT"); - - b.Property("Source") - .HasColumnType("INTEGER"); - - b.HasIndex("DbTagDefinitionObjectId"); - - b.ToTable("TagDefinitions", (string)null); - }); - - modelBuilder.Entity("HyperBooru.DbUploadedFile", b => - { - b.HasOne("HyperBooru.DbMedia", "Media") - .WithMany("UploadedFiles") - .HasForeignKey("MediaObjectId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("Media"); - }); - - modelBuilder.Entity("HyperBooru.DbMedia", b => - { - b.HasOne("HyperBooru.DbObject", null) - .WithOne() - .HasForeignKey("HyperBooru.DbMedia", "ObjectId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - }); - - modelBuilder.Entity("HyperBooru.DbTag", b => - { - b.HasOne("HyperBooru.DbObject", null) - .WithOne() - .HasForeignKey("HyperBooru.DbTag", "ObjectId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("HyperBooru.DbTagDefinition", "TagDefinition") - .WithMany() - .HasForeignKey("TagDefinitionObjectId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("HyperBooru.DbObject", "Target") - .WithMany("Tags") - .HasForeignKey("TargetObjectId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("TagDefinition"); - - b.Navigation("Target"); - }); - - modelBuilder.Entity("HyperBooru.DbTagDefinition", b => - { - b.HasOne("HyperBooru.DbTagDefinition", null) - .WithMany("ImplicitTags") - .HasForeignKey("DbTagDefinitionObjectId"); - - b.HasOne("HyperBooru.DbObject", null) - .WithOne() - .HasForeignKey("HyperBooru.DbTagDefinition", "ObjectId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - }); - - modelBuilder.Entity("HyperBooru.DbObject", b => - { - b.Navigation("Tags"); - }); - - modelBuilder.Entity("HyperBooru.DbMedia", b => - { - b.Navigation("UploadedFiles"); - }); - - modelBuilder.Entity("HyperBooru.DbTagDefinition", b => - { - b.Navigation("ImplicitTags"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/Migrations/20230811002826_Initial.cs b/Migrations/20230811002826_Initial.cs deleted file mode 100644 index f3017e5..0000000 --- a/Migrations/20230811002826_Initial.cs +++ /dev/null @@ -1,180 +0,0 @@ -using System; -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -namespace HyperBooru.Migrations -{ - /// - public partial class Initial : Migration - { - /// - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "Objects", - columns: table => new - { - ObjectId = table.Column(type: "INTEGER", nullable: false) - .Annotation("Sqlite:Autoincrement", true), - Guid = table.Column(type: "TEXT", nullable: false), - ObjectType = table.Column(type: "INTEGER", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_Objects", x => x.ObjectId); - }); - - migrationBuilder.CreateTable( - name: "Media", - columns: table => new - { - ObjectId = table.Column(type: "INTEGER", nullable: false) - .Annotation("Sqlite:Autoincrement", true), - Checksum = table.Column(type: "TEXT", nullable: false), - MimeType = table.Column(type: "TEXT", nullable: false), - ShortDescription = table.Column(type: "TEXT", nullable: true), - LongDescription = table.Column(type: "TEXT", nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_Media", x => x.ObjectId); - table.ForeignKey( - name: "FK_Media_Objects_ObjectId", - column: x => x.ObjectId, - principalTable: "Objects", - principalColumn: "ObjectId", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateTable( - name: "TagDefinitions", - columns: table => new - { - ObjectId = table.Column(type: "INTEGER", nullable: false) - .Annotation("Sqlite:Autoincrement", true), - Source = table.Column(type: "INTEGER", nullable: false), - Namespace = table.Column(type: "TEXT", nullable: true), - Name = table.Column(type: "TEXT", nullable: false), - DbTagDefinitionObjectId = table.Column(type: "INTEGER", nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_TagDefinitions", x => x.ObjectId); - table.ForeignKey( - name: "FK_TagDefinitions_Objects_ObjectId", - column: x => x.ObjectId, - principalTable: "Objects", - principalColumn: "ObjectId", - onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_TagDefinitions_TagDefinitions_DbTagDefinitionObjectId", - column: x => x.DbTagDefinitionObjectId, - principalTable: "TagDefinitions", - principalColumn: "ObjectId"); - }); - - migrationBuilder.CreateTable( - name: "UploadedFiles", - columns: table => new - { - UploadedFileId = table.Column(type: "INTEGER", nullable: false) - .Annotation("Sqlite:Autoincrement", true), - OriginalChecksum = table.Column(type: "TEXT", nullable: false), - Filename = table.Column(type: "TEXT", nullable: true), - UploadTime = table.Column(type: "TEXT", nullable: false), - LastAccessTime = table.Column(type: "TEXT", nullable: true), - LastWriteTime = table.Column(type: "TEXT", nullable: true), - CreateTime = table.Column(type: "TEXT", nullable: true), - MediaObjectId = table.Column(type: "INTEGER", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_UploadedFiles", x => x.UploadedFileId); - table.ForeignKey( - name: "FK_UploadedFiles_Media_MediaObjectId", - column: x => x.MediaObjectId, - principalTable: "Media", - principalColumn: "ObjectId", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateTable( - name: "Tags", - columns: table => new - { - ObjectId = table.Column(type: "INTEGER", nullable: false) - .Annotation("Sqlite:Autoincrement", true), - TagDefinitionObjectId = table.Column(type: "INTEGER", nullable: false), - CreateTime = table.Column(type: "TEXT", nullable: false), - TargetObjectId = table.Column(type: "INTEGER", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_Tags", x => x.ObjectId); - table.ForeignKey( - name: "FK_Tags_Objects_ObjectId", - column: x => x.ObjectId, - principalTable: "Objects", - principalColumn: "ObjectId", - onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_Tags_Objects_TargetObjectId", - column: x => x.TargetObjectId, - principalTable: "Objects", - principalColumn: "ObjectId", - onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_Tags_TagDefinitions_TagDefinitionObjectId", - column: x => x.TagDefinitionObjectId, - principalTable: "TagDefinitions", - principalColumn: "ObjectId", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateIndex( - name: "IX_Objects_Guid", - table: "Objects", - column: "Guid"); - - migrationBuilder.CreateIndex( - name: "IX_TagDefinitions_DbTagDefinitionObjectId", - table: "TagDefinitions", - column: "DbTagDefinitionObjectId"); - - migrationBuilder.CreateIndex( - name: "IX_Tags_TagDefinitionObjectId", - table: "Tags", - column: "TagDefinitionObjectId"); - - migrationBuilder.CreateIndex( - name: "IX_Tags_TargetObjectId", - table: "Tags", - column: "TargetObjectId"); - - migrationBuilder.CreateIndex( - name: "IX_UploadedFiles_MediaObjectId", - table: "UploadedFiles", - column: "MediaObjectId"); - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "Tags"); - - migrationBuilder.DropTable( - name: "UploadedFiles"); - - migrationBuilder.DropTable( - name: "TagDefinitions"); - - migrationBuilder.DropTable( - name: "Media"); - - migrationBuilder.DropTable( - name: "Objects"); - } - } -} diff --git a/Migrations/20230812095148_Initial.Designer.cs b/Migrations/20230812095148_Initial.Designer.cs new file mode 100644 index 0000000..277e607 --- /dev/null +++ b/Migrations/20230812095148_Initial.Designer.cs @@ -0,0 +1,239 @@ +// +using System; +using HyperBooru; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace HyperBooru.Migrations +{ + [DbContext(typeof(HyperBooruDbContext))] + [Migration("20230812095148_Initial")] + partial class Initial + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.10") + .HasAnnotation("Proxies:ChangeTracking", false) + .HasAnnotation("Proxies:CheckEquality", false) + .HasAnnotation("Proxies:LazyLoading", true); + + modelBuilder.Entity("HyperBooru.DbObject", b => + { + b.Property("ObjectId") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Guid") + .HasColumnType("TEXT"); + + b.Property("ObjectType") + .HasColumnType("INTEGER"); + + b.HasKey("ObjectId"); + + b.HasIndex("Guid"); + + b.ToTable("Objects", (string)null); + + b.UseTptMappingStrategy(); + }); + + modelBuilder.Entity("HyperBooru.DbUploadedFile", b => + { + b.Property("UploadedFileId") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("CreateTime") + .HasColumnType("TEXT"); + + b.Property("Filename") + .HasColumnType("TEXT"); + + b.Property("LastAccessTime") + .HasColumnType("TEXT"); + + b.Property("LastWriteTime") + .HasColumnType("TEXT"); + + b.Property("MediaObjectId") + .HasColumnType("INTEGER"); + + b.Property("OriginalChecksum") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("UploadTime") + .HasColumnType("TEXT"); + + b.HasKey("UploadedFileId"); + + b.HasIndex("MediaObjectId"); + + b.ToTable("UploadedFiles", (string)null); + }); + + modelBuilder.Entity("HyperBooru.DbMedia", b => + { + b.HasBaseType("HyperBooru.DbObject"); + + b.Property("Checksum") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("LongDescription") + .HasColumnType("TEXT"); + + b.Property("MimeType") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("ShortDescription") + .HasColumnType("TEXT"); + + b.ToTable("Media", (string)null); + }); + + modelBuilder.Entity("HyperBooru.DbTag", b => + { + b.HasBaseType("HyperBooru.DbObject"); + + b.Property("CreateTime") + .HasColumnType("TEXT"); + + b.Property("TagDefinitionObjectId") + .HasColumnType("INTEGER"); + + b.Property("TargetObjectId") + .HasColumnType("INTEGER"); + + b.HasIndex("TagDefinitionObjectId"); + + b.HasIndex("TargetObjectId"); + + b.ToTable("Tags", (string)null); + }); + + modelBuilder.Entity("HyperBooru.DbTagDefinition", b => + { + b.HasBaseType("HyperBooru.DbObject"); + + b.Property("DbTagDefinitionObjectId") + .HasColumnType("INTEGER"); + + b.Property("Name") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Namespace") + .HasColumnType("TEXT"); + + b.Property("Source") + .HasColumnType("INTEGER"); + + b.HasIndex("DbTagDefinitionObjectId"); + + b.ToTable("TagDefinitions", (string)null); + + b.HasData( + new + { + ObjectId = -1, + Guid = new Guid("a03e1649-bd30-4db1-a81d-be05ba05f357"), + ObjectType = 0, + Name = "nsfw", + Source = 0 + }, + new + { + ObjectId = -2, + Guid = new Guid("3094fe00-8e41-4c08-8828-dea2ba878dd4"), + ObjectType = 0, + Name = "ingest", + Source = 0 + }); + }); + + modelBuilder.Entity("HyperBooru.DbUploadedFile", b => + { + b.HasOne("HyperBooru.DbMedia", "Media") + .WithMany("UploadedFiles") + .HasForeignKey("MediaObjectId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Media"); + }); + + modelBuilder.Entity("HyperBooru.DbMedia", b => + { + b.HasOne("HyperBooru.DbObject", null) + .WithOne() + .HasForeignKey("HyperBooru.DbMedia", "ObjectId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("HyperBooru.DbTag", b => + { + b.HasOne("HyperBooru.DbObject", null) + .WithOne() + .HasForeignKey("HyperBooru.DbTag", "ObjectId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("HyperBooru.DbTagDefinition", "TagDefinition") + .WithMany() + .HasForeignKey("TagDefinitionObjectId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("HyperBooru.DbObject", "Target") + .WithMany("Tags") + .HasForeignKey("TargetObjectId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("TagDefinition"); + + b.Navigation("Target"); + }); + + modelBuilder.Entity("HyperBooru.DbTagDefinition", b => + { + b.HasOne("HyperBooru.DbTagDefinition", null) + .WithMany("ImplicitTags") + .HasForeignKey("DbTagDefinitionObjectId"); + + b.HasOne("HyperBooru.DbObject", null) + .WithOne() + .HasForeignKey("HyperBooru.DbTagDefinition", "ObjectId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("HyperBooru.DbObject", b => + { + b.Navigation("Tags"); + }); + + modelBuilder.Entity("HyperBooru.DbMedia", b => + { + b.Navigation("UploadedFiles"); + }); + + modelBuilder.Entity("HyperBooru.DbTagDefinition", b => + { + b.Navigation("ImplicitTags"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Migrations/20230812095148_Initial.cs b/Migrations/20230812095148_Initial.cs new file mode 100644 index 0000000..40150bd --- /dev/null +++ b/Migrations/20230812095148_Initial.cs @@ -0,0 +1,200 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional + +namespace HyperBooru.Migrations +{ + /// + public partial class Initial : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Objects", + columns: table => new + { + ObjectId = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + Guid = table.Column(type: "TEXT", nullable: false), + ObjectType = table.Column(type: "INTEGER", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Objects", x => x.ObjectId); + }); + + migrationBuilder.CreateTable( + name: "Media", + columns: table => new + { + ObjectId = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + Checksum = table.Column(type: "TEXT", nullable: false), + MimeType = table.Column(type: "TEXT", nullable: false), + ShortDescription = table.Column(type: "TEXT", nullable: true), + LongDescription = table.Column(type: "TEXT", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Media", x => x.ObjectId); + table.ForeignKey( + name: "FK_Media_Objects_ObjectId", + column: x => x.ObjectId, + principalTable: "Objects", + principalColumn: "ObjectId", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "TagDefinitions", + columns: table => new + { + ObjectId = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + Source = table.Column(type: "INTEGER", nullable: false), + Namespace = table.Column(type: "TEXT", nullable: true), + Name = table.Column(type: "TEXT", nullable: false), + DbTagDefinitionObjectId = table.Column(type: "INTEGER", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_TagDefinitions", x => x.ObjectId); + table.ForeignKey( + name: "FK_TagDefinitions_Objects_ObjectId", + column: x => x.ObjectId, + principalTable: "Objects", + principalColumn: "ObjectId", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_TagDefinitions_TagDefinitions_DbTagDefinitionObjectId", + column: x => x.DbTagDefinitionObjectId, + principalTable: "TagDefinitions", + principalColumn: "ObjectId"); + }); + + migrationBuilder.CreateTable( + name: "UploadedFiles", + columns: table => new + { + UploadedFileId = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + OriginalChecksum = table.Column(type: "TEXT", nullable: false), + Filename = table.Column(type: "TEXT", nullable: true), + UploadTime = table.Column(type: "TEXT", nullable: false), + LastAccessTime = table.Column(type: "TEXT", nullable: true), + LastWriteTime = table.Column(type: "TEXT", nullable: true), + CreateTime = table.Column(type: "TEXT", nullable: true), + MediaObjectId = table.Column(type: "INTEGER", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_UploadedFiles", x => x.UploadedFileId); + table.ForeignKey( + name: "FK_UploadedFiles_Media_MediaObjectId", + column: x => x.MediaObjectId, + principalTable: "Media", + principalColumn: "ObjectId", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "Tags", + columns: table => new + { + ObjectId = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + TagDefinitionObjectId = table.Column(type: "INTEGER", nullable: false), + CreateTime = table.Column(type: "TEXT", nullable: false), + TargetObjectId = table.Column(type: "INTEGER", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Tags", x => x.ObjectId); + table.ForeignKey( + name: "FK_Tags_Objects_ObjectId", + column: x => x.ObjectId, + principalTable: "Objects", + principalColumn: "ObjectId", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_Tags_Objects_TargetObjectId", + column: x => x.TargetObjectId, + principalTable: "Objects", + principalColumn: "ObjectId", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_Tags_TagDefinitions_TagDefinitionObjectId", + column: x => x.TagDefinitionObjectId, + principalTable: "TagDefinitions", + principalColumn: "ObjectId", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.InsertData( + table: "Objects", + columns: new[] { "ObjectId", "Guid", "ObjectType" }, + values: new object[,] + { + { -2, new Guid("3094fe00-8e41-4c08-8828-dea2ba878dd4"), 0 }, + { -1, new Guid("a03e1649-bd30-4db1-a81d-be05ba05f357"), 0 } + }); + + migrationBuilder.InsertData( + table: "TagDefinitions", + columns: new[] { "ObjectId", "DbTagDefinitionObjectId", "Name", "Namespace", "Source" }, + values: new object[,] + { + { -2, null, "ingest", null, 0 }, + { -1, null, "nsfw", null, 0 } + }); + + migrationBuilder.CreateIndex( + name: "IX_Objects_Guid", + table: "Objects", + column: "Guid"); + + migrationBuilder.CreateIndex( + name: "IX_TagDefinitions_DbTagDefinitionObjectId", + table: "TagDefinitions", + column: "DbTagDefinitionObjectId"); + + migrationBuilder.CreateIndex( + name: "IX_Tags_TagDefinitionObjectId", + table: "Tags", + column: "TagDefinitionObjectId"); + + migrationBuilder.CreateIndex( + name: "IX_Tags_TargetObjectId", + table: "Tags", + column: "TargetObjectId"); + + migrationBuilder.CreateIndex( + name: "IX_UploadedFiles_MediaObjectId", + table: "UploadedFiles", + column: "MediaObjectId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Tags"); + + migrationBuilder.DropTable( + name: "UploadedFiles"); + + migrationBuilder.DropTable( + name: "TagDefinitions"); + + migrationBuilder.DropTable( + name: "Media"); + + migrationBuilder.DropTable( + name: "Objects"); + } + } +} diff --git a/Migrations/HyperBooruDbContextModelSnapshot.cs b/Migrations/HyperBooruDbContextModelSnapshot.cs index 36f6e72..7096799 100644 --- a/Migrations/HyperBooruDbContextModelSnapshot.cs +++ b/Migrations/HyperBooruDbContextModelSnapshot.cs @@ -138,6 +138,24 @@ namespace HyperBooru.Migrations b.HasIndex("DbTagDefinitionObjectId"); b.ToTable("TagDefinitions", (string)null); + + b.HasData( + new + { + ObjectId = -1, + Guid = new Guid("a03e1649-bd30-4db1-a81d-be05ba05f357"), + ObjectType = 0, + Name = "nsfw", + Source = 0 + }, + new + { + ObjectId = -2, + Guid = new Guid("3094fe00-8e41-4c08-8828-dea2ba878dd4"), + ObjectType = 0, + Name = "ingest", + Source = 0 + }); }); modelBuilder.Entity("HyperBooru.DbUploadedFile", b => diff --git a/Pages/TagDefinitions.cshtml.cs b/Pages/TagDefinitions.cshtml.cs index e2253d6..afbad87 100644 --- a/Pages/TagDefinitions.cshtml.cs +++ b/Pages/TagDefinitions.cshtml.cs @@ -5,7 +5,7 @@ namespace HyperBooru.Pages; public class TagDefinitionsModel : PageModel { public IEnumerable TagDefinitions => - db.TagDefinitions; + db.TagDefinitions.Where(td => td.Source == TagSource.UserTag); private HyperBooruDbContext db; diff --git a/Pages/ViewMedia.cshtml b/Pages/ViewMedia.cshtml index e37bbf1..e77ec22 100644 --- a/Pages/ViewMedia.cshtml +++ b/Pages/ViewMedia.cshtml @@ -115,28 +115,41 @@ }
- + +
- - @foreach(var tag in Model.Media.Tags.Select(t => t.TagDefinition)) { + @foreach(var tag in Model.UserTags) { + bool isImplicit = Model.IsImplicit(tag); - - - + + }
Source Namespace Tag Name
@tag.Source@tag.Namespace@tag.Name + @if(isImplicit) { + @tag.Namespace + } else { + @tag.Namespace + } + + @if(isImplicit) { + @tag.Name + } else { + @tag.Name + } + Delete
- + +
diff --git a/Pages/ViewMedia.cshtml.cs b/Pages/ViewMedia.cshtml.cs index 476ea40..76c515b 100644 --- a/Pages/ViewMedia.cshtml.cs +++ b/Pages/ViewMedia.cshtml.cs @@ -6,8 +6,10 @@ namespace HyperBooru.Pages; public class ViewMediaModel : PageModel { public DbMedia Media { get; private set; } + public DbTagDefinition[] UserTags { get; private set; } + public IEnumerable TagDefinitions => - db.TagDefinitions; + db.TagDefinitions.Where(td => td.Source == TagSource.UserTag); private HyperBooruDbContext db; @@ -15,12 +17,27 @@ public class ViewMediaModel : PageModel { this.db = db; public IActionResult OnGet([FromQuery(Name = "m")] Guid mediaId) { - var media = db.Media.First(m => m.Guid == mediaId); - if(media is null) + Media = db.Media.First(m => m.Guid == mediaId); + if(Media is null) return NotFound(); - Media = media; + UserTags = GetTagRecursive( + Media.Tags + .Select(t => t.TagDefinition)) + .OrderBy(td => td.Namespace) + .ThenBy(td => td.Name) + .ToArray(); return Page(); } + + public bool IsImplicit(DbTagDefinition tagDef) => + !Media.Tags + .Select(t => t.TagDefinition.Guid) + .Contains(tagDef.Guid); + + private IEnumerable GetTagRecursive(IEnumerable tagDefs) => + tagDefs + .Concat(tagDefs.SelectMany(td => GetTagRecursive(td.ImplicitTags))) + .DistinctBy(td => td.Guid); } \ No newline at end of file diff --git a/Pages/ViewMedia.cshtml.css b/Pages/ViewMedia.cshtml.css index 622de48..29094b8 100644 --- a/Pages/ViewMedia.cshtml.css +++ b/Pages/ViewMedia.cshtml.css @@ -52,6 +52,10 @@ div#metadata-fileinfo > table td { font-size: 8pt; } +div#metadata-fileinfo button#delete-button { + background: #ff4848; +} + div#metadata-tags > table td { font-size: 8pt; } diff --git a/TagController.cs b/TagController.cs index 5c9fa39..6972da2 100644 --- a/TagController.cs +++ b/TagController.cs @@ -54,6 +54,46 @@ public class TagController : Controller { return Ok(); } + [HttpPost("{tagId}/implicit/{implicitTagId}")] + public IActionResult AddImplicitTag( + [FromRoute] Guid tagId, + [FromRoute] Guid implicitTagId) { + + var tagDef = db.TagDefinitions.First(td => td.Guid == tagId); + var implicitTagDef = db.TagDefinitions.First(td => td.Guid == implicitTagId); + + if(tagDef is null || implicitTagDef is null) + return NotFound(); + + if(tagDef.ImplicitTags.Select(td => td.Guid).Contains(implicitTagId)) + return BadRequest(); + + tagDef.ImplicitTags.Add(implicitTagDef); + db.SaveChanges(); + + return Ok(); + } + + [HttpDelete("{tagId}/implicit/{implicitTagId}")] + public IActionResult RemoveImplicitTag( + [FromRoute] Guid tagId, + [FromRoute] Guid implicitTagId) { + + var tagDef = db.TagDefinitions.First(td => td.Guid == tagId); + var implicitTagDef = db.TagDefinitions.First(td => td.Guid == implicitTagId); + + if(tagDef is null || implicitTagDef is null) + return NotFound(); + + if(!tagDef.ImplicitTags.Select(td => td.Guid).Contains(implicitTagId)) + return BadRequest(); + + tagDef.ImplicitTags.Remove(implicitTagDef); + db.SaveChanges(); + + return Ok(); + } + [HttpPost("def")] public void CreateTagDefinition( [FromForm] string name, diff --git a/wwwroot/styles/global.css b/wwwroot/styles/global.css index 969c531..83d76ec 100644 --- a/wwwroot/styles/global.css +++ b/wwwroot/styles/global.css @@ -1,15 +1,19 @@ :root { - --col-accent-pri: #0aa; - --col-accent-pri-hl: #0cc; - --col-bg: #222; - --col-dialog-bg: #333; - --col-navbar-bg: var(--col-accent-pri); - --col-button-pri: var(--col-accent-pri); - --col-button-pri-hl: var(--col-accent-pri-hl); - --col-button-sec: #555; - --col-button-sec-hl: #777; - --col-scrollbar: #666666; - --col-scrollbar-hover: #aaaaaa; + --col-accent-pri: #0aa; + --col-accent-pri-hl: #0cc; + --col-bg: #222; + --col-dialog-bg: #333; + --col-navbar-bg: var(--col-accent-pri); + --col-button-pri: var(--col-accent-pri); + --col-button-pri-hl: var(--col-accent-pri-hl); + --col-button-disabled: #777; + --col-button-disabled-bg: #444; + --col-button-sec: #555; + --col-button-sec-hl: #777; + --col-button-sec-disabled: #555; + --col-button-sec-disabled-bg: #000; + --col-scrollbar: #666666; + --col-scrollbar-hover: #aaaaaa; } body { @@ -43,6 +47,11 @@ button, input[type=submit] { user-select: none; } +button:disabled { + color: var(--col-button-disabled) !important; + background: var(--col-button-disabled-bg) !important; +} + button.secondary { background: var(--col-button-sec); } @@ -56,6 +65,11 @@ button.secondary:active { color: var(--col-button-sec); } +button.secondary:disabled { + color: var(--col-button-sec-disabled) !important; + background: var(--col-button-sec-disabled-bg) !important; +} + button:hover, input[type=submit]:hover { background: var(--col-button-pri-hl); } -- cgit v1.3