2021-02-23 16:01:29 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2021-04-16 19:12:56 +00:00
|
|
|
"encoding/json"
|
2021-05-22 19:33:32 +00:00
|
|
|
"fmt"
|
2021-02-23 16:01:29 +00:00
|
|
|
"openmediacenter/apiGo/api/types"
|
|
|
|
"openmediacenter/apiGo/database"
|
2021-04-16 19:12:56 +00:00
|
|
|
"openmediacenter/apiGo/database/settings"
|
2021-02-23 16:01:29 +00:00
|
|
|
"openmediacenter/apiGo/videoparser"
|
2021-04-16 19:12:56 +00:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2021-02-23 16:01:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func AddSettingsHandlers() {
|
|
|
|
saveSettingsToDB()
|
|
|
|
getSettingsFromDB()
|
|
|
|
reIndexHandling()
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSettingsFromDB() {
|
2021-05-22 19:33:32 +00:00
|
|
|
AddHandler("loadGeneralSettings", SettingsNode, func(info *HandlerInfo) []byte {
|
2021-02-23 16:01:29 +00:00
|
|
|
result := database.GetSettings()
|
|
|
|
return jsonify(result)
|
|
|
|
})
|
2021-05-22 19:33:32 +00:00
|
|
|
|
|
|
|
AddHandler("loadInitialData", SettingsNode, func(info *HandlerInfo) []byte {
|
2021-04-16 19:12:56 +00:00
|
|
|
sett := settings.LoadSettings()
|
|
|
|
|
|
|
|
type InitialDataTypeResponse struct {
|
|
|
|
DarkMode bool
|
|
|
|
Pasword bool
|
|
|
|
MediacenterName string
|
|
|
|
VideoPath string
|
2021-04-16 20:44:56 +00:00
|
|
|
TVShowPath string
|
2021-04-16 19:12:56 +00:00
|
|
|
}
|
|
|
|
|
2021-04-16 20:44:56 +00:00
|
|
|
regexMatchUrl := regexp.MustCompile("^http(|s)://([0-9]){1,3}\\.([0-9]){1,3}\\.([0-9]){1,3}\\.([0-9]){1,3}:[0-9]{1,5}")
|
2021-04-16 19:12:56 +00:00
|
|
|
videoUrl := regexMatchUrl.FindString(sett.VideoPath)
|
2021-04-16 20:44:56 +00:00
|
|
|
tvshowurl := regexMatchUrl.FindString(sett.TVShowPath)
|
2021-04-16 19:12:56 +00:00
|
|
|
serverVideoPath := strings.TrimPrefix(sett.VideoPath, videoUrl)
|
2021-04-16 20:44:56 +00:00
|
|
|
serverTVShowPath := strings.TrimPrefix(sett.TVShowPath, tvshowurl)
|
2021-04-16 19:12:56 +00:00
|
|
|
|
|
|
|
res := InitialDataTypeResponse{
|
|
|
|
DarkMode: sett.DarkMode,
|
|
|
|
Pasword: sett.Pasword != "-1",
|
2021-04-16 20:44:56 +00:00
|
|
|
MediacenterName: sett.MediacenterName,
|
2021-04-16 19:12:56 +00:00
|
|
|
VideoPath: serverVideoPath,
|
2021-04-16 20:44:56 +00:00
|
|
|
TVShowPath: serverTVShowPath,
|
2021-04-16 19:12:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
str, _ := json.Marshal(res)
|
|
|
|
return str
|
|
|
|
})
|
2021-02-23 16:01:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func saveSettingsToDB() {
|
2021-05-22 19:33:32 +00:00
|
|
|
AddHandler("saveGeneralSettings", SettingsNode, func(info *HandlerInfo) []byte {
|
|
|
|
var args struct {
|
|
|
|
Settings types.SettingsType
|
|
|
|
}
|
|
|
|
if err := FillStruct(&args, info.Data); err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-23 16:01:29 +00:00
|
|
|
query := `
|
|
|
|
UPDATE settings SET
|
|
|
|
video_path=?,
|
|
|
|
episode_path=?,
|
|
|
|
password=?,
|
|
|
|
mediacenter_name=?,
|
|
|
|
TMDB_grabbing=?,
|
|
|
|
DarkMode=?
|
|
|
|
WHERE 1`
|
|
|
|
return database.SuccessQuery(query,
|
2021-05-22 19:33:32 +00:00
|
|
|
args.Settings.VideoPath, args.Settings.EpisodePath, args.Settings.Password,
|
|
|
|
args.Settings.MediacenterName, args.Settings.TMDBGrabbing, args.Settings.DarkMode)
|
2021-02-23 16:01:29 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// methods for handling reindexing and cleanup of db gravity
|
|
|
|
func reIndexHandling() {
|
2021-05-22 19:33:32 +00:00
|
|
|
AddHandler("startReindex", SettingsNode, func(info *HandlerInfo) []byte {
|
2021-02-23 16:01:29 +00:00
|
|
|
videoparser.StartReindex()
|
|
|
|
return database.ManualSuccessResponse(nil)
|
|
|
|
})
|
|
|
|
|
2021-05-22 19:33:32 +00:00
|
|
|
AddHandler("startTVShowReindex", SettingsNode, func(info *HandlerInfo) []byte {
|
2021-04-16 20:44:56 +00:00
|
|
|
videoparser.StartTVShowReindex()
|
|
|
|
return database.ManualSuccessResponse(nil)
|
|
|
|
})
|
|
|
|
|
2021-05-22 19:33:32 +00:00
|
|
|
AddHandler("cleanupGravity", SettingsNode, func(info *HandlerInfo) []byte {
|
2021-02-23 16:01:29 +00:00
|
|
|
videoparser.StartCleanup()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|