summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--HBContext.cs9
-rw-r--r--Media.cs17
-rw-r--r--Migrations/20230905163953_CurrentUploadedFile.Designer.cs327
-rw-r--r--Migrations/20230905163953_CurrentUploadedFile.cs75
-rw-r--r--Migrations/HBContextModelSnapshot.cs14
-rw-r--r--Services/MediaService.cs11
6 files changed, 438 insertions, 15 deletions
diff --git a/HBContext.cs b/HBContext.cs
index 15dad6d..415b745 100644
--- a/HBContext.cs
+++ b/HBContext.cs
@@ -55,10 +55,15 @@ public class HBContext : DbContext {
}
});
- // Implicit tags need some special attention to make many<->many
- // navigations work for the same object type.
+ // Some complex relationships cannot be inferred and require
+ // additional configuration, as seen below.
modelBuilder.Entity<TagDefinition>()
.HasMany(e => e.ImplicitTags)
.WithMany();
+
+ modelBuilder.Entity<Media>()
+ .HasOne(m => m.CurrentUploadedFile)
+ .WithOne()
+ .HasForeignKey<Media>("CurrentUploadedFileId");
}
} \ No newline at end of file
diff --git a/Media.cs b/Media.cs
index db38e9f..ff5a648 100644
--- a/Media.cs
+++ b/Media.cs
@@ -7,14 +7,15 @@ using System.Net.NetworkInformation;
namespace HyperBooru;
public class Media : HBObject {
- public string Checksum { get; set; }
- public string MimeType { get; set; }
- public string? ShortDescription { get; set; }
- public string? LongDescription { get; set; }
- public int Width { get; set; }
- public int Height { get; set; }
- public virtual OcrData? OcrData { get; set; }
- public virtual List<UploadedFile> UploadedFiles { get; set; } = new();
+ public string Checksum { get; set; }
+ public string MimeType { get; set; }
+ public string? ShortDescription { get; set; }
+ public string? LongDescription { get; set; }
+ public int Width { get; set; }
+ public int Height { get; set; }
+ public virtual OcrData? OcrData { get; set; }
+ public virtual UploadedFile CurrentUploadedFile { get; set; }
+ public virtual List<UploadedFile> UploadedFiles { get; set; } = new();
public bool IsIngest => Tags
.Select(t => t.TagDefinitionId)
diff --git a/Migrations/20230905163953_CurrentUploadedFile.Designer.cs b/Migrations/20230905163953_CurrentUploadedFile.Designer.cs
new file mode 100644
index 0000000..8e52caf
--- /dev/null
+++ b/Migrations/20230905163953_CurrentUploadedFile.Designer.cs
@@ -0,0 +1,327 @@
+// <auto-generated />
+using System;
+using HyperBooru;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Infrastructure;
+using Microsoft.EntityFrameworkCore.Migrations;
+using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
+using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
+
+#nullable disable
+
+namespace HyperBooru.Migrations
+{
+ [DbContext(typeof(HBContext))]
+ [Migration("20230905163953_CurrentUploadedFile")]
+ partial class CurrentUploadedFile
+ {
+ /// <inheritdoc />
+ protected override void BuildTargetModel(ModelBuilder modelBuilder)
+ {
+#pragma warning disable 612, 618
+ modelBuilder
+ .HasAnnotation("ProductVersion", "7.0.10")
+ .HasAnnotation("Relational:MaxIdentifierLength", 63);
+
+ NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
+
+ modelBuilder.Entity("HyperBooru.HBObject", b =>
+ {
+ b.Property<int>("ObjectId")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("integer");
+
+ NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("ObjectId"));
+
+ b.Property<Guid>("Guid")
+ .HasColumnType("uuid");
+
+ b.HasKey("ObjectId");
+
+ b.HasIndex("Guid");
+
+ b.ToTable("Objects", (string)null);
+
+ b.UseTptMappingStrategy();
+ });
+
+ modelBuilder.Entity("HyperBooru.OcrData", b =>
+ {
+ b.Property<int>("OcrDataId")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("integer");
+
+ NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("OcrDataId"));
+
+ b.Property<int>("MediaId")
+ .HasColumnType("integer");
+
+ b.Property<string>("SearchableText")
+ .IsRequired()
+ .HasColumnType("text");
+
+ b.Property<string>("Text")
+ .IsRequired()
+ .HasColumnType("text");
+
+ b.Property<DateTime>("Timestamp")
+ .HasColumnType("timestamp with time zone");
+
+ b.HasKey("OcrDataId");
+
+ b.HasIndex("MediaId")
+ .IsUnique();
+
+ b.ToTable("OcrData");
+ });
+
+ modelBuilder.Entity("TagDefinitionTagDefinition", b =>
+ {
+ b.Property<int>("ImplicitTagsObjectId")
+ .HasColumnType("integer");
+
+ b.Property<int>("TagDefinitionObjectId")
+ .HasColumnType("integer");
+
+ b.HasKey("ImplicitTagsObjectId", "TagDefinitionObjectId");
+
+ b.HasIndex("TagDefinitionObjectId");
+
+ b.ToTable("TagDefinitionTagDefinition");
+ });
+
+ modelBuilder.Entity("HyperBooru.Media", b =>
+ {
+ b.HasBaseType("HyperBooru.HBObject");
+
+ b.Property<string>("Checksum")
+ .IsRequired()
+ .HasColumnType("text");
+
+ b.Property<int>("CurrentUploadedFileId")
+ .HasColumnType("integer");
+
+ b.Property<int>("Height")
+ .HasColumnType("integer");
+
+ b.Property<string>("LongDescription")
+ .HasColumnType("text");
+
+ b.Property<string>("MimeType")
+ .IsRequired()
+ .HasColumnType("text");
+
+ b.Property<string>("ShortDescription")
+ .HasColumnType("text");
+
+ b.Property<int>("Width")
+ .HasColumnType("integer");
+
+ b.HasIndex("CurrentUploadedFileId")
+ .IsUnique();
+
+ b.ToTable("Media", (string)null);
+ });
+
+ modelBuilder.Entity("HyperBooru.Tag", b =>
+ {
+ b.HasBaseType("HyperBooru.HBObject");
+
+ b.Property<DateTime>("CreateTime")
+ .HasColumnType("timestamp with time zone");
+
+ b.Property<int>("TagDefinitionId")
+ .HasColumnType("integer");
+
+ b.Property<int>("TargetObjectId")
+ .HasColumnType("integer");
+
+ b.HasIndex("TagDefinitionId");
+
+ b.HasIndex("TargetObjectId");
+
+ b.ToTable("Tags", (string)null);
+ });
+
+ modelBuilder.Entity("HyperBooru.TagDefinition", b =>
+ {
+ b.HasBaseType("HyperBooru.HBObject");
+
+ b.Property<string>("Alias")
+ .HasColumnType("text");
+
+ b.Property<string>("Name")
+ .IsRequired()
+ .HasColumnType("text");
+
+ b.Property<string>("Namespace")
+ .HasColumnType("text");
+
+ b.Property<int>("Source")
+ .HasColumnType("integer");
+
+ b.ToTable("TagDefinitions", (string)null);
+
+ b.HasData(
+ new
+ {
+ ObjectId = -1,
+ Guid = new Guid("ebdad4f8-455a-4351-8017-1d4854d6fa38"),
+ Name = "nsfw",
+ Source = 0
+ },
+ new
+ {
+ ObjectId = -2,
+ Guid = new Guid("ea212801-5bcc-4c0e-814f-fb9d30db58bc"),
+ Name = "ingest",
+ Source = 0
+ });
+ });
+
+ modelBuilder.Entity("HyperBooru.UploadedFile", b =>
+ {
+ b.HasBaseType("HyperBooru.HBObject");
+
+ b.Property<bool>("ChecksumVerified")
+ .HasColumnType("boolean");
+
+ b.Property<DateTime?>("CreateTime")
+ .HasColumnType("timestamp with time zone");
+
+ b.Property<string>("Filename")
+ .HasColumnType("text");
+
+ b.Property<DateTime?>("LastAccessTime")
+ .HasColumnType("timestamp with time zone");
+
+ b.Property<DateTime?>("LastWriteTime")
+ .HasColumnType("timestamp with time zone");
+
+ b.Property<long>("Length")
+ .HasColumnType("bigint");
+
+ b.Property<int>("MediaObjectId")
+ .HasColumnType("integer");
+
+ b.Property<string>("OriginalChecksum")
+ .IsRequired()
+ .HasColumnType("text");
+
+ b.Property<DateTime>("UploadTime")
+ .HasColumnType("timestamp with time zone");
+
+ b.HasIndex("MediaObjectId");
+
+ b.ToTable("UploadedFiles", (string)null);
+ });
+
+ modelBuilder.Entity("HyperBooru.OcrData", b =>
+ {
+ b.HasOne("HyperBooru.Media", "Media")
+ .WithOne("OcrData")
+ .HasForeignKey("HyperBooru.OcrData", "MediaId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.Navigation("Media");
+ });
+
+ modelBuilder.Entity("TagDefinitionTagDefinition", b =>
+ {
+ b.HasOne("HyperBooru.TagDefinition", null)
+ .WithMany()
+ .HasForeignKey("ImplicitTagsObjectId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.HasOne("HyperBooru.TagDefinition", null)
+ .WithMany()
+ .HasForeignKey("TagDefinitionObjectId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+ });
+
+ modelBuilder.Entity("HyperBooru.Media", b =>
+ {
+ b.HasOne("HyperBooru.UploadedFile", "CurrentUploadedFile")
+ .WithOne()
+ .HasForeignKey("HyperBooru.Media", "CurrentUploadedFileId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.HasOne("HyperBooru.HBObject", null)
+ .WithOne()
+ .HasForeignKey("HyperBooru.Media", "ObjectId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.Navigation("CurrentUploadedFile");
+ });
+
+ modelBuilder.Entity("HyperBooru.Tag", b =>
+ {
+ b.HasOne("HyperBooru.HBObject", null)
+ .WithOne()
+ .HasForeignKey("HyperBooru.Tag", "ObjectId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.HasOne("HyperBooru.TagDefinition", "TagDefinition")
+ .WithMany()
+ .HasForeignKey("TagDefinitionId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.HasOne("HyperBooru.HBObject", "Target")
+ .WithMany("Tags")
+ .HasForeignKey("TargetObjectId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.Navigation("TagDefinition");
+
+ b.Navigation("Target");
+ });
+
+ modelBuilder.Entity("HyperBooru.TagDefinition", b =>
+ {
+ b.HasOne("HyperBooru.HBObject", null)
+ .WithOne()
+ .HasForeignKey("HyperBooru.TagDefinition", "ObjectId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+ });
+
+ modelBuilder.Entity("HyperBooru.UploadedFile", b =>
+ {
+ b.HasOne("HyperBooru.Media", "Media")
+ .WithMany("UploadedFiles")
+ .HasForeignKey("MediaObjectId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.HasOne("HyperBooru.HBObject", null)
+ .WithOne()
+ .HasForeignKey("HyperBooru.UploadedFile", "ObjectId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.Navigation("Media");
+ });
+
+ modelBuilder.Entity("HyperBooru.HBObject", b =>
+ {
+ b.Navigation("Tags");
+ });
+
+ modelBuilder.Entity("HyperBooru.Media", b =>
+ {
+ b.Navigation("OcrData");
+
+ b.Navigation("UploadedFiles");
+ });
+#pragma warning restore 612, 618
+ }
+ }
+}
diff --git a/Migrations/20230905163953_CurrentUploadedFile.cs b/Migrations/20230905163953_CurrentUploadedFile.cs
new file mode 100644
index 0000000..265259e
--- /dev/null
+++ b/Migrations/20230905163953_CurrentUploadedFile.cs
@@ -0,0 +1,75 @@
+using Microsoft.EntityFrameworkCore.Migrations;
+
+#nullable disable
+
+namespace HyperBooru.Migrations
+{
+ /// <inheritdoc />
+ public partial class CurrentUploadedFile : Migration
+ {
+ /// <inheritdoc />
+ protected override void Up(MigrationBuilder migrationBuilder)
+ {
+ migrationBuilder.AddColumn<int>(
+ name: "CurrentUploadedFileId",
+ table: "Media",
+ type: "integer",
+ nullable: true);
+
+ migrationBuilder.Sql(@"
+ UPDATE
+ ""Media""
+ SET
+ ""CurrentUploadedFileId"" = uf.""ObjectId""
+ FROM
+ (SELECT
+ ""ObjectId"",
+ ""MediaObjectId""
+ FROM
+ ""UploadedFiles""
+ GROUP BY
+ ""ObjectId"",
+ ""MediaObjectId""
+ ORDER BY
+ ""ObjectId"") AS uf
+ WHERE
+ ""Media"".""ObjectId"" = uf.""MediaObjectId"";");
+
+ migrationBuilder.AlterColumn<int>(
+ name: "CurrentUploadedFileId",
+ table: "Media",
+ type: "integer",
+ nullable: false);
+
+ migrationBuilder.CreateIndex(
+ name: "IX_Media_CurrentUploadedFileId",
+ table: "Media",
+ column: "CurrentUploadedFileId",
+ unique: true);
+
+ migrationBuilder.AddForeignKey(
+ name: "FK_Media_UploadedFiles_CurrentUploadedFileId",
+ table: "Media",
+ column: "CurrentUploadedFileId",
+ principalTable: "UploadedFiles",
+ principalColumn: "ObjectId",
+ onDelete: ReferentialAction.Cascade);
+ }
+
+ /// <inheritdoc />
+ protected override void Down(MigrationBuilder migrationBuilder)
+ {
+ migrationBuilder.DropForeignKey(
+ name: "FK_Media_UploadedFiles_CurrentUploadedFileId",
+ table: "Media");
+
+ migrationBuilder.DropIndex(
+ name: "IX_Media_CurrentUploadedFileId",
+ table: "Media");
+
+ migrationBuilder.DropColumn(
+ name: "CurrentUploadedFileId",
+ table: "Media");
+ }
+ }
+}
diff --git a/Migrations/HBContextModelSnapshot.cs b/Migrations/HBContextModelSnapshot.cs
index 3211e1f..81ee485 100644
--- a/Migrations/HBContextModelSnapshot.cs
+++ b/Migrations/HBContextModelSnapshot.cs
@@ -95,6 +95,9 @@ namespace HyperBooru.Migrations
.IsRequired()
.HasColumnType("text");
+ b.Property<int>("CurrentUploadedFileId")
+ .HasColumnType("integer");
+
b.Property<int>("Height")
.HasColumnType("integer");
@@ -111,6 +114,9 @@ namespace HyperBooru.Migrations
b.Property<int>("Width")
.HasColumnType("integer");
+ b.HasIndex("CurrentUploadedFileId")
+ .IsUnique();
+
b.ToTable("Media", (string)null);
});
@@ -235,11 +241,19 @@ namespace HyperBooru.Migrations
modelBuilder.Entity("HyperBooru.Media", b =>
{
+ b.HasOne("HyperBooru.UploadedFile", "CurrentUploadedFile")
+ .WithOne()
+ .HasForeignKey("HyperBooru.Media", "CurrentUploadedFileId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
b.HasOne("HyperBooru.HBObject", null)
.WithOne()
.HasForeignKey("HyperBooru.Media", "ObjectId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
+
+ b.Navigation("CurrentUploadedFile");
});
modelBuilder.Entity("HyperBooru.Tag", b =>
diff --git a/Services/MediaService.cs b/Services/MediaService.cs
index 015eae9..cfd15f8 100644
--- a/Services/MediaService.cs
+++ b/Services/MediaService.cs
@@ -147,11 +147,12 @@ public class MediaService : IMediaService {
.First(td => td.Guid == HBContext.IngestTag);
media = new() {
- Checksum = hash,
- MimeType = mime,
- Width = magickImage.Width,
- Height = magickImage.Height,
- UploadedFiles = new() {
+ Checksum = hash,
+ MimeType = mime,
+ Width = magickImage.Width,
+ Height = magickImage.Height,
+ CurrentUploadedFile = fileRecord,
+ UploadedFiles = new() {
fileRecord
},
Tags = new() {