mirror of
https://git.mills.io/saltyim/saltyim.git
synced 2024-06-28 09:41:02 +00:00
3fccb3ae5f
This PR also: - Tidies up the default options and config - Tidies up the service user code Co-authored-by: James Mills <prologic@shortcircuit.net.au> Reviewed-on: https://git.mills.io/saltyim/saltyim/pulls/116
73 lines
1.5 KiB
Go
73 lines
1.5 KiB
Go
package internal
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"go.mills.io/saltyim"
|
|
)
|
|
|
|
const (
|
|
wellknownPath = ".well-known/salty"
|
|
avatarsPath = "avatars"
|
|
avatarResolution = 80 // 80x80 px
|
|
)
|
|
|
|
func CreateConfig(conf *Config, hash string, key string) error {
|
|
p := filepath.Join(conf.Data, wellknownPath)
|
|
p = saltyim.FixUnixHome(p)
|
|
|
|
fn := filepath.Join(p, fmt.Sprintf("%s.json", hash))
|
|
if FileExists(fn) {
|
|
return nil
|
|
}
|
|
|
|
if err := os.MkdirAll(p, 0755); err != nil {
|
|
return fmt.Errorf("error creating config paths %s: %w", p, err)
|
|
}
|
|
|
|
ulid, err := saltyim.GenerateULID()
|
|
if err != nil {
|
|
return fmt.Errorf("error generating ulid")
|
|
}
|
|
|
|
endpointURL := *conf.baseURL
|
|
endpointURL.Path = fmt.Sprintf("/inbox/%s", ulid)
|
|
|
|
config := saltyim.Config{
|
|
Endpoint: endpointURL.String(),
|
|
Key: key,
|
|
}
|
|
|
|
data, err := json.Marshal(config)
|
|
if err != nil {
|
|
return fmt.Errorf("error serializing config")
|
|
}
|
|
|
|
if err := os.WriteFile(fn, data, 0644); err != nil {
|
|
return fmt.Errorf("error writing config to %s: %w", fn, err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func CreateOrUpdateAvatar(conf *Config, addr *saltyim.Addr, contents []byte) error {
|
|
p := filepath.Join(conf.Data, avatarsPath)
|
|
p = saltyim.FixUnixHome(p)
|
|
|
|
if err := os.MkdirAll(p, 0755); err != nil {
|
|
return fmt.Errorf("error creating avatars paths %s: %w", p, err)
|
|
}
|
|
|
|
fn := filepath.Join(p, fmt.Sprintf("%s.png", addr.Hash()))
|
|
|
|
if err := SaveAvatar(fn, bytes.NewBuffer(contents), avatarResolution); err != nil {
|
|
return fmt.Errorf("error writing avatar %s: %w", fn, err)
|
|
}
|
|
|
|
return nil
|
|
}
|