From 64f162a2f1c163bcd40119ea6025ff229c3613d4 Mon Sep 17 00:00:00 2001 From: rraymondgh Date: Thu, 24 Apr 2025 16:15:33 +0000 Subject: [PATCH 1/2] correct file extension derivation --- internal/model/file_extension_test.go | 48 ++++++++++++++++++++++++++ internal/model/torrent_files.go | 2 +- migrations/00021_extension_default.sql | 29 ++++++++++++++++ 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 internal/model/file_extension_test.go create mode 100644 migrations/00021_extension_default.sql diff --git a/internal/model/file_extension_test.go b/internal/model/file_extension_test.go new file mode 100644 index 00000000..d2029d2a --- /dev/null +++ b/internal/model/file_extension_test.go @@ -0,0 +1,48 @@ +package model + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestFileExtensionFromPath(t *testing.T) { + t.Parallel() + + type parseTest struct { + inputString string + expectedExt string + } + + var parseTests = []parseTest{ + { + inputString: "short film.mkv", + expectedExt: "mkv", + }, + { + inputString: "short film.mkv.mk2", + expectedExt: "mk2", + }, + { + inputString: "short film....mkv", + expectedExt: "mkv", + }, + { + inputString: ".mkv", + expectedExt: "", + }, + { + inputString: "short film.", + expectedExt: "", + }, + } + + for _, test := range parseTests { + t.Run(test.inputString, func(t *testing.T) { + t.Parallel() + + actualOutput := FileExtensionFromPath(test.inputString) + assert.Equal(t, test.expectedExt, actualOutput.String) + }) + } +} diff --git a/internal/model/torrent_files.go b/internal/model/torrent_files.go index 585a406d..3057c5df 100644 --- a/internal/model/torrent_files.go +++ b/internal/model/torrent_files.go @@ -30,7 +30,7 @@ func (f TorrentFile) BaseName() string { return basePathParts[len(basePathParts)-1] } -var fileExtensionRegex = regexp.MustCompile(`[^/.]\.([a-z0-9]+)$`) +var fileExtensionRegex = regexp.MustCompile(`.\.([a-z0-9]+)$`) func FileExtensionFromPath(path string) NullString { match := fileExtensionRegex.FindStringSubmatch(strings.ToLower(path)) diff --git a/migrations/00021_extension_default.sql b/migrations/00021_extension_default.sql new file mode 100644 index 00000000..4053a822 --- /dev/null +++ b/migrations/00021_extension_default.sql @@ -0,0 +1,29 @@ +-- +goose Up +-- +goose StatementBegin + +alter table "torrents" drop column "extension"; +alter table "torrents" add column "extension" text generated always as (case + when files_status = 'single'::"FilesStatus" + then substring(lower(name) from '.\.([a-z0-9]+)$') end) stored; +create index on torrents (extension); +alter table "torrent_files" drop column "extension"; +alter table "torrent_files" add column "extension" text generated always as + (substring(lower(path) from '.\.([a-z0-9]+)$')) stored; +create index on torrent_files (extension); + +-- +goose StatementEnd + +-- +goose Down +-- +goose StatementBegin + +alter table "torrents" drop column "extension"; +alter table "torrents" add column "extension" text generated always as (case + when files_status = 'single'::"FilesStatus" + then substring(lower(name) from '[^/.]\.([a-z0-9]+)$') end) stored; +create index on torrents (extension); +alter table "torrent_files" drop column "extension"; +alter table "torrent_files" add column "extension" text generated always as + (substring(lower(path) from '[^/.]\.([a-z0-9]+)$')) stored; +create index on torrent_files (extension); + +-- +goose StatementEnd From c03c334e804815be2833d2c88d09ba3c9ac1361a Mon Sep 17 00:00:00 2001 From: rraymondgh Date: Tue, 13 May 2025 20:00:27 +0000 Subject: [PATCH 2/2] fix linting --- internal/model/file_extension_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/model/file_extension_test.go b/internal/model/file_extension_test.go index d2029d2a..d796ec24 100644 --- a/internal/model/file_extension_test.go +++ b/internal/model/file_extension_test.go @@ -14,7 +14,7 @@ func TestFileExtensionFromPath(t *testing.T) { expectedExt string } - var parseTests = []parseTest{ + parseTests := []parseTest{ { inputString: "short film.mkv", expectedExt: "mkv",