feat: login

This commit is contained in:
Rogee
2024-09-02 14:50:51 +08:00
parent 97e0731e2e
commit d739ca0184
7 changed files with 80 additions and 51 deletions

View File

@@ -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
}