-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
torrent_files.go
50 lines (42 loc) · 1.06 KB
/
torrent_files.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package model
import (
"gorm.io/gorm"
"gorm.io/gorm/clause"
"regexp"
"strings"
)
func (f *TorrentFile) BeforeCreate(tx *gorm.DB) (err error) {
tx.Statement.AddClause(clause.OnConflict{
DoNothing: true,
})
return nil
}
func (f TorrentFile) BasePath() string {
baseName := f.Path
if f.Extension.Valid {
baseName = baseName[:len(baseName)-len(f.Extension.String)-1]
}
return baseName
}
func (f TorrentFile) BaseName() string {
basePathParts := strings.Split(f.BasePath(), "/")
return basePathParts[len(basePathParts)-1]
}
var fileExtensionRegex = regexp.MustCompile(`[^/.]\.([a-z0-9]+)$`)
func FileExtensionFromPath(path string) NullString {
match := fileExtensionRegex.FindStringSubmatch(strings.ToLower(path))
if len(match) == 2 {
return NewNullString(match[1])
}
return NullString{}
}
func fileTypeFromPath(path string) NullFileType {
extension := FileExtensionFromPath(path)
if extension.Valid {
return FileTypeFromExtension(extension.String)
}
return NullFileType{}
}
func (f TorrentFile) FileType() NullFileType {
return fileTypeFromPath(f.Path)
}