/
new_post.go
171 lines (131 loc) · 3.26 KB
/
new_post.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package admin
import (
"bytes"
"context"
"fmt"
"image/jpeg"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"text/template"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/google/uuid"
"github.com/lib/pq"
"gorm.io/gorm"
)
type Post struct {
Id string `json:"id"`
Image string `json:"image"`
Title string `json:"title"`
Content string `json:"content"`
Description string `json:"description"`
Tags pq.StringArray `json:"tags" gorm:"type:text[]"`
}
func NewPost(w http.ResponseWriter, r *http.Request, db *gorm.DB) error {
tmpl, err := template.ParseFiles("templates/admin/new_post.html")
if err != nil {
return err
}
tmpl.Execute(w, nil)
return nil
}
func CreateNewPost(w http.ResponseWriter, r *http.Request, db *gorm.DB) error {
err := r.ParseMultipartForm(10 << 20)
if err != nil {
return err
}
tags := strings.Split(r.PostForm.Get("tags"), ",")
for i, tag := range tags {
if tag == "" {
tags = append(tags[:i], tags[:i]...)
}
}
post := Post{
Id: uuid.NewString(),
Title: r.PostForm.Get("title"),
Content: r.PostForm.Get("content"),
Description: r.PostForm.Get("description"),
Tags: tags,
}
file, fileHeader, err := r.FormFile("image")
if err != nil {
return err
}
dst, err := os.Create(fmt.Sprintf("./%s%s", post.Id, filepath.Ext(fileHeader.Filename)))
if err != nil {
return err
}
_, err = io.Copy(dst, file)
if err != nil {
return err
}
dst.Close()
file.Close()
err = uploadImage(post.Id)
if err != nil {
return err
}
post.Image = fmt.Sprintf("%s/%s", os.Getenv("STORAGE_BUCKET"), post.Id+".jpg")
err = db.Create(&post).Error
if err != nil {
return err
}
w.WriteHeader(200)
w.Write([]byte("The post was created."))
return nil
}
func uploadImage(id string) error {
ctx := context.Background()
r2Resolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
return aws.Endpoint{
URL: fmt.Sprintf("https://%s.r2.cloudflarestorage.com", os.Getenv("ACCOUNT_ID")),
HostnameImmutable: true,
Source: aws.EndpointSourceCustom,
}, nil
})
cfg, err := config.LoadDefaultConfig(
ctx,
config.WithEndpointResolverWithOptions(r2Resolver),
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(
os.Getenv("STORAGE_BUCKET_KEY_ID"),
os.Getenv("STORAGE_BUCKET_KEY_SECRET"),
"",
)),
config.WithRegion("auto"),
)
if err != nil {
return err
}
client := s3.NewFromConfig(cfg)
f, err := os.Open(fmt.Sprintf("./%s.jpg", id))
if err != nil {
return err
}
image, err := jpeg.Decode(f)
if err != nil {
return err
}
var imageBuffer bytes.Buffer
if err := jpeg.Encode(&imageBuffer, image, nil); err != nil {
return err
}
_, err = client.PutObject(ctx, &s3.PutObjectInput{
Bucket: aws.String(os.Getenv("STORAGE_BUCKET_NAME")),
Key: aws.String(id + ".jpg"),
Body: bytes.NewReader(imageBuffer.Bytes()),
})
if err != nil {
return err
}
f.Close()
err = os.Remove(fmt.Sprintf("./%s.jpg", id))
if err != nil {
return err
}
return nil
}