feat: login
This commit is contained in:
@@ -2,7 +2,9 @@ package internal
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/gotd/td/telegram/downloader"
|
||||
@@ -64,3 +66,33 @@ func (t *TClient) Channel(ctx context.Context, channel *tg.Channel, offset int)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type ChannelConfig struct {
|
||||
ID int64 `json:"id"`
|
||||
Offset int `json:"offset"`
|
||||
}
|
||||
|
||||
func (t *TClient) SaveChannelConfig(ctx context.Context, channelID int64, offset int) (*ChannelConfig, error) {
|
||||
channelConfigFile := fmt.Sprintf("outputs/%d/config.json", channelID)
|
||||
// if file not exists then create it
|
||||
if _, err := os.Stat(channelConfigFile); os.IsNotExist(err) {
|
||||
// create config file
|
||||
data, _ := json.Marshal(&ChannelConfig{ID: channelID})
|
||||
if err := os.WriteFile(channelConfigFile, data, 0o644); err != nil {
|
||||
return nil, errors.Wrap(err, "write channel config")
|
||||
}
|
||||
}
|
||||
|
||||
// read config file
|
||||
data, err := os.ReadFile(channelConfigFile)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "read channel config")
|
||||
}
|
||||
|
||||
var config *ChannelConfig
|
||||
if err := json.Unmarshal(data, config); err != nil {
|
||||
return nil, errors.Wrap(err, "unmarshal channel config")
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user