-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathassets.go
176 lines (162 loc) · 5.07 KB
/
assets.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
172
173
174
175
176
package handlers
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net/http"
"github.com/danielgtaylor/huma/v2"
"github.com/danielgtaylor/huma/v2/conditional"
"github.com/enchant97/note-mark/backend/config"
"github.com/enchant97/note-mark/backend/core"
"github.com/enchant97/note-mark/backend/middleware"
"github.com/enchant97/note-mark/backend/services"
"github.com/enchant97/note-mark/backend/storage"
"github.com/google/uuid"
)
func SetupAssetsHandler(
api huma.API,
appConfig config.AppConfig,
storage_backend storage.StorageController,
authProvider middleware.AuthDetailsProvider,
) {
assetsHandler := AssetsHandler{
AppConfig: appConfig,
Storage: storage_backend,
AuthProvider: authProvider,
}
huma.Register(api, huma.Operation{
Method: http.MethodPost,
Path: "/api/notes/{noteID}/assets",
Middlewares: huma.Middlewares{authProvider.AuthRequiredMiddleware},
MaxBodyBytes: int64(appConfig.AssetSizeLimit),
DefaultStatus: http.StatusCreated,
}, assetsHandler.PostNoteAsset)
huma.Get(api, "/api/notes/{noteID}/assets", assetsHandler.GetNoteAssets)
huma.Get(api, "/api/notes/{noteID}/assets/{assetID}", assetsHandler.GetNoteAssetContentByID)
huma.Register(api, huma.Operation{
Method: http.MethodDelete,
Path: "/api/notes/{noteID}/assets/{assetID}",
Middlewares: huma.Middlewares{authProvider.AuthRequiredMiddleware},
}, assetsHandler.DeleteNoteAssetByID)
}
type PostNoteAssetInput struct {
NoteID uuid.UUID `path:"noteID" format:"uuid"`
Name string `header:"X-Name" required:"true"`
RawBody []byte `required:"true"`
}
type PostNoteAssetOutput struct {
Body services.StoredAsset
}
type GetNoteAssetsInput struct {
NoteID uuid.UUID `path:"noteID" format:"uuid"`
}
type GetNoteAssetsOutput struct {
Body []services.StoredAsset
}
type GetNoteAssetContentByIDInput struct {
conditional.Params
NoteID uuid.UUID `path:"noteID" format:"uuid"`
AssetID uuid.UUID `path:"assetID" format:"uuid"`
}
type DeleteNoteAssetByIDInput struct {
NoteID uuid.UUID `path:"noteID" format:"uuid"`
AssetID uuid.UUID `path:"assetID" format:"uuid"`
}
type AssetsHandler struct {
services.AssetsService
AppConfig config.AppConfig
Storage storage.StorageController
AuthProvider middleware.AuthDetailsProvider
}
func (h AssetsHandler) PostNoteAsset(
ctx context.Context,
input *PostNoteAssetInput) (*PostNoteAssetOutput, error) {
authDetails, _ := h.AuthProvider.TryGetAuthDetails(ctx)
body := bytes.NewReader(input.RawBody)
if asset, err := h.AssetsService.CreateNoteAsset(
authDetails.GetAuthenticatedUser().UserID,
input.NoteID,
input.Name,
body,
h.Storage); err != nil {
if errors.Is(err, services.NotFoundError) {
return nil, huma.Error404NotFound("note does not exist or you do not have access")
} else if errors.Is(err, services.ConflictError) {
return nil, huma.Error409Conflict("asset with that name already exists")
} else {
return nil, err
}
} else {
return &PostNoteAssetOutput{Body: asset}, nil
}
}
func (h AssetsHandler) GetNoteAssets(
ctx context.Context,
input *GetNoteAssetsInput) (*GetNoteAssetsOutput, error) {
authDetails, _ := h.AuthProvider.TryGetAuthDetails(ctx)
if assets, err := h.AssetsService.GetNoteAssets(
authDetails.GetOptionalUserID(),
input.NoteID,
h.Storage); err != nil {
if errors.Is(err, services.NotFoundError) {
return nil, huma.Error404NotFound("note does not exist or you do not have access")
} else {
return nil, err
}
} else {
return &GetNoteAssetsOutput{Body: assets}, nil
}
}
// TODO Work out way to authenticate this
func (h AssetsHandler) GetNoteAssetContentByID(
ctx context.Context,
input *GetNoteAssetContentByIDInput) (*huma.StreamResponse, error) {
if asset, info, stream, err := h.AssetsService.GetNoteAssetContentByID(
input.NoteID,
input.AssetID,
h.Storage); err != nil {
if errors.Is(err, services.NotFoundError) {
return nil, huma.Error404NotFound("asset does not exist or you do not have access")
} else {
return nil, err
}
} else {
if input.HasConditionalParams() {
if err := input.PreconditionFailed(info.Checksum, info.FileInfo.LastModified); err != nil {
stream.Close()
return nil, err
}
}
return &huma.StreamResponse{
Body: func(ctx huma.Context) {
ctx.SetHeader("Content-Type", info.MimeType)
ctx.SetHeader(
"Last-Modified",
core.TimeIntoHTTPFormat(info.LastModified))
ctx.SetHeader(
"Content-Disposition",
fmt.Sprintf("inline; filename=\"%s\"", asset.Name))
writer := ctx.BodyWriter()
io.Copy(writer, stream)
stream.Close()
}}, nil
}
}
func (h AssetsHandler) DeleteNoteAssetByID(ctx context.Context, input *DeleteNoteAssetByIDInput) (*struct{}, error) {
authDetails, _ := h.AuthProvider.TryGetAuthDetails(ctx)
if err := h.AssetsService.DeleteNoteAssetByID(
authDetails.GetAuthenticatedUser().UserID,
input.NoteID,
input.AssetID,
h.Storage); err != nil {
if errors.Is(err, services.NotFoundError) {
return nil, huma.Error404NotFound("asset does not exist or you do not have access")
} else {
return nil, err
}
} else {
return nil, nil
}
}