2020-01-20 11:17:43 -09:00
package server
import (
2020-04-26 08:06:38 -08:00
"context"
2020-02-08 10:50:33 -09:00
"fmt"
2020-10-06 13:24:16 -08:00
"os/exec"
2020-01-20 11:17:43 -09:00
"time"
2020-02-08 10:50:33 -09:00
"github.com/deluan/navidrome/conf"
2020-01-23 15:44:08 -09:00
"github.com/deluan/navidrome/consts"
"github.com/deluan/navidrome/log"
"github.com/deluan/navidrome/model"
2020-01-20 11:17:43 -09:00
"github.com/google/uuid"
)
func initialSetup ( ds model . DataStore ) {
_ = ds . WithTx ( func ( tx model . DataStore ) error {
2020-04-26 08:06:38 -08:00
properties := ds . Property ( context . TODO ( ) )
_ , err := properties . Get ( consts . InitialSetupFlagKey )
2020-01-20 11:17:43 -09:00
if err == nil {
return nil
}
log . Warn ( "Running initial setup" )
if err = createJWTSecret ( ds ) ; err != nil {
return err
}
2020-02-08 10:50:33 -09:00
if conf . Server . DevAutoCreateAdminPassword != "" {
if err = createInitialAdminUser ( ds ) ; err != nil {
return err
}
}
2020-04-26 08:06:38 -08:00
err = properties . Put ( consts . InitialSetupFlagKey , time . Now ( ) . String ( ) )
2020-01-20 11:17:43 -09:00
return err
} )
}
2020-02-08 10:50:33 -09:00
func createInitialAdminUser ( ds model . DataStore ) error {
2020-04-26 08:06:38 -08:00
users := ds . User ( context . TODO ( ) )
c , err := users . CountAll ( )
2020-02-08 10:50:33 -09:00
if err != nil {
panic ( fmt . Sprintf ( "Could not access User table: %s" , err ) )
}
if c == 0 {
id , _ := uuid . NewRandom ( )
random , _ := uuid . NewRandom ( )
initialPassword := random . String ( )
if conf . Server . DevAutoCreateAdminPassword != "" {
initialPassword = conf . Server . DevAutoCreateAdminPassword
}
log . Warn ( "Creating initial admin user. This should only be used for development purposes!!" , "user" , consts . DevInitialUserName , "password" , initialPassword )
initialUser := model . User {
ID : id . String ( ) ,
UserName : consts . DevInitialUserName ,
Name : consts . DevInitialName ,
Email : "" ,
Password : initialPassword ,
IsAdmin : true ,
}
2020-04-26 08:06:38 -08:00
err := users . Put ( & initialUser )
2020-02-08 10:50:33 -09:00
if err != nil {
log . Error ( "Could not create initial admin user" , "user" , initialUser , err )
}
}
return err
}
2020-01-20 11:17:43 -09:00
func createJWTSecret ( ds model . DataStore ) error {
2020-04-26 08:06:38 -08:00
properties := ds . Property ( context . TODO ( ) )
_ , err := properties . Get ( consts . JWTSecretKey )
2020-01-20 11:17:43 -09:00
if err == nil {
return nil
}
jwtSecret , _ := uuid . NewRandom ( )
log . Warn ( "Creating JWT secret, used for encrypting UI sessions" )
2020-04-26 08:06:38 -08:00
err = properties . Put ( consts . JWTSecretKey , jwtSecret . String ( ) )
2020-01-20 11:17:43 -09:00
if err != nil {
log . Error ( "Could not save JWT secret in DB" , err )
}
return err
}
2020-10-06 13:24:16 -08:00
func checkFfmpegInstallation ( ) {
path , err := exec . LookPath ( "ffmpeg" )
if err == nil {
log . Debug ( "Found ffmpeg" , "path" , path )
}
log . Warn ( "Unable to find ffmpeg. Transcoding will fail if used" , err )
if conf . Server . Scanner . Extractor == "ffmpeg" {
log . Warn ( "ffmpeg cannot be used for metadata extraction. Falling back to taglib" )
conf . Server . Scanner . Extractor = "taglib"
}
}