🎨 Update project structure
This commit is contained in:
113
pkg/internal/services/attachments.go
Normal file
113
pkg/internal/services/attachments.go
Normal file
@ -0,0 +1,113 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/database"
|
||||
"mime"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/models"
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func GetAttachmentByID(id uint) (models.Attachment, error) {
|
||||
var attachment models.Attachment
|
||||
if err := database.C.Where(models.Attachment{
|
||||
BaseModel: models.BaseModel{ID: id},
|
||||
}).First(&attachment).Error; err != nil {
|
||||
return attachment, err
|
||||
}
|
||||
return attachment, nil
|
||||
}
|
||||
|
||||
func GetAttachmentByUUID(id string) (models.Attachment, error) {
|
||||
var attachment models.Attachment
|
||||
if err := database.C.Where(models.Attachment{
|
||||
Uuid: id,
|
||||
}).First(&attachment).Error; err != nil {
|
||||
return attachment, err
|
||||
}
|
||||
return attachment, nil
|
||||
}
|
||||
|
||||
func GetAttachmentByHash(hash string) (models.Attachment, error) {
|
||||
var attachment models.Attachment
|
||||
if err := database.C.Where(models.Attachment{
|
||||
HashCode: hash,
|
||||
}).First(&attachment).Error; err != nil {
|
||||
return attachment, err
|
||||
}
|
||||
return attachment, nil
|
||||
}
|
||||
|
||||
func NewAttachmentMetadata(tx *gorm.DB, user models.Account, file *multipart.FileHeader, attachment models.Attachment) (models.Attachment, bool, error) {
|
||||
linked := false
|
||||
exists, pickupErr := GetAttachmentByHash(attachment.HashCode)
|
||||
if pickupErr == nil {
|
||||
linked = true
|
||||
exists.Alternative = attachment.Alternative
|
||||
exists.Usage = attachment.Usage
|
||||
exists.Metadata = attachment.Metadata
|
||||
attachment = exists
|
||||
attachment.ID = 0
|
||||
attachment.AccountID = user.ID
|
||||
} else {
|
||||
// Upload the new file
|
||||
attachment.Uuid = uuid.NewString()
|
||||
attachment.Size = file.Size
|
||||
attachment.Name = file.Filename
|
||||
attachment.AccountID = user.ID
|
||||
|
||||
// If user didn't provide file mimetype manually, we gotta to detect it
|
||||
if len(attachment.MimeType) == 0 {
|
||||
if ext := filepath.Ext(attachment.Name); len(ext) > 0 {
|
||||
// Detect mimetype by file extensions
|
||||
attachment.MimeType = mime.TypeByExtension(ext)
|
||||
} else {
|
||||
// Detect mimetype by file header
|
||||
// This method as a fallback method, because this isn't pretty accurate
|
||||
header, err := file.Open()
|
||||
if err != nil {
|
||||
return attachment, false, fmt.Errorf("failed to read file header: %v", err)
|
||||
}
|
||||
defer header.Close()
|
||||
|
||||
fileHeader := make([]byte, 512)
|
||||
_, err = header.Read(fileHeader)
|
||||
if err != nil {
|
||||
return attachment, false, err
|
||||
}
|
||||
attachment.MimeType = http.DetectContentType(fileHeader)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err := tx.Save(&attachment).Error; err != nil {
|
||||
return attachment, linked, fmt.Errorf("failed to save attachment record: %v", err)
|
||||
}
|
||||
|
||||
return attachment, linked, nil
|
||||
}
|
||||
|
||||
func DeleteAttachment(item models.Attachment) error {
|
||||
var dupeCount int64
|
||||
if err := database.C.
|
||||
Where(&models.Attachment{HashCode: item.HashCode}).
|
||||
Model(&models.Attachment{}).
|
||||
Count(&dupeCount).Error; err != nil {
|
||||
dupeCount = -1
|
||||
}
|
||||
|
||||
if err := database.C.Delete(&item).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if dupeCount != -1 && dupeCount <= 1 {
|
||||
return DeleteFile(item)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
76
pkg/internal/services/auth.go
Normal file
76
pkg/internal/services/auth.go
Normal file
@ -0,0 +1,76 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/database"
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/grpc"
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/models"
|
||||
"git.solsynth.dev/hydrogen/passport/pkg/grpc/proto"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func LinkAccount(userinfo *proto.Userinfo) (models.Account, error) {
|
||||
var account models.Account
|
||||
if userinfo == nil {
|
||||
return account, fmt.Errorf("remote userinfo was not found")
|
||||
}
|
||||
if err := database.C.Where(&models.Account{
|
||||
ExternalID: uint(userinfo.Id),
|
||||
}).First(&account).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
account = models.Account{
|
||||
Name: userinfo.Name,
|
||||
Nick: userinfo.Nick,
|
||||
Avatar: userinfo.Avatar,
|
||||
Banner: userinfo.Banner,
|
||||
Description: userinfo.GetDescription(),
|
||||
EmailAddress: userinfo.Email,
|
||||
PowerLevel: 0,
|
||||
ExternalID: uint(userinfo.Id),
|
||||
}
|
||||
return account, database.C.Save(&account).Error
|
||||
}
|
||||
return account, err
|
||||
}
|
||||
|
||||
prev := account
|
||||
account.Name = userinfo.Name
|
||||
account.Nick = userinfo.Nick
|
||||
account.Avatar = userinfo.Avatar
|
||||
account.Banner = userinfo.Banner
|
||||
account.Description = userinfo.GetDescription()
|
||||
account.EmailAddress = userinfo.Email
|
||||
|
||||
var err error
|
||||
if !reflect.DeepEqual(prev, account) {
|
||||
err = database.C.Save(&account).Error
|
||||
}
|
||||
|
||||
return account, err
|
||||
}
|
||||
|
||||
func Authenticate(atk, rtk string) (models.Account, string, string, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||
defer cancel()
|
||||
|
||||
var err error
|
||||
var user models.Account
|
||||
reply, err := grpc.Auth.Authenticate(ctx, &proto.AuthRequest{
|
||||
AccessToken: atk,
|
||||
RefreshToken: &rtk,
|
||||
})
|
||||
if err != nil {
|
||||
return user, reply.GetAccessToken(), reply.GetRefreshToken(), err
|
||||
} else if !reply.IsValid {
|
||||
return user, reply.GetAccessToken(), reply.GetRefreshToken(), fmt.Errorf("invalid authorization context")
|
||||
}
|
||||
|
||||
user, err = LinkAccount(reply.Userinfo)
|
||||
|
||||
return user, reply.GetAccessToken(), reply.GetRefreshToken(), err
|
||||
}
|
24
pkg/internal/services/cleaner.go
Normal file
24
pkg/internal/services/cleaner.go
Normal file
@ -0,0 +1,24 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
database2 "git.solsynth.dev/hydrogen/paperclip/pkg/internal/database"
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func DoAutoDatabaseCleanup() {
|
||||
deadline := time.Now().Add(60 * time.Minute)
|
||||
log.Debug().Time("deadline", deadline).Msg("Now cleaning up entire database...")
|
||||
|
||||
var count int64
|
||||
for _, model := range database2.AutoMaintainRange {
|
||||
tx := database2.C.Unscoped().Delete(model, "deleted_at >= ?", deadline)
|
||||
if tx.Error != nil {
|
||||
log.Error().Err(tx.Error).Msg("An error occurred when running auth context cleanup...")
|
||||
}
|
||||
count += tx.RowsAffected
|
||||
}
|
||||
|
||||
log.Debug().Int64("affected", count).Msg("Clean up entire database accomplished.")
|
||||
}
|
81
pkg/internal/services/jwt.go
Normal file
81
pkg/internal/services/jwt.go
Normal file
@ -0,0 +1,81 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"time"
|
||||
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type PayloadClaims struct {
|
||||
jwt.RegisteredClaims
|
||||
|
||||
Type string `json:"typ"`
|
||||
}
|
||||
|
||||
const (
|
||||
JwtAccessType = "access"
|
||||
JwtRefreshType = "refresh"
|
||||
)
|
||||
|
||||
const (
|
||||
CookieAccessKey = "passport_auth_key"
|
||||
CookieRefreshKey = "passport_refresh_key"
|
||||
)
|
||||
|
||||
func EncodeJwt(id string, typ, sub string, aud []string, exp time.Time) (string, error) {
|
||||
tk := jwt.NewWithClaims(jwt.SigningMethodHS512, PayloadClaims{
|
||||
jwt.RegisteredClaims{
|
||||
Subject: sub,
|
||||
Audience: aud,
|
||||
Issuer: fmt.Sprintf("https://%s", viper.GetString("domain")),
|
||||
ExpiresAt: jwt.NewNumericDate(exp),
|
||||
NotBefore: jwt.NewNumericDate(time.Now()),
|
||||
IssuedAt: jwt.NewNumericDate(time.Now()),
|
||||
ID: id,
|
||||
},
|
||||
typ,
|
||||
})
|
||||
|
||||
return tk.SignedString([]byte(viper.GetString("secret")))
|
||||
}
|
||||
|
||||
func DecodeJwt(str string) (PayloadClaims, error) {
|
||||
var claims PayloadClaims
|
||||
tk, err := jwt.ParseWithClaims(str, &claims, func(token *jwt.Token) (interface{}, error) {
|
||||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
|
||||
}
|
||||
return []byte(viper.GetString("secret")), nil
|
||||
})
|
||||
if err != nil {
|
||||
return claims, err
|
||||
}
|
||||
|
||||
if data, ok := tk.Claims.(*PayloadClaims); ok {
|
||||
return *data, nil
|
||||
} else {
|
||||
return claims, fmt.Errorf("unexpected token payload: not payload claims type")
|
||||
}
|
||||
}
|
||||
|
||||
func SetJwtCookieSet(c *fiber.Ctx, access, refresh string) {
|
||||
c.Cookie(&fiber.Cookie{
|
||||
Name: CookieAccessKey,
|
||||
Value: access,
|
||||
Domain: viper.GetString("security.cookie_domain"),
|
||||
SameSite: viper.GetString("security.cookie_samesite"),
|
||||
Expires: time.Now().Add(60 * time.Minute),
|
||||
Path: "/",
|
||||
})
|
||||
c.Cookie(&fiber.Cookie{
|
||||
Name: CookieRefreshKey,
|
||||
Value: refresh,
|
||||
Domain: viper.GetString("security.cookie_domain"),
|
||||
SameSite: viper.GetString("security.cookie_samesite"),
|
||||
Expires: time.Now().Add(24 * 30 * time.Hour),
|
||||
Path: "/",
|
||||
})
|
||||
}
|
61
pkg/internal/services/recycler.go
Normal file
61
pkg/internal/services/recycler.go
Normal file
@ -0,0 +1,61 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/models"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func DeleteFile(meta models.Attachment) error {
|
||||
destMap := viper.GetStringMap("destinations")
|
||||
dest, destOk := destMap[meta.Destination]
|
||||
if !destOk {
|
||||
return fmt.Errorf("invalid destination: destination configuration was not found")
|
||||
}
|
||||
|
||||
var destParsed models.BaseDestination
|
||||
rawDest, _ := jsoniter.Marshal(dest)
|
||||
_ = jsoniter.Unmarshal(rawDest, &destParsed)
|
||||
|
||||
switch destParsed.Type {
|
||||
case models.DestinationTypeLocal:
|
||||
var destConfigured models.LocalDestination
|
||||
_ = jsoniter.Unmarshal(rawDest, &destConfigured)
|
||||
return DeleteFileFromLocal(destConfigured, meta)
|
||||
case models.DestinationTypeS3:
|
||||
var destConfigured models.S3Destination
|
||||
_ = jsoniter.Unmarshal(rawDest, &destConfigured)
|
||||
return DeleteFileFromS3(destConfigured, meta)
|
||||
default:
|
||||
return fmt.Errorf("invalid destination: unsupported protocol %s", destParsed.Type)
|
||||
}
|
||||
}
|
||||
|
||||
func DeleteFileFromLocal(config models.LocalDestination, meta models.Attachment) error {
|
||||
fullpath := filepath.Join(config.Path, meta.Uuid)
|
||||
return os.Remove(fullpath)
|
||||
}
|
||||
|
||||
func DeleteFileFromS3(config models.S3Destination, meta models.Attachment) error {
|
||||
client, err := minio.New(config.Endpoint, &minio.Options{
|
||||
Creds: credentials.NewStaticV4(config.SecretID, config.SecretKey, ""),
|
||||
Secure: config.EnableSSL,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to configure s3 client: %v", err)
|
||||
}
|
||||
|
||||
err = client.RemoveObject(context.Background(), config.Bucket, filepath.Join(config.Path, meta.Uuid), minio.RemoveObjectOptions{})
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to upload file to s3: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
76
pkg/internal/services/uploader.go
Normal file
76
pkg/internal/services/uploader.go
Normal file
@ -0,0 +1,76 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"path/filepath"
|
||||
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/models"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func UploadFile(destName string, ctx *fiber.Ctx, file *multipart.FileHeader, meta models.Attachment) error {
|
||||
destMap := viper.GetStringMap("destinations")
|
||||
dest, destOk := destMap[destName]
|
||||
if !destOk {
|
||||
return fmt.Errorf("invalid destination: destination configuration was not found")
|
||||
}
|
||||
|
||||
var destParsed models.BaseDestination
|
||||
rawDest, _ := jsoniter.Marshal(dest)
|
||||
_ = jsoniter.Unmarshal(rawDest, &destParsed)
|
||||
|
||||
switch destParsed.Type {
|
||||
case models.DestinationTypeLocal:
|
||||
var destConfigured models.LocalDestination
|
||||
_ = jsoniter.Unmarshal(rawDest, &destConfigured)
|
||||
return UploadFileToLocal(destConfigured, ctx, file, meta)
|
||||
case models.DestinationTypeS3:
|
||||
var destConfigured models.S3Destination
|
||||
_ = jsoniter.Unmarshal(rawDest, &destConfigured)
|
||||
return UploadFileToS3(destConfigured, file, meta)
|
||||
default:
|
||||
return fmt.Errorf("invalid destination: unsupported protocol %s", destParsed.Type)
|
||||
}
|
||||
}
|
||||
|
||||
func UploadFileToLocal(config models.LocalDestination, ctx *fiber.Ctx, file *multipart.FileHeader, meta models.Attachment) error {
|
||||
return ctx.SaveFile(file, filepath.Join(config.Path, meta.Uuid))
|
||||
}
|
||||
|
||||
func UploadFileToS3(config models.S3Destination, file *multipart.FileHeader, meta models.Attachment) error {
|
||||
header, err := file.Open()
|
||||
if err != nil {
|
||||
return fmt.Errorf("read upload file: %v", err)
|
||||
}
|
||||
defer header.Close()
|
||||
|
||||
buffer := bytes.NewBuffer(nil)
|
||||
if _, err := io.Copy(buffer, header); err != nil {
|
||||
return fmt.Errorf("create io reader for upload file: %v", err)
|
||||
}
|
||||
|
||||
client, err := minio.New(config.Endpoint, &minio.Options{
|
||||
Creds: credentials.NewStaticV4(config.SecretID, config.SecretKey, ""),
|
||||
Secure: config.EnableSSL,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to configure s3 client: %v", err)
|
||||
}
|
||||
|
||||
_, err = client.PutObject(context.Background(), config.Bucket, filepath.Join(config.Path, meta.Uuid), buffer, -1, minio.PutObjectOptions{
|
||||
ContentType: meta.MimeType,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to upload file to s3: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user