diff --git a/config.yml b/config.yml index 074d8f4..21c9fd9 100644 --- a/config.yml +++ b/config.yml @@ -4,5 +4,6 @@ app_hash: bc7a2f7b8893889ffa6115f5f0eac278 bot_token: session_file: ./session.json log_file: ./log.json -max_size: 50M +output: /share/telegram/outputs +max_size: 50MB dsn: "postgresql://postgres:xixi0202@10.1.1.3:5432/telegram_resource?sslmode=disable" \ No newline at end of file diff --git a/config/config.go b/config/config.go index 7fd1712..0f5b3a5 100644 --- a/config/config.go +++ b/config/config.go @@ -16,12 +16,13 @@ type Config struct { LogFile string `mapstructure:"log_file"` MaxSize string `mapstructure:"max_size"` DSN string `mapstructure:"dsn"` + Outputs string `mapstructure:"outputs"` } // GetMaxSize func (c *Config) GetMaxSize() uint { - // parse 50m to 50 * 1024 * 1024 - return viper.GetSizeInBytes(c.MaxSize) + // parse 50mb to 50 * 1024 * 1024 + return viper.GetSizeInBytes("max_size") } func Load(path string) error { diff --git a/internal/db_channel.go b/internal/db_channel.go index aae3b77..b220e71 100644 --- a/internal/db_channel.go +++ b/internal/db_channel.go @@ -8,6 +8,7 @@ import ( "strings" "time" + "exporter/config" "exporter/database/telegram_resource/public/model" "exporter/database/telegram_resource/public/table" @@ -38,7 +39,8 @@ func NewDBChannel(uuid int64, username, title string) *DBChannel { } func (c *DBChannel) Asset(assetID int64, ext string) string { - assetFile := fmt.Sprintf("outputs/%d/%d.%s", c.UUID, assetID, strings.Trim(ext, ".")) + assetFile := fmt.Sprintf("%d/%d.%s", c.UUID, assetID, strings.Trim(ext, ".")) + assetFile = filepath.Join(config.C.Outputs, assetFile) // if file dir not exists then create it if _, err := os.Stat(filepath.Dir(assetFile)); os.IsNotExist(err) { diff --git a/main_test.go b/main_test.go index 92fbc1c..a654226 100644 --- a/main_test.go +++ b/main_test.go @@ -3,6 +3,9 @@ package main import ( "mime" "testing" + + "github.com/dustin/go-humanize" + "github.com/spf13/viper" ) func Test_Mime(t *testing.T) { @@ -12,3 +15,15 @@ func Test_Mime(t *testing.T) { t.Log(mime.ExtensionsByType(m)) } + +func Test_Size(t *testing.T) { + // Test code here + s := 6456390 + t.Logf("Size: %d", s) + t.Logf("Human: %s", humanize.Bytes(uint64(s))) + + viper.Set("size", "20MB") + b := viper.GetSizeInBytes("size") + t.Logf("Size: %d", s) + t.Logf("Vize: %d", b) +} diff --git a/outputs/.gitignore b/outputs/.gitignore deleted file mode 100644 index c96a04f..0000000 --- a/outputs/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -* -!.gitignore \ No newline at end of file