Remove settings.yaml; add default_profile.png to profile initialization

This commit is contained in:
Alessio 2024-02-26 17:37:34 -08:00
parent 4022717e96
commit 925f36c33a
6 changed files with 16 additions and 47 deletions

1
go.mod
View File

@ -15,5 +15,4 @@ require (
github.com/stretchr/testify v1.7.0
golang.org/x/net v0.9.0
golang.org/x/term v0.7.0
gopkg.in/yaml.v2 v2.4.0
)

3
go.sum
View File

@ -96,8 +96,7 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

View File

@ -9,7 +9,6 @@ import (
sql "github.com/jmoiron/sqlx"
"github.com/jmoiron/sqlx/reflectx"
_ "github.com/mattn/go-sqlite3"
"gopkg.in/yaml.v2"
"gitlab.com/offline-twitter/twitter_offline_engine/pkg/scraper"
)
@ -17,11 +16,11 @@ import (
//go:embed schema.sql
var sql_init string
type Settings struct{}
//go:embed default_profile.png
var default_profile_image []byte
type Profile struct {
ProfileDir string
Settings Settings
DB *sql.DB
}
@ -40,9 +39,9 @@ func NewProfile(target_dir string) (Profile, error) {
return Profile{}, fmt.Errorf("Could not create target %q:\n %w", target_dir, ErrTargetAlreadyExists)
}
settings_file := filepath.Join(target_dir, "settings.yaml")
sqlite_file := filepath.Join(target_dir, "twitter.db")
profile_images_dir := filepath.Join(target_dir, "profile_images")
default_profile_image_file := filepath.Join(target_dir, "profile_images/default_profile.png")
link_thumbnails_dir := filepath.Join(target_dir, "link_preview_images")
images_dir := filepath.Join(target_dir, "images")
videos_dir := filepath.Join(target_dir, "videos")
@ -62,24 +61,18 @@ func NewProfile(target_dir string) (Profile, error) {
InitializeDatabaseVersion(db)
db.Mapper = reflectx.NewMapperFunc("db", ToSnakeCase)
// Create `settings.yaml`
fmt.Printf("Creating............. %s\n", settings_file)
settings := Settings{}
data, err := yaml.Marshal(&settings)
if err != nil {
return Profile{}, fmt.Errorf("Error YAML-marshalling [empty!] settings file:\n %w", err)
}
err = os.WriteFile(settings_file, data, os.FileMode(0644))
if err != nil {
return Profile{}, fmt.Errorf("Error creating settings file %q:\n %w", settings_file, err)
}
// Create `profile_images`
fmt.Printf("Creating............. %s/\n", profile_images_dir)
err = os.Mkdir(profile_images_dir, os.FileMode(0755))
if err != nil {
return Profile{}, fmt.Errorf("Error creating %q:\n %w", profile_images_dir, err)
}
// Put the default profile image in it
fmt.Printf("Creating............. %s/\n", default_profile_image_file)
err = os.WriteFile(default_profile_image_file, default_profile_image, os.FileMode(0644))
if err != nil {
return Profile{}, fmt.Errorf("Error creating default profile image file %q:\n %w", default_profile_image, err)
}
// Create `link_thumbnail_images`
fmt.Printf("Creating............. %s/\n", link_thumbnails_dir)
@ -109,7 +102,7 @@ func NewProfile(target_dir string) (Profile, error) {
return Profile{}, fmt.Errorf("Error creating %q:\n %w", video_thumbnails_dir, err)
}
return Profile{target_dir, settings, db}, nil
return Profile{ProfileDir: target_dir, DB: db}, nil
}
// Loads the profile at the given location. Fails if the given directory is not a Profile.
@ -120,26 +113,9 @@ func NewProfile(target_dir string) (Profile, error) {
// returns:
// - the loaded Profile
func LoadProfile(profile_dir string) (Profile, error) {
settings_file := filepath.Join(profile_dir, "settings.yaml")
sqlite_file := filepath.Join(profile_dir, "twitter.db")
for _, file := range []string{
settings_file,
sqlite_file,
} {
if !file_exists(file) {
return Profile{}, fmt.Errorf("Invalid profile, could not find file: %s", file)
}
}
settings_data, err := os.ReadFile(settings_file)
if err != nil {
return Profile{}, fmt.Errorf("Error reading %q:\n %w", settings_file, err)
}
settings := Settings{}
err = yaml.Unmarshal(settings_data, &settings)
if err != nil {
return Profile{}, fmt.Errorf("Error YAML-unmarshalling %q:\n %w", settings_file, err)
if !file_exists(sqlite_file) {
return Profile{}, fmt.Errorf("Invalid profile, could not find file: %s", sqlite_file)
}
db := sql.MustOpen("sqlite3", fmt.Sprintf("%s?_foreign_keys=on&_journal_mode=WAL", sqlite_file))
@ -147,11 +123,9 @@ func LoadProfile(profile_dir string) (Profile, error) {
ret := Profile{
ProfileDir: profile_dir,
Settings: settings,
DB: db,
}
err = ret.check_and_update_version()
err := ret.check_and_update_version()
return ret, err
}

View File

@ -60,7 +60,7 @@ func TestNewProfile(t *testing.T) {
// Check files were created
contents, err := os.ReadDir(profile_path)
require.NoError(err)
assert.Len(contents, 7)
assert.Len(contents, 6)
expected_files := []struct {
filename string
@ -69,7 +69,6 @@ func TestNewProfile(t *testing.T) {
{"images", true},
{"link_preview_images", true},
{"profile_images", true},
{"settings.yaml", false},
{"twitter.db", false},
{"video_thumbnails", true},
{"videos", true},

View File

@ -9,14 +9,12 @@ if [[ -e "$THIS_DIR/profile" ]]; then
fi
mkdir $THIS_DIR/profile
touch $THIS_DIR/profile/settings.yaml
touch $THIS_DIR/profile/users.yaml
test -e $THIS_DIR/profile/twitter.db && rm $THIS_DIR/profile/twitter.db
sqlite3 $THIS_DIR/profile/twitter.db < $THIS_DIR/seed_data.sql
mkdir $THIS_DIR/profile/profile_images
cp $THIS_DIR/kwamurai_* $THIS_DIR/profile/profile_images
cp $THIS_DIR/../pkg/persistence/default_profile.png $THIS_DIR/profile/profile_images
mkdir $THIS_DIR/profile/images
cp $THIS_DIR/EYG* $THIS_DIR/profile/images