Skip to content

Commit

Permalink
(#102) ContentProxy: redesign the attribute optionality
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Aug 28, 2022
1 parent b2cccee commit f6bcfff
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 33 deletions.
4 changes: 2 additions & 2 deletions Emulsion.ContentProxy/ContentStorage.fs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ type MessageContentIdentity = {
ChatUserName: string
MessageId: int64
FileId: string
FileName: string option
MimeType: string option
FileName: string
MimeType: string
}

let getOrCreateMessageRecord (context: EmulsionDbContext) (id: MessageContentIdentity): Async<TelegramContent> = async {
Expand Down
4 changes: 0 additions & 4 deletions Emulsion.Database/EmulsionDbContext.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace Emulsion.Database

open EntityFrameworkCore.FSharp.Extensions
open Microsoft.EntityFrameworkCore
open Microsoft.EntityFrameworkCore.Design

Expand All @@ -9,9 +8,6 @@ open Emulsion.Database.Entities
type EmulsionDbContext(options: DbContextOptions) =
inherit DbContext(options)

override _.OnModelCreating builder =
builder.RegisterOptionTypes()

[<DefaultValue>] val mutable private telegramContents: DbSet<TelegramContent>
member this.TelegramContents with get() = this.telegramContents and set v = this.telegramContents <- v

Expand Down
4 changes: 2 additions & 2 deletions Emulsion.Database/Entities.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ type TelegramContent = {
ChatUserName: string
MessageId: int64
FileId: string
FileName: string option
MimeType: string option
FileName: string
MimeType: string
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ open System
open Emulsion.Database
open Microsoft.EntityFrameworkCore
open Microsoft.EntityFrameworkCore.Infrastructure
open Microsoft.EntityFrameworkCore.Metadata
open Microsoft.EntityFrameworkCore.Migrations
open Microsoft.EntityFrameworkCore.Storage.ValueConversion

[<DbContext(typeof<EmulsionDbContext>)>]
[<Migration("20220828121717_FileNameAndMimeType")>]
type FileNameAndMimeType() =
[<Migration("20220828133844_ContentFileNameAndMimeType")>]
type ContentFileNameAndMimeType() =
inherit Migration()

override this.Up(migrationBuilder:MigrationBuilder) =
Expand All @@ -20,13 +18,15 @@ type FileNameAndMimeType() =
,table = "TelegramContents"
,``type`` = "TEXT"
,nullable = true
,defaultValue = "file.bin"
) |> ignore

migrationBuilder.AddColumn<string>(
name = "MimeType"
,table = "TelegramContents"
,``type`` = "TEXT"
,nullable = true
,defaultValue = "application/octet-stream"
) |> ignore


Expand Down Expand Up @@ -59,13 +59,13 @@ type FileNameAndMimeType() =
b.Property<string>("FileId")
.IsRequired(false)
.HasColumnType("TEXT") |> ignore
b.Property<string option>("FileName")
b.Property<string>("FileName")
.IsRequired(false)
.HasColumnType("TEXT") |> ignore
b.Property<Int64>("MessageId")
.IsRequired(true)
.HasColumnType("INTEGER") |> ignore
b.Property<string option>("MimeType")
b.Property<string>("MimeType")
.IsRequired(false)
.HasColumnType("TEXT") |> ignore

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ type EmulsionDbContextModelSnapshot() =
b.Property<string>("FileId")
.IsRequired(false)
.HasColumnType("TEXT") |> ignore
b.Property<string option>("FileName")
b.Property<string>("FileName")
.IsRequired(false)
.HasColumnType("TEXT") |> ignore
b.Property<Int64>("MessageId")
.IsRequired(true)
.HasColumnType("INTEGER") |> ignore
b.Property<string option>("MimeType")
b.Property<string>("MimeType")
.IsRequired(false)
.HasColumnType("TEXT") |> ignore

Expand Down
4 changes: 2 additions & 2 deletions Emulsion.Telegram/LinkGenerator.fs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ let private getContentIdentities(message: FunogramMessage): ContentStorage.Messa
ChatUserName = chatName
MessageId = message.MessageId
FileId = fileInfo.FileId
FileName = fileInfo.FileName
MimeType = fileInfo.MimeType
FileName = Option.defaultValue "file.bin" fileInfo.FileName
MimeType = Option.defaultValue "application/octet-stream" fileInfo.MimeType
}
)
| _ -> Seq.empty
Expand Down
4 changes: 2 additions & 2 deletions Emulsion.Tests/ContentProxy/ContentStorageTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ let private testIdentity = {
ChatUserName = "test"
MessageId = 123L
FileId = "this_is_file"
FileName = None
MimeType = None
FileName = "file.bin"
MimeType = "application/octet-stream"
}

let private executeQuery settings =
Expand Down
4 changes: 2 additions & 2 deletions Emulsion.Tests/Database/DatabaseStructureTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ let ``Unique constraint should hold``(): unit =
ChatUserName = "testChat"
MessageId = 666L
FileId = "foobar"
FileName = None
MimeType = None
FileName = "file.bin"
MimeType = "application/octet-stream"
}
async {
do! DataStorage.addAsync ctx.TelegramContents newContent
Expand Down
16 changes: 8 additions & 8 deletions Emulsion.Tests/Web/ContentControllerTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ type ContentControllerTests(output: ITestOutputHelper) =
ChatUserName = chatUserName
MessageId = messageId
FileId = "foobar"
FileName = None
MimeType = None
FileName = "file.bin"
MimeType = "application/octet-stream"
}

performTestWithContent None content (fun controller -> async {
Expand All @@ -106,8 +106,8 @@ type ContentControllerTests(output: ITestOutputHelper) =
ChatUserName = chatUserName
MessageId = messageId
FileId = fileId
FileName = None
MimeType = None
FileName = "file.bin"
MimeType = "application/octet-stream"
}

telegramClient.SetResponse(fileId, None)
Expand All @@ -131,8 +131,8 @@ type ContentControllerTests(output: ITestOutputHelper) =
ChatUserName = chatUserName
MessageId = messageId
FileId = fileId
FileName = None
MimeType = None
FileName = "file.bin"
MimeType = "application/octet-stream"
}

use fileCache = setUpFileCache()
Expand Down Expand Up @@ -160,8 +160,8 @@ type ContentControllerTests(output: ITestOutputHelper) =
ChatUserName = chatUserName
MessageId = messageId
FileId = fileId
FileName = None
MimeType = None
FileName = "file.bin"
MimeType = "application/octet-stream"
}

let onServerFileId = "fileIdOnServer"
Expand Down
4 changes: 1 addition & 3 deletions Emulsion.Web/ContentController.fs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,5 @@ type ContentController(logger: ILogger<ContentController>,
logger.LogWarning $"Link \"{fileInfo}\" could not be downloaded."
return this.NotFound() :> IActionResult
| Some stream ->
let contentType = Option.defaultValue "application/octet-stream" content.MimeType
let fileName = Option.defaultValue "" content.FileName
return FileStreamResult(stream, contentType, FileDownloadName = fileName)
return FileStreamResult(stream, content.MimeType, FileDownloadName = content.FileName)
}

0 comments on commit f6bcfff

Please sign in to comment.