/
controller.go
163 lines (147 loc) · 5.04 KB
/
controller.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
package controller
import (
"context"
"fmt"
"io"
"net/http"
"time"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/nhost/hasura-storage/image"
"github.com/sirupsen/logrus"
)
type FileSummary struct {
ID string `json:"id"`
Name string `json:"name"`
IsUploaded bool `json:"isUploaded"`
BucketID string `json:"bucketId"`
}
type BucketMetadata struct {
ID string
MinUploadFile int
MaxUploadFile int
PresignedURLsEnabled bool
DownloadExpiration int
CreatedAt string
UpdatedAt string
CacheControl string
}
type FileMetadata struct {
ID string `json:"id"`
Name string `json:"name"`
Size int64 `json:"size"`
BucketID string `json:"bucketId"`
ETag string `json:"etag"`
CreatedAt string `json:"createdAt"`
UpdatedAt string `json:"updatedAt"`
IsUploaded bool `json:"isUploaded"`
MimeType string `json:"mimeType"`
UploadedByUserID string `json:"uploadedByUserId"`
}
type FileMetadataWithBucket struct {
FileMetadata
Bucket BucketMetadata
}
//go:generate mockgen --build_flags=--mod=mod -destination mock_controller/metadata_storage.go -package mock_controller . MetadataStorage
type MetadataStorage interface {
GetBucketByID(ctx context.Context, id string, headers http.Header) (BucketMetadata, *APIError)
GetFileByID(ctx context.Context, id string, headers http.Header) (FileMetadata, *APIError)
InitializeFile(ctx context.Context, uuid string, headers http.Header) *APIError
PopulateMetadata(
ctx context.Context,
id, name string, size int64, bucketID, etag string, IsUploaded bool, mimeType string,
headers http.Header) (FileMetadata, *APIError,
)
SetIsUploaded(ctx context.Context, fileID string, isUploaded bool, headers http.Header) *APIError
DeleteFileByID(ctx context.Context, fileID string, headers http.Header) (FileMetadataWithBucket, *APIError)
ListFiles(ctx context.Context, headers http.Header) ([]FileSummary, *APIError)
}
//go:generate mockgen --build_flags=--mod=mod -destination mock_controller/content_storage.go -package mock_controller . ContentStorage
type ContentStorage interface {
PutFile(content io.ReadSeeker, filepath, contentType string) (string, *APIError)
GetFile(filepath string) (io.ReadCloser, *APIError)
CreatePresignedURL(filepath string, expire time.Duration) (string, *APIError)
GetFileWithPresignedURL(
ctx context.Context, filepath, signature string, headers http.Header,
) (*FileWithPresignedURL, *APIError)
DeleteFile(filepath string) *APIError
ListFiles() ([]string, *APIError)
}
type Controller struct {
publicURL string
hasuraAdminSecret string
metadataStorage MetadataStorage
contentStorage ContentStorage
imageTransformer *image.Transformer
logger *logrus.Logger
}
func New(
publicURL string,
hasuraAdminSecret string,
metadataStorage MetadataStorage,
contentStorage ContentStorage,
imageTransformer *image.Transformer,
logger *logrus.Logger,
) *Controller {
return &Controller{
publicURL,
hasuraAdminSecret,
metadataStorage,
contentStorage,
imageTransformer,
logger,
}
}
func (ctrl *Controller) SetupRouter(trustedProxies []string, logger gin.HandlerFunc) (*gin.Engine, error) {
router := gin.New()
if err := router.SetTrustedProxies(trustedProxies); err != nil {
return nil, fmt.Errorf("problem setting trusted proxies: %w", err)
}
router.MaxMultipartMemory = 1000 << 20 // nolint:gomnd // 1GB
router.Use(gin.Recovery())
router.Use(logger)
router.Use(cors.New(cors.Config{
AllowOrigins: []string{"*"},
AllowMethods: []string{"GET", "PUT", "POST", "HEAD", "DELETE"},
AllowHeaders: []string{
"Authorization", "Origin", "if-match", "if-none-match", "if-modified-since", "if-unmodified-since",
"x-hasura-admin-secret", "x-nhost-bucket-id", "x-nhost-file-name", "x-nhost-file-id",
},
// AllowWildcard: true,
ExposeHeaders: []string{
"Content-Length", "Content-Type", "Cache-Control", "ETag", "Last-Modified", "X-Error",
},
MaxAge: 12 * time.Hour, // nolint: gomnd
}))
router.GET("/healthz", ctrl.Health)
apiRoot := router.Group("/v1/storage")
{
apiRoot.GET("/openapi.yaml", ctrl.OpenAPI)
apiRoot.GET("/version", ctrl.Version)
}
files := apiRoot.Group("/files")
{
files.POST("", ctrl.UploadFile) // To delete
files.POST("/", ctrl.UploadFile)
files.GET("/:id", ctrl.GetFile)
files.HEAD("/:id", ctrl.GetFileInformation)
files.PUT("/:id", ctrl.UpdateFile)
files.DELETE("/:id", ctrl.DeleteFile)
files.GET("/:id/presignedurl", ctrl.GetFilePresignedURL)
files.GET("/:id/presignedurl/content", ctrl.GetFileWithPresignedURL)
}
ops := apiRoot.Group("/ops")
{
ops.POST("list-orphans", ctrl.ListOrphans)
ops.POST("delete-orphans", ctrl.DeleteOrphans)
ops.POST("list-broken-metadata", ctrl.ListBrokenMetadata)
ops.POST("delete-broken-metadata", ctrl.DeleteBrokenMetadata)
ops.POST("list-not-uploaded", ctrl.ListNotUploaded)
}
return router, nil
}
func (ctrl *Controller) Health(ctx *gin.Context) {
ctx.JSON(http.StatusOK, gin.H{
"healthz": "ok",
})
}