Skip to content

bug fix : correct file extension derivation #416

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions internal/model/file_extension_test.go
Original file line number Diff line number Diff line change
@@ -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
}

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)
})
}
}
2 changes: 1 addition & 1 deletion internal/model/torrent_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
29 changes: 29 additions & 0 deletions migrations/00021_extension_default.sql
Original file line number Diff line number Diff line change
@@ -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