From 61ea42ef0118babac68681f6462d6cef6a0d4de2 Mon Sep 17 00:00:00 2001 From: lukas Date: Mon, 27 Sep 2021 17:33:05 +0200 Subject: [PATCH 1/2] allow more videotypes than mp4 --- apiGo/api/FileUpload.go | 5 ++--- apiGo/videoparser/ReIndexTVShows.go | 1 + apiGo/videoparser/VideoParser.go | 18 ++++++++++++++++-- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/apiGo/api/FileUpload.go b/apiGo/api/FileUpload.go index 3f5c148..70ca94b 100644 --- a/apiGo/api/FileUpload.go +++ b/apiGo/api/FileUpload.go @@ -7,7 +7,6 @@ import ( "openmediacenter/apiGo/database" "openmediacenter/apiGo/videoparser" "os" - "path/filepath" ) func addUploadHandler() { @@ -31,11 +30,11 @@ func addUploadHandler() { break } - // todo allow more video formats than mp4 // only allow valid extensions - if filepath.Ext(part.FileName()) != ".mp4" { + if !videoparser.ValidVideoSuffix(part.FileName()) { continue } + vidpath := PathPrefix + mSettings.VideoPath + part.FileName() dst, err := os.OpenFile(vidpath, os.O_WRONLY|os.O_CREATE, 0644) if err != nil { diff --git a/apiGo/videoparser/ReIndexTVShows.go b/apiGo/videoparser/ReIndexTVShows.go index f496add..d138c17 100644 --- a/apiGo/videoparser/ReIndexTVShows.go +++ b/apiGo/videoparser/ReIndexTVShows.go @@ -38,6 +38,7 @@ func insertEpisodesIfNotExisting(show Show) { fmt.Println(err.Error()) } + // todo no fixed mp4 extension here! dbepisodes = append(dbepisodes, fmt.Sprintf("%s S%02dE%02d.mp4", epname, season, episode)) } diff --git a/apiGo/videoparser/VideoParser.go b/apiGo/videoparser/VideoParser.go index d64a01e..3c3e67d 100644 --- a/apiGo/videoparser/VideoParser.go +++ b/apiGo/videoparser/VideoParser.go @@ -15,6 +15,20 @@ type StatusMessage struct { ContentAvailable bool } +func getVideoTypes() []string { + return []string{".mp4", ".mov", ".mkv", ".flv", ".avi", ".mpeg", ".m4v"} +} + +func ValidVideoSuffix(filename string) bool { + validExts := getVideoTypes() + for _, validExt := range validExts { + if strings.HasSuffix(filename, validExt) { + return true + } + } + return false +} + func StartReindex() bool { fmt.Println("starting reindex..") SendEvent("start") @@ -40,7 +54,7 @@ func StartReindex() bool { return err } - if !info.IsDir() && strings.HasSuffix(info.Name(), ".mp4") { + if !info.IsDir() && ValidVideoSuffix(info.Name()) { files = append(files, info.Name()) } return nil @@ -107,7 +121,7 @@ func StartTVShowReindex() { } for _, epfile := range episodefiles { - if strings.HasSuffix(epfile.Name(), ".mp4") { + if ValidVideoSuffix(epfile.Name()) { elem.files = append(elem.files, epfile.Name()) } } From dfcb7f71d9d2c9b61bdac6425d314399b9897a87 Mon Sep 17 00:00:00 2001 From: lukas Date: Thu, 30 Sep 2021 10:49:33 +0200 Subject: [PATCH 2/2] use correct filepath when checking if tv show episode already exits in databse --- apiGo/api/api/Helpers.go | 43 ----------------------------- apiGo/videoparser/ReIndexTVShows.go | 20 ++++++++------ 2 files changed, 12 insertions(+), 51 deletions(-) diff --git a/apiGo/api/api/Helpers.go b/apiGo/api/api/Helpers.go index 0b4f338..09f7e44 100644 --- a/apiGo/api/api/Helpers.go +++ b/apiGo/api/api/Helpers.go @@ -5,7 +5,6 @@ import ( "encoding/json" "fmt" "net/http" - "reflect" ) func Jsonify(v interface{}) []byte { @@ -30,45 +29,3 @@ func DecodeRequest(request *http.Request, arg interface{}) error { return err } - -// setField set a specific field of an object with an object provided -func setField(obj interface{}, name string, value interface{}) error { - structValue := reflect.ValueOf(obj).Elem() - structFieldValue := structValue.FieldByName(name) - - if !structFieldValue.IsValid() { - return fmt.Errorf("no such field: %s in obj", name) - } - - if !structFieldValue.CanSet() { - return fmt.Errorf("cannot set %s field value", name) - } - - structFieldType := structFieldValue.Type() - val := reflect.ValueOf(value) - - if structFieldType != val.Type() { - if val.Type().ConvertibleTo(structFieldType) { - // if type is convertible - convert and set - structFieldValue.Set(val.Convert(structFieldType)) - } else { - return fmt.Errorf("provided value %s type didn't match obj field type and isn't convertible", name) - } - } else { - // set value if type is the same - structFieldValue.Set(val) - } - - return nil -} - -// FillStruct fill a custom struct with objects of a map -func FillStruct(i interface{}, m map[string]interface{}) error { - for k, v := range m { - err := setField(i, k, v) - if err != nil { - return err - } - } - return nil -} diff --git a/apiGo/videoparser/ReIndexTVShows.go b/apiGo/videoparser/ReIndexTVShows.go index d138c17..32a996e 100644 --- a/apiGo/videoparser/ReIndexTVShows.go +++ b/apiGo/videoparser/ReIndexTVShows.go @@ -25,21 +25,18 @@ func startTVShowReindex(files []Show) { } func insertEpisodesIfNotExisting(show Show) { - query := "SELECT tvshow_episodes.name, season, episode FROM tvshow_episodes JOIN tvshow t on t.id = tvshow_episodes.tvshow_id WHERE t.name=?" + query := "SELECT filename FROM tvshow_episodes JOIN tvshow t on t.id = tvshow_episodes.tvshow_id WHERE t.name=?" rows := database.Query(query, show.Name) var dbepisodes []string for rows.Next() { - var epname string - var season int - var episode int - err := rows.Scan(&epname, &season, &episode) + var filename string + err := rows.Scan(&filename) if err != nil { fmt.Println(err.Error()) } - // todo no fixed mp4 extension here! - dbepisodes = append(dbepisodes, fmt.Sprintf("%s S%02dE%02d.mp4", epname, season, episode)) + dbepisodes = append(dbepisodes, filename) } // get those episodes that are missing in db @@ -84,6 +81,10 @@ VALUES (?, ?, ?, (SELECT tvshow.id FROM tvshow WHERE tvshow.name=?), ?, ?)` // difference returns the elements in `a` that aren't in `b`. func difference(a, b []string) []string { + if b == nil || len(b) == 0 { + return a + } + mb := make(map[string]struct{}, len(b)) for _, x := range b { mb[x] = struct{}{} @@ -130,7 +131,10 @@ func getAllTVShows() *[]string { var res []string for rows.Next() { var show string - rows.Scan(&show) + err := rows.Scan(&show) + if err != nil { + continue + } res = append(res, show) }