Skip to content

Commit

Permalink
modify: redisign post
Browse files Browse the repository at this point in the history
  • Loading branch information
sunwei committed Nov 6, 2024
1 parent 8b6deb1 commit 412b14d
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 12 deletions.
23 changes: 21 additions & 2 deletions ADRs/notes/mvp.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ CoverImage = 'cover.jpeg'" \
#### 创建Post

curl -X POST "http://127.0.0.1:1314/api/content?type=Post" \
-H "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOm51bGwsImV4cCI6IjIwMjQtMTItMDRUMDg6MTQ6NTIuNTk2MDI5KzA4OjAwIiwiaWF0IjpudWxsLCJpc3MiOm51bGwsImp0aSI6bnVsbCwibmJmIjpudWxsLCJzdWIiOm51bGwsInVzZXIiOiJtZUBzdW53ZWkueHl6In0.foManZwcdG0h52dCxeKY6jE6iTkdSZFcEbnGFanLZU0" \
-H "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOm51bGwsImV4cCI6IjIwMjQtMTItMDVUMTY6MTE6NDQuOTU3ODUxKzA4OjAwIiwiaWF0IjpudWxsLCJpc3MiOm51bGwsImp0aSI6bnVsbCwibmJmIjpudWxsLCJzdWIiOm51bGwsInVzZXIiOiJ1c2VyXzk3NjkxNkBtZGZyaWRheS5jb20ifQ.iLWi8wWHg2C9JuJsEQW7WB7m0k524Wcv_Ok0TW3j2zA" \
-F "type=Post" \
-F "title=关于我" \
-F "author=laoyuan" \
-F "params=weight: 1" \
-F "assets.0=@/Users/sunwei/Downloads/building.jpg" \
-F "assets.1=@/Users/sunwei/Downloads/laoyuan-bili.jpg" \
-F "content=- **个人长期陪跑教练**
- 企业级敏捷教练
- 研发团队效能顾问
Expand All @@ -61,6 +62,24 @@ curl -X POST "http://127.0.0.1:1314/api/content?type=Post" \
"



curl -X POST "http://127.0.0.1:1314/api/content?type=Post" \
-H "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOm51bGwsImV4cCI6IjIwMjQtMTItMDVUMTY6MTE6NDQuOTU3ODUxKzA4OjAwIiwiaWF0IjpudWxsLCJpc3MiOm51bGwsImp0aSI6bnVsbCwibmJmIjpudWxsLCJzdWIiOm51bGwsInVzZXIiOiJ1c2VyXzk3NjkxNkBtZGZyaWRheS5jb20ifQ.iLWi8wWHg2C9JuJsEQW7WB7m0k524Wcv_Ok0TW3j2zA" \
-F "type=Post" \
-F "content=- **个人长期陪跑教练**
- 企业级敏捷教练
- 研发团队效能顾问
- unFIX中文社区发起人
- 中国最大的敏捷主题个人自媒体(bilibili \"老袁讲敏捷\"
- \"老袁讲敏捷\" 公众号和视频号
- 长篇小说作家(湖北省作协会员)

\![good](building.jpg)

---

"

创建SitePost

curl -X POST "http://127.0.0.1:1314/api/content?type=SitePost" \
Expand All @@ -72,7 +91,7 @@ curl -X POST "http://127.0.0.1:1314/api/content?type=SitePost" \
#### Preview

curl -X POST "http://127.0.0.1:1314/api/preview?type=Site&id=2" \
-H "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOm51bGwsImV4cCI6IjIwMjQtMTItMDRUMDg6MTQ6NTIuNTk2MDI5KzA4OjAwIiwiaWF0IjpudWxsLCJpc3MiOm51bGwsImp0aSI6bnVsbCwibmJmIjpudWxsLCJzdWIiOm51bGwsInVzZXIiOiJtZUBzdW53ZWkueHl6In0.foManZwcdG0h52dCxeKY6jE6iTkdSZFcEbnGFanLZU0"
-H "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOm51bGwsImV4cCI6IjIwMjQtMTItMDZUMTk6MDk6MjcuNDUwNzA1KzA4OjAwIiwiaWF0IjpudWxsLCJpc3MiOm51bGwsImp0aSI6bnVsbCwibmJmIjpudWxsLCJzdWIiOm51bGwsInVzZXIiOiJ1c2VyXzYyNTIxNkBtZGZyaWRheS5jb20ifQ.ZJMDUiRshJUAUXts6lZCtPNDZnFyPqx-ujqeIfi6xJw"


#### Deployment
Expand Down
8 changes: 1 addition & 7 deletions internal/domain/content/valueobject/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ func (s *Post) String() string { return s.Title }
func (s *Post) Create(res http.ResponseWriter, req *http.Request) error {
// do form data validation for required fields
required := []string{
"title",
"content",
}

Expand Down Expand Up @@ -167,12 +166,7 @@ func (s *Post) FullContent() string {
}

func (s *Post) Markdown() ([]byte, error) {
const postTemplate = `---
title: {{.Title}}
author: {{.Author}}
{{.Params}}
---
const postTemplate = `
{{.Content}}
`
tmpl, err := template.New("toml").Parse(postTemplate)
Expand Down
1 change: 1 addition & 0 deletions internal/interfaces/api/content/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func (c *Content) Handle(next http.HandlerFunc) http.HandlerFunc {
} else if strings.HasPrefix(contentType, "multipart/form-data") {
// 支持 multipart/form-data
if err := req.ParseMultipartForm(4 << 20); err != nil { // 限制上传大小为 4 MB
fmt.Println("[content] Error parsing multipart form data:", err)
http.Error(res, "Failed to parse multipart form data", http.StatusBadRequest)
return
}
Expand Down
1 change: 1 addition & 0 deletions internal/interfaces/api/handler/handlecontent.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ func (s *Handler) postContent(res http.ResponseWriter, req *http.Request) {

t := req.URL.Query().Get("type")
if t == "" {
s.log.Errorf("Attempt to submit without type from %s", req.URL.String())
res.WriteHeader(http.StatusBadRequest)
return
}
Expand Down
18 changes: 15 additions & 3 deletions internal/interfaces/api/handler/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ func (s *Handler) StoreFiles(req *http.Request) (map[string]string, error) {

src, err := fds[0].Open()
if err != nil {
err := fmt.Errorf("Couldn't open uploaded file: %s", err)
err := fmt.Errorf("couldn't open uploaded file: %s", err)
s.log.Errorf("Error opening uploaded file: %s", err)
return nil, err

}
defer src.Close()

// Overwrite file if it exists by default
// support later : check if file at path exists, if so, add timestamp to file
Expand All @@ -78,22 +78,32 @@ func (s *Handler) StoreFiles(req *http.Request) (map[string]string, error) {
dst, err := os.Create(absPath)
if err != nil {
err := fmt.Errorf("failed to create destination file for upload: %s", err)
s.log.Errorf("Error creating destination file for upload: %s", err)
return nil, err
}

// copy file from src to dst on disk
var size int64
if size, err = io.Copy(dst, src); err != nil {
_ = src.Close()
_ = dst.Close()
err := fmt.Errorf("failed to copy uploaded file to destination: %s", err)
s.log.Errorf("Error copying uploaded file to destination: %s", err)
return nil, err
}

// Close the source and destination files explicitly
_ = src.Close()
_ = dst.Close()

// add name:urlPath to req.PostForm to be inserted into db
urlPath := fmt.Sprintf("/%s/%s/%s/%d/%02d/%s", urlPathPrefix, uploadDirName, s.db.UserDir(), tm.Year(), tm.Month(), filename)
urlPaths[name] = urlPath

// add upload information to db
go s.storeFileInfo(size, filename, urlPath, fds)
go func() {
s.storeFileInfo(size, filename, urlPath, fds)
}()
}

return urlPaths, nil
Expand All @@ -107,6 +117,8 @@ func (s *Handler) storeFileInfo(size int64, filename, urlPath string, fds []*mul
"content_length": []string{fmt.Sprintf("%d", size)},
}

s.log.Debugln("storeFileInfo: ", filename, urlPath, fmt.Sprintf("%d", size))

if err := s.adminApp.NewUpload(data); err != nil {
s.log.Errorf("Error saving file upload record to database: %v", err)
}
Expand Down

0 comments on commit 412b14d

Please sign in to comment.