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-08-20 19:20:33 +00:00
|
|
|
gws "github.com/gowebsecure/goWebSecure-go"
|
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-23 12:21:44 +00:00
|
|
|
/**
|
|
|
|
* @api {post} /api/settings [loadGeneralSettings]
|
|
|
|
* @apiDescription Get the settings object
|
|
|
|
* @apiName loadGeneralSettings
|
|
|
|
* @apiGroup Settings
|
|
|
|
*
|
|
|
|
* @apiSuccess {Object} Settings Settings object
|
|
|
|
* @apiSuccess {string} Settings.VideoPath webserver path to the videos
|
|
|
|
* @apiSuccess {string} Settings.EpisodePath webserver path to the tvshows
|
|
|
|
* @apiSuccess {string} Settings.MediacenterName overall name of the mediacenter
|
|
|
|
* @apiSuccess {string} Settings.Password new server password (-1 if no password set)
|
|
|
|
* @apiSuccess {bool} Settings.TMDBGrabbing TMDB grabbing support to grab tag info and thumbnails
|
|
|
|
* @apiSuccess {bool} Settings.DarkMode Darkmode enabled?
|
2021-07-11 12:26:10 +00:00
|
|
|
* @apiSuccess {Object} Sizes Sizes object
|
|
|
|
* @apiSuccess {uint32} Sizes.VideoNr total number of videos
|
|
|
|
* @apiSuccess {float32} Sizes.DBSize total size of database
|
|
|
|
* @apiSuccess {uint32} Sizes.DifferentTags number of different tags available
|
|
|
|
* @apiSuccess {uint32} Sizes.TagsAdded number of different tags added to videos
|
2021-05-23 12:21:44 +00:00
|
|
|
*/
|
2021-08-20 19:20:33 +00:00
|
|
|
gws.AddHandler("loadGeneralSettings", SettingsNode, func(info *gws.HandlerInfo) []byte {
|
2021-07-11 12:26:10 +00:00
|
|
|
result, _, sizes := database.GetSettings()
|
|
|
|
|
|
|
|
var ret = struct {
|
|
|
|
Settings *types.SettingsType
|
|
|
|
Sizes *types.SettingsSizeType
|
|
|
|
}{
|
|
|
|
Settings: &result,
|
|
|
|
Sizes: &sizes,
|
|
|
|
}
|
|
|
|
return jsonify(ret)
|
2021-02-23 16:01:29 +00:00
|
|
|
})
|
2021-05-22 19:33:32 +00:00
|
|
|
|
2021-05-23 12:21:44 +00:00
|
|
|
/**
|
|
|
|
* @api {post} /api/settings [loadInitialData]
|
|
|
|
* @apiDescription load startdata to display on homepage
|
|
|
|
* @apiName loadInitialData
|
|
|
|
* @apiGroup Settings
|
|
|
|
*
|
|
|
|
* @apiSuccess {string} VideoPath webserver path to the videos
|
|
|
|
* @apiSuccess {string} EpisodePath webserver path to the tvshows
|
|
|
|
* @apiSuccess {string} MediacenterName overall name of the mediacenter
|
|
|
|
* @apiSuccess {string} Pasword new server password (-1 if no password set)
|
|
|
|
* @apiSuccess {bool} DarkMode Darkmode enabled?
|
2021-06-08 19:55:54 +00:00
|
|
|
* @apiSuccess {bool} TVShowEnabled is are TVShows enabled
|
2021-05-23 12:21:44 +00:00
|
|
|
*/
|
2021-08-20 19:20:33 +00:00
|
|
|
gws.AddHandler("loadInitialData", SettingsNode, func(info *gws.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-06-08 19:55:54 +00:00
|
|
|
TVShowEnabled bool
|
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-06-08 19:55:54 +00:00
|
|
|
TVShowEnabled: settings.TVShowsEnabled(),
|
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-23 12:21:44 +00:00
|
|
|
/**
|
|
|
|
* @api {post} /api/settings [saveGeneralSettings]
|
|
|
|
* @apiDescription Save the global settings provided
|
|
|
|
* @apiName saveGeneralSettings
|
|
|
|
* @apiGroup Settings
|
|
|
|
*
|
2021-07-11 12:26:10 +00:00
|
|
|
* @apiParam {string} VideoPath webserver path to the videos
|
|
|
|
* @apiParam {string} EpisodePath webserver path to the tvshows
|
|
|
|
* @apiParam {string} MediacenterName overall name of the mediacenter
|
|
|
|
* @apiParam {string} Password new server password (-1 if no password set)
|
|
|
|
* @apiParam {bool} TMDBGrabbing TMDB grabbing support to grab tag info and thumbnails
|
|
|
|
* @apiParam {bool} DarkMode Darkmode enabled?
|
2021-05-23 12:21:44 +00:00
|
|
|
*
|
|
|
|
* @apiSuccess {string} result 'success' if successfully or error message if not
|
|
|
|
*/
|
2021-08-20 19:20:33 +00:00
|
|
|
gws.AddHandler("saveGeneralSettings", SettingsNode, func(info *gws.HandlerInfo) []byte {
|
2021-07-11 12:26:10 +00:00
|
|
|
var args types.SettingsType
|
2021-05-22 19:33:32 +00:00
|
|
|
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-07-11 12:26:10 +00:00
|
|
|
args.VideoPath, args.EpisodePath, args.Password,
|
|
|
|
args.MediacenterName, args.TMDBGrabbing, args.DarkMode)
|
2021-02-23 16:01:29 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// methods for handling reindexing and cleanup of db gravity
|
|
|
|
func reIndexHandling() {
|
2021-05-23 12:21:44 +00:00
|
|
|
/**
|
|
|
|
* @api {post} /api/settings [startReindex]
|
|
|
|
* @apiDescription Start Database video reindex Job
|
|
|
|
* @apiName startReindex
|
|
|
|
* @apiGroup Settings
|
|
|
|
*
|
|
|
|
* @apiSuccess {string} result 'success' if successfully or error message if not
|
|
|
|
*/
|
2021-08-20 19:20:33 +00:00
|
|
|
gws.AddHandler("startReindex", SettingsNode, func(info *gws.HandlerInfo) []byte {
|
2021-02-23 16:01:29 +00:00
|
|
|
videoparser.StartReindex()
|
|
|
|
return database.ManualSuccessResponse(nil)
|
|
|
|
})
|
|
|
|
|
2021-05-23 12:21:44 +00:00
|
|
|
/**
|
|
|
|
* @api {post} /api/settings [startTVShowReindex]
|
|
|
|
* @apiDescription Start Database TVShow reindex job
|
|
|
|
* @apiName startTVShowReindex
|
|
|
|
* @apiGroup Settings
|
|
|
|
*
|
|
|
|
* @apiSuccess {string} result 'success' if successfully or error message if not
|
|
|
|
*/
|
2021-08-20 19:20:33 +00:00
|
|
|
gws.AddHandler("startTVShowReindex", SettingsNode, func(info *gws.HandlerInfo) []byte {
|
2021-04-16 20:44:56 +00:00
|
|
|
videoparser.StartTVShowReindex()
|
|
|
|
return database.ManualSuccessResponse(nil)
|
|
|
|
})
|
|
|
|
|
2021-05-23 12:21:44 +00:00
|
|
|
/**
|
|
|
|
* @api {post} /api/settings [cleanupGravity]
|
|
|
|
* @apiDescription Start Database cleanup job
|
|
|
|
* @apiName cleanupGravity
|
|
|
|
* @apiGroup Settings
|
|
|
|
*/
|
2021-08-20 19:20:33 +00:00
|
|
|
gws.AddHandler("cleanupGravity", SettingsNode, func(info *gws.HandlerInfo) []byte {
|
2021-02-23 16:01:29 +00:00
|
|
|
videoparser.StartCleanup()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|