Permalink
Browse files

only read config, if supplied

  • Loading branch information...
xenotor committed Jun 3, 2017
1 parent b87f392 commit 6375aa717b68625642d570eade07270943983a96
Showing with 43 additions and 70 deletions.
  1. +8 −1 chrometizer.go
  2. +26 −60 config/config.go
  3. BIN dist/chrometizer
  4. +1 −1 dist/md5sum.txt
  5. +5 −5 fs/files.go
  6. +2 −2 web/cast.go
  7. +1 −1 web/server.go
View
@@ -20,7 +20,14 @@ func main() {
os.Exit(1)
}
- // load config from /storage or write it from ENV
+ // test config
+ confErr := config.TestConfig()
+ if confErr != nil {
+ fmt.Println(confErr)
+ os.Exit(1)
+ }
+
+ // load config from /storage
config.LoadConfig()
ffmpeg.TranscodeAll()
View
@@ -2,11 +2,10 @@ package config
import (
"encoding/json"
+ "errors"
"fmt"
"io/ioutil"
"os"
- "strconv"
- "strings"
)
const FFMPEG_CMD = "ffmpeg"
@@ -40,88 +39,55 @@ var SFE = [...]string{"MKV", "MP4", "AVI", "MPEG", "MPG", "FLV", "3GP", "WEBM"}
var REQ_CODECS = [...]string{"E MP4", "E MATROSKA", "E ADTS", "E H264"}
+const MEDIA = "/storage"
+
type Config struct {
- Config_loc string
- Media_loc string
+ UID int
Exclude []string
Remove_orig bool
}
var Conf Config
func LoadConfig() {
-
- if readConfig() != nil {
- // ln if needed
- Conf.Media_loc = "/storage"
- Conf.Config_loc = Conf.Media_loc + "/.chrometizer.json"
-
- Conf.Exclude = nil
- exc := os.Getenv("EXC")
- if len(exc) > 0 {
- Conf.Exclude = strings.Split(exc, ",")
+ // defaults
+ Conf.UID = -1
+ Conf.Exclude = nil
+ Conf.Remove_orig = false
+
+ // load from .chrometizer.json if we have it
+ file, e := ioutil.ReadFile(MEDIA + "/.chrometizer.json")
+ if e == nil {
+ e = json.Unmarshal(file, &Conf)
+ if e == nil {
+ fmt.Printf("config loaded : %+v\n", Conf)
+ } else {
+ fmt.Println(e)
}
-
- Conf.Remove_orig = false
- rem := os.Getenv("REM")
- if len(rem) > 0 {
- Conf.Remove_orig, _ = strconv.ParseBool(rem)
- }
-
- // write the config
- StoreConfig()
}
-
- fmt.Printf("\nusing %s as config!", Conf.Config_loc)
-}
-
-func StoreConfig() error {
- jb, err := json.MarshalIndent(Conf, "", " ")
- if err != nil {
- fmt.Printf("\nMarshal error: %v\n", err)
- return err
- }
- return ioutil.WriteFile(Conf.Config_loc, jb, 0644)
-}
-
-func readConfig() error {
- // try to read from default location or return error
- file, e := ioutil.ReadFile("/storage/.chrometizer.json")
- if e != nil {
- return e
- }
-
- // config there & OK - scan & transcode
- return json.Unmarshal(file, &Conf)
}
// tests Media_loc - writable directory
-func ConfigTest(temp_conf *Config) (bool, JsonMessage) {
- ml := temp_conf.Media_loc
- fi, err := os.Stat(ml)
+func TestConfig() error {
+ fi, err := os.Stat(MEDIA)
if err != nil && os.IsNotExist(err) {
- return false, JsonMessage{"Media_loc", err.Error()}
+ return err
}
if !fi.IsDir() {
- return false, JsonMessage{"Media_loc", "Not a directory"}
+ return errors.New("/storage is not a directory")
}
- err = ioutil.WriteFile(ml+"/temp.txt", []byte("test"), 0644)
+ err = ioutil.WriteFile(MEDIA+"/temp.txt", []byte("test"), 0644)
if err != nil {
- return false, JsonMessage{"Media_loc", "Can not write to media directory"}
+ return err
}
- err = os.Remove(ml + "/temp.txt")
+ err = os.Remove(MEDIA + "/temp.txt")
if err != nil {
- return false, JsonMessage{"Media_loc", "Can not write to media directory"}
+ return err
}
- return true, JsonMessage{}
-}
-
-type JsonMessage struct {
- Id string
- Message string
+ return nil
}
View
Binary file not shown.
View
@@ -1 +1 @@
-2ce3b964f48fc835bdf058ff59f7df2a chrometizer
+569be84ff23ce444c74975d1296c76b8 chrometizer
View
@@ -24,7 +24,7 @@ func findVideos() (vfs []*VF) {
var all_vfs []*VF
- err := filepath.Walk(config.Conf.Media_loc, func(path string, f os.FileInfo, err error) error {
+ err := filepath.Walk(config.MEDIA, func(path string, f os.FileInfo, err error) error {
if !f.IsDir() && notExcluded(path) && isSupVideoFile(f, &path) {
all_vfs = append(all_vfs,
&VF{
@@ -81,13 +81,13 @@ func Sort(vfs []*VF, order int) []*VF {
}
func notExcluded(path string) bool {
- if len(config.Conf.Exclude) < 1 || len(path) == len(config.Conf.Media_loc) {
+ if len(config.Conf.Exclude) < 1 || len(path) == len(config.MEDIA) {
return true
}
- dir, _ := filepath.Split(path) // /a/b/c/file.fn -> /a/b/c/
- dir = dir[len(config.Conf.Media_loc):] // /a/b/c -> /c
- dir = strings.ToUpper(dir) // /c -> /C
+ dir, _ := filepath.Split(path) // /a/b/c/file.fn -> /a/b/c/
+ dir = dir[len(config.MEDIA):] // /a/b/c -> /c
+ dir = strings.ToUpper(dir) // /c -> /C
for _, exdir := range config.Conf.Exclude {
if strings.Contains(dir, strings.ToUpper(exdir)) {
View
@@ -66,7 +66,7 @@ func relVfs(vfs []*fs.VF) (rvfs []*fs.VF) {
for _, vf := range vfs {
rvfs = append(rvfs, &fs.VF{
vf.Name,
- vf.Path[len(config.Conf.Media_loc):], // make it relative!
+ vf.Path[len(config.MEDIA):], // make it relative!
vf.Mtime,
vf.Ready,
vf.Transcoding,
@@ -112,7 +112,7 @@ func HandleVnames(w http.ResponseWriter, r *http.Request) {
func vNames(vfs []*fs.VF) (vnames []Video) {
for _, vf := range vfs {
- vnames = append(vnames, Video{vf.Name, vf.Path[len(config.Conf.Media_loc):]})
+ vnames = append(vnames, Video{vf.Name, vf.Path[len(config.MEDIA):]})
}
return vnames
}
View
@@ -41,7 +41,7 @@ func HandleFile(w http.ResponseWriter, r *http.Request) {
}
path := strings.TrimPrefix(r.URL.Path, "/file/")
- http.ServeFile(w, r, config.Conf.Media_loc+"/"+path)
+ http.ServeFile(w, r, config.MEDIA+"/"+path)
}
func HandleApi(w http.ResponseWriter, r *http.Request) {

0 comments on commit 6375aa7

Please sign in to comment.