Skip to content

Commit

Permalink
Merge pull request #145 from picosh/am/lib-refactor
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
antoniomika authored Oct 8, 2024
2 parents 5fec9cc + 5b1432d commit 62eeceb
Show file tree
Hide file tree
Showing 56 changed files with 456 additions and 1,040 deletions.
11 changes: 6 additions & 5 deletions auth/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/picosh/pico/db"
"github.com/picosh/pico/db/postgres"
"github.com/picosh/pico/shared"
"github.com/picosh/utils"
)

type Client struct {
Expand Down Expand Up @@ -647,13 +648,13 @@ type AuthCfg struct {
}

func StartApiServer() {
debug := shared.GetEnv("AUTH_DEBUG", "0")
debug := utils.GetEnv("AUTH_DEBUG", "0")
cfg := &AuthCfg{
DbURL: shared.GetEnv("DATABASE_URL", ""),
DbURL: utils.GetEnv("DATABASE_URL", ""),
Debug: debug == "1",
Issuer: shared.GetEnv("AUTH_ISSUER", "pico.sh"),
Domain: shared.GetEnv("AUTH_DOMAIN", "http://0.0.0.0:3000"),
Port: shared.GetEnv("AUTH_WEB_PORT", "3000"),
Issuer: utils.GetEnv("AUTH_ISSUER", "pico.sh"),
Domain: utils.GetEnv("AUTH_DOMAIN", "http://0.0.0.0:3000"),
Port: utils.GetEnv("AUTH_WEB_PORT", "3000"),
}

logger := shared.CreateLogger("auth")
Expand Down
4 changes: 2 additions & 2 deletions cmd/scripts/analytics/analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/picosh/pico/db"
"github.com/picosh/pico/db/postgres"
"github.com/picosh/pico/shared"
"github.com/picosh/utils"
)

func main() {
Expand All @@ -23,7 +23,7 @@ func main() {
// By: "post_id",
By: "user_id",
Interval: "day",
Origin: shared.StartOfMonth(),
Origin: utils.StartOfMonth(),
// Where: "AND (post_id IS NOT NULL OR (post_id IS NULL AND project_id IS NULL))",
},
)
Expand Down
5 changes: 3 additions & 2 deletions cmd/scripts/clean-object-store/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/picosh/pico/pgs"
"github.com/picosh/pico/shared"
"github.com/picosh/pico/shared/storage"
"github.com/picosh/utils"
)

func bail(err error) {
Expand All @@ -27,7 +28,7 @@ type RmProject struct {
// have a corresponding project inside our database.
func main() {
// to actually commit changes, set to true
writeEnv := shared.GetEnv("WRITE", "0")
writeEnv := utils.GetEnv("WRITE", "0")
write := false
if writeEnv == "1" {
write = true
Expand Down Expand Up @@ -102,7 +103,7 @@ func main() {
}
}

session := &shared.CmdSessionLogger{
session := &utils.CmdSessionLogger{
Log: logger,
}

Expand Down
3 changes: 2 additions & 1 deletion cmd/scripts/shasum/shasum.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/picosh/pico/db/postgres"
"github.com/picosh/pico/shared"
"github.com/picosh/utils"
)

func main() {
Expand All @@ -24,7 +25,7 @@ func main() {
empty := 0
diff := 0
for _, post := range posts {
nextShasum := shared.Shasum([]byte(post.Text))
nextShasum := utils.Shasum([]byte(post.Text))
if post.Shasum == "" {
empty += 1
} else if post.Shasum != nextShasum {
Expand Down
4 changes: 2 additions & 2 deletions db/postgres/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

_ "github.com/lib/pq"
"github.com/picosh/pico/db"
"github.com/picosh/pico/shared"
"github.com/picosh/utils"
)

var PAGER_SIZE = 15
Expand Down Expand Up @@ -1552,7 +1552,7 @@ func (me *PsqlDB) FindFeedItemsByPostID(postID string) ([]*db.FeedItem, error) {
}

func (me *PsqlDB) InsertProject(userID, name, projectDir string) (string, error) {
if !shared.IsValidSubdomain(name) {
if !utils.IsValidSubdomain(name) {
return "", fmt.Errorf("'%s' is not a valid project name, must match /^[a-z0-9-]+$/", name)
}

Expand Down
2 changes: 1 addition & 1 deletion docker-compose.prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ services:
env_file:
- .env.prod
environment:
APP_DOMAIN: ${PUBSUB_DOMAIN:-send.pico.sh}
APP_DOMAIN: ${PUBSUB_DOMAIN:-pipe.pico.sh}
APP_EMAIL: ${PUBSUB_EMAIL:[email protected]}
volumes:
- ${PUBSUB_CADDYFILE}:/etc/caddy/Caddyfile
Expand Down
21 changes: 11 additions & 10 deletions feeds/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ package feeds

import (
"github.com/picosh/pico/shared"
"github.com/picosh/utils"
)

func NewConfigSite() *shared.ConfigSite {
debug := shared.GetEnv("FEEDS_DEBUG", "0")
domain := shared.GetEnv("FEEDS_DOMAIN", "feeds.pico.sh")
port := shared.GetEnv("FEEDS_WEB_PORT", "3000")
protocol := shared.GetEnv("FEEDS_PROTOCOL", "https")
storageDir := shared.GetEnv("IMGS_STORAGE_DIR", ".storage")
minioURL := shared.GetEnv("MINIO_URL", "")
minioUser := shared.GetEnv("MINIO_ROOT_USER", "")
minioPass := shared.GetEnv("MINIO_ROOT_PASSWORD", "")
dbURL := shared.GetEnv("DATABASE_URL", "")
sendgridKey := shared.GetEnv("SENDGRID_API_KEY", "")
debug := utils.GetEnv("FEEDS_DEBUG", "0")
domain := utils.GetEnv("FEEDS_DOMAIN", "feeds.pico.sh")
port := utils.GetEnv("FEEDS_WEB_PORT", "3000")
protocol := utils.GetEnv("FEEDS_PROTOCOL", "https")
storageDir := utils.GetEnv("IMGS_STORAGE_DIR", ".storage")
minioURL := utils.GetEnv("MINIO_URL", "")
minioUser := utils.GetEnv("MINIO_ROOT_USER", "")
minioPass := utils.GetEnv("MINIO_ROOT_PASSWORD", "")
dbURL := utils.GetEnv("DATABASE_URL", "")
sendgridKey := utils.GetEnv("SENDGRID_API_KEY", "")

return &shared.ConfigSite{
Debug: debug == "1",
Expand Down
7 changes: 4 additions & 3 deletions feeds/scp_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/picosh/pico/db"
"github.com/picosh/pico/filehandlers"
"github.com/picosh/pico/shared"
"github.com/picosh/utils"
)

type FeedHooks struct {
Expand All @@ -19,15 +20,15 @@ type FeedHooks struct {
}

func (p *FeedHooks) FileValidate(s ssh.Session, data *filehandlers.PostMetaData) (bool, error) {
if !shared.IsTextFile(string(data.Text)) {
if !utils.IsTextFile(string(data.Text)) {
err := fmt.Errorf(
"WARNING: (%s) invalid file must be plain text (utf-8), skipping",
data.Filename,
)
return false, err
}

if !shared.IsExtAllowed(data.Filename, p.Cfg.AllowedExt) {
if !utils.IsExtAllowed(data.Filename, p.Cfg.AllowedExt) {
extStr := strings.Join(p.Cfg.AllowedExt, ",")
err := fmt.Errorf(
"WARNING: (%s) invalid file, format must be (%s), skipping",
Expand All @@ -44,7 +45,7 @@ func (p *FeedHooks) FileMeta(s ssh.Session, data *filehandlers.PostMetaData) err
parsedText := shared.ListParseText(string(data.Text))

if parsedText.Title == "" {
data.Title = shared.ToUpper(data.Slug)
data.Title = utils.ToUpper(data.Slug)
} else {
data.Title = parsedText.Title
}
Expand Down
16 changes: 8 additions & 8 deletions feeds/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ import (
"github.com/picosh/pico/db/postgres"
"github.com/picosh/pico/filehandlers"
"github.com/picosh/pico/filehandlers/util"
"github.com/picosh/pico/shared"
"github.com/picosh/pico/shared/storage"
wsh "github.com/picosh/pico/wish"
"github.com/picosh/send/auth"
"github.com/picosh/send/list"
"github.com/picosh/send/pipe"
wishrsync "github.com/picosh/send/protocols/rsync"
"github.com/picosh/send/protocols/scp"
"github.com/picosh/send/protocols/sftp"
"github.com/picosh/send/proxy"
"github.com/picosh/send/send/auth"
wishrsync "github.com/picosh/send/send/rsync"
"github.com/picosh/send/send/scp"
"github.com/picosh/send/send/sftp"
"github.com/picosh/utils"
)

func createRouter(handler *filehandlers.FileHandlerRouter) proxy.Router {
Expand Down Expand Up @@ -52,9 +52,9 @@ func withProxy(handler *filehandlers.FileHandlerRouter, otherMiddleware ...wish.
}

func StartSshServer() {
host := shared.GetEnv("LISTS_HOST", "0.0.0.0")
port := shared.GetEnv("LISTS_SSH_PORT", "2222")
promPort := shared.GetEnv("LISTS_PROM_PORT", "9222")
host := utils.GetEnv("LISTS_HOST", "0.0.0.0")
port := utils.GetEnv("LISTS_SSH_PORT", "2222")
promPort := utils.GetEnv("LISTS_PROM_PORT", "9222")
cfg := NewConfigSite()
logger := cfg.Logger
dbh := postgres.NewDB(cfg.DbURL, cfg.Logger)
Expand Down
27 changes: 14 additions & 13 deletions filehandlers/assets/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import (
"github.com/picosh/pico/shared/storage"
"github.com/picosh/pobj"
sst "github.com/picosh/pobj/storage"
"github.com/picosh/send/send/utils"
sendutils "github.com/picosh/send/utils"
"github.com/picosh/utils"
ignore "github.com/sabhiram/go-gitignore"
)

Expand Down Expand Up @@ -91,7 +92,7 @@ func shouldIgnoreFile(fp, ignoreStr string) bool {
}

type FileData struct {
*utils.FileEntry
*sendutils.FileEntry
User *db.User
Bucket sst.Bucket
Project *db.Project
Expand All @@ -116,13 +117,13 @@ func (h *UploadAssetHandler) GetLogger() *slog.Logger {
return h.Cfg.Logger
}

func (h *UploadAssetHandler) Read(s ssh.Session, entry *utils.FileEntry) (os.FileInfo, utils.ReaderAtCloser, error) {
func (h *UploadAssetHandler) Read(s ssh.Session, entry *sendutils.FileEntry) (os.FileInfo, sendutils.ReaderAtCloser, error) {
user, err := futil.GetUser(s.Context())
if err != nil {
return nil, nil, err
}

fileInfo := &utils.VirtualFile{
fileInfo := &sendutils.VirtualFile{
FName: filepath.Base(entry.Filepath),
FIsDir: false,
FSize: entry.Size,
Expand Down Expand Up @@ -170,7 +171,7 @@ func (h *UploadAssetHandler) List(s ssh.Session, fpath string, isDir bool, recur
name = "/"
}

info := &utils.VirtualFile{
info := &sendutils.VirtualFile{
FName: name,
FIsDir: true,
}
Expand Down Expand Up @@ -243,7 +244,7 @@ func (h *UploadAssetHandler) findDenylist(bucket sst.Bucket, project *db.Project
return str, nil
}

func (h *UploadAssetHandler) Write(s ssh.Session, entry *utils.FileEntry) (string, error) {
func (h *UploadAssetHandler) Write(s ssh.Session, entry *sendutils.FileEntry) (string, error) {
user, err := futil.GetUser(s.Context())
if err != nil {
h.Cfg.Logger.Error("user not found in ctx", "err", err.Error())
Expand Down Expand Up @@ -365,17 +366,17 @@ func (h *UploadAssetHandler) Write(s ssh.Session, entry *utils.FileEntry) (strin
)

fsize, err := h.writeAsset(
shared.NewMaxBytesReader(data.Reader, int64(sizeRemaining)),
utils.NewMaxBytesReader(data.Reader, int64(sizeRemaining)),
data,
)
if err != nil {
logger.Error("could not write asset", "err", err.Error())
cerr := fmt.Errorf(
"%s: storage size %.2fmb, storage max %.2fmb, file max %.2fmb",
err,
shared.BytesToMB(int(curStorageSize)),
shared.BytesToMB(int(storageMax)),
shared.BytesToMB(int(fileMax)),
utils.BytesToMB(int(curStorageSize)),
utils.BytesToMB(int(storageMax)),
utils.BytesToMB(int(fileMax)),
)
return "", cerr
}
Expand All @@ -393,15 +394,15 @@ func (h *UploadAssetHandler) Write(s ssh.Session, entry *utils.FileEntry) (strin
str := fmt.Sprintf(
"%s (space: %.2f/%.2fGB, %.2f%%)",
url,
shared.BytesToGB(int(nextStorageSize)),
shared.BytesToGB(maxSize),
utils.BytesToGB(int(nextStorageSize)),
utils.BytesToGB(maxSize),
(float32(nextStorageSize)/float32(maxSize))*100,
)

return str, nil
}

func (h *UploadAssetHandler) Delete(s ssh.Session, entry *utils.FileEntry) error {
func (h *UploadAssetHandler) Delete(s ssh.Session, entry *sendutils.FileEntry) error {
user, err := futil.GetUser(s.Context())
if err != nil {
h.Cfg.Logger.Error("user not found in ctx", "err", err.Error())
Expand Down
21 changes: 11 additions & 10 deletions filehandlers/imgs/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import (
"github.com/picosh/pico/shared"
"github.com/picosh/pico/shared/storage"
"github.com/picosh/pobj"
"github.com/picosh/send/send/utils"
sendutils "github.com/picosh/send/utils"
"github.com/picosh/utils"
)

var Space = "imgs"
Expand All @@ -29,7 +30,7 @@ type PostMetaData struct {
Cur *db.Post
Tags []string
User *db.User
*utils.FileEntry
*sendutils.FileEntry
FeatureFlag *db.FeatureFlag
}

Expand All @@ -47,7 +48,7 @@ func NewUploadImgHandler(dbpool db.DB, cfg *shared.ConfigSite, storage storage.S
}
}

func (h *UploadImgHandler) Read(s ssh.Session, entry *utils.FileEntry) (os.FileInfo, utils.ReaderAtCloser, error) {
func (h *UploadImgHandler) Read(s ssh.Session, entry *sendutils.FileEntry) (os.FileInfo, sendutils.ReaderAtCloser, error) {
user, err := util.GetUser(s.Context())
if err != nil {
return nil, nil, err
Expand All @@ -64,7 +65,7 @@ func (h *UploadImgHandler) Read(s ssh.Session, entry *utils.FileEntry) (os.FileI
return nil, nil, err
}

fileInfo := &utils.VirtualFile{
fileInfo := &sendutils.VirtualFile{
FName: post.Filename,
FIsDir: false,
FSize: int64(post.FileSize),
Expand All @@ -86,7 +87,7 @@ func (h *UploadImgHandler) Read(s ssh.Session, entry *utils.FileEntry) (os.FileI
return fileInfo, reader, nil
}

func (h *UploadImgHandler) Write(s ssh.Session, entry *utils.FileEntry) (string, error) {
func (h *UploadImgHandler) Write(s ssh.Session, entry *sendutils.FileEntry) (string, error) {
logger := h.Cfg.Logger
user, err := util.GetUser(s.Context())
if err != nil {
Expand Down Expand Up @@ -123,8 +124,8 @@ func (h *UploadImgHandler) Write(s ssh.Session, entry *utils.FileEntry) (string,

now := time.Now()
fileSize := binary.Size(text)
shasum := shared.Shasum(text)
slug := shared.SanitizeFileExt(filename)
shasum := utils.Shasum(text)
slug := utils.SanitizeFileExt(filename)

nextPost := db.Post{
Filename: filename,
Expand Down Expand Up @@ -184,14 +185,14 @@ func (h *UploadImgHandler) Write(s ssh.Session, entry *utils.FileEntry) (string,
str := fmt.Sprintf(
"%s (space: %.2f/%.2fGB, %.2f%%)",
url,
shared.BytesToGB(totalFileSize),
shared.BytesToGB(maxSize),
utils.BytesToGB(totalFileSize),
utils.BytesToGB(maxSize),
(float32(totalFileSize)/float32(maxSize))*100,
)
return str, nil
}

func (h *UploadImgHandler) Delete(s ssh.Session, entry *utils.FileEntry) error {
func (h *UploadImgHandler) Delete(s ssh.Session, entry *sendutils.FileEntry) error {
user, err := util.GetUser(s.Context())
if err != nil {
return err
Expand Down
Loading

0 comments on commit 62eeceb

Please sign in to comment.