-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: message, database, and router
- Loading branch information
1 parent
719bd64
commit 6775eee
Showing
12 changed files
with
269 additions
and
169 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,52 @@ | ||
package app | ||
|
||
import "github.com/ItsNotGoodName/smtpbridge/domain" | ||
import ( | ||
"path" | ||
|
||
"github.com/ItsNotGoodName/smtpbridge/dto" | ||
) | ||
|
||
type MessageListRequest struct { | ||
Limit int | ||
Offset int | ||
Page int | ||
AttachmentPath string | ||
} | ||
|
||
func (a *App) MessageList(req *MessageListRequest) ([]domain.Message, error) { | ||
return a.messageSVC.List(req.Limit, req.Offset) | ||
func (a *App) MessageList(req *MessageListRequest) ([]dto.Message, error) { | ||
if req.Page < 0 { | ||
req.Page = 0 | ||
} | ||
|
||
msgs, err := a.messageSVC.List(10, req.Page*10) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var result []dto.Message | ||
for _, msg := range msgs { | ||
var attachments []dto.Attachment | ||
for _, attachment := range msg.Attachments { | ||
attachments = append(attachments, dto.Attachment{ | ||
UUID: attachment.UUID, | ||
Name: attachment.Name, | ||
Path: path.Join(req.AttachmentPath, a.dao.Attachment.GetAttachmentFile(&attachment)), | ||
}) | ||
} | ||
|
||
var to []string | ||
for toAddr := range msg.To { | ||
to = append(to, toAddr) | ||
} | ||
|
||
result = append(result, dto.Message{ | ||
UUID: msg.UUID, | ||
From: msg.From, | ||
To: to, | ||
Status: msg.Status.String(), | ||
Subject: msg.Subject, | ||
Text: msg.Text, | ||
Attachments: attachments, | ||
}) | ||
} | ||
|
||
return result, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package dto | ||
|
||
type Message struct { | ||
UUID string `json:"uuid"` | ||
Status string `json:"status"` | ||
From string `json:"from"` | ||
To []string `json:"to"` | ||
Subject string `json:"subject"` | ||
Text string `json:"text"` | ||
Attachments []Attachment `json:"attachments"` | ||
} | ||
|
||
type Attachment struct { | ||
UUID string `json:"uuid"` | ||
Name string `json:"name"` | ||
Path string `json:"path"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
package router | ||
|
||
func (s *Router) route() { | ||
s.r.Get("/attachments/*", s.GetAttachments("/attachments/")) | ||
s.r.Get(s.attachmentURI+"*", s.GetAttachments()) | ||
s.r.Get("/", s.GetIndex()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package database | ||
|
||
import ( | ||
"fmt" | ||
"io/fs" | ||
"os" | ||
"path" | ||
|
||
"github.com/ItsNotGoodName/smtpbridge/domain" | ||
"github.com/asdine/storm" | ||
"github.com/asdine/storm/q" | ||
) | ||
|
||
func (db *DB) CreateAttachment(att *domain.Attachment) error { | ||
err := db.db.Save(att) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return os.WriteFile(db.getAttachmentPath(att), att.Data, 0644) | ||
} | ||
|
||
// getAttachmentPath returns the path to the attachment file on the file system. | ||
func (db *DB) getAttachmentPath(att *domain.Attachment) string { | ||
return path.Join(db.attDir, db.GetAttachmentFile(att)) | ||
} | ||
|
||
func (db *DB) GetAttachmentFile(att *domain.Attachment) string { | ||
return fmt.Sprintf("%s.%s", att.UUID, att.Type) | ||
} | ||
|
||
func (db *DB) GetAttachmentFS() fs.FS { | ||
return db.fs | ||
} | ||
|
||
func (db *DB) GetAttachment(uuid string) (*domain.Attachment, error) { | ||
var att domain.Attachment | ||
err := db.db.One("UUID", uuid, att) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &att, nil | ||
} | ||
|
||
func (db *DB) GetAttachmentData(att *domain.Attachment) ([]byte, error) { | ||
data, err := os.ReadFile(db.getAttachmentPath(att)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return data, nil | ||
} | ||
|
||
func (db *DB) GetAttachments(msg *domain.Message) ([]domain.Attachment, error) { | ||
var atts []domain.Attachment | ||
err := db.db.Select(q.Eq("MessageUUID", msg.UUID)).Find(&atts) | ||
if err != nil { | ||
if err == storm.ErrNotFound { | ||
return []domain.Attachment{}, nil | ||
} | ||
return nil, err | ||
} | ||
|
||
return atts, nil | ||
} |
Oops, something went wrong.