chore: get resource blob optional (#991)

pull/993/head
boojack 2 years ago committed by GitHub
parent dd5a23e36e
commit 6c3ff6de63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -40,6 +40,7 @@ type ResourceFind struct {
// Domain specific fields // Domain specific fields
Filename *string `json:"filename"` Filename *string `json:"filename"`
MemoID *int MemoID *int
GetBlob bool
} }
type ResourcePatch struct { type ResourcePatch struct {

@ -153,6 +153,7 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
resourceFind := &api.ResourceFind{ resourceFind := &api.ResourceFind{
ID: &resourceID, ID: &resourceID,
CreatorID: &userID, CreatorID: &userID,
GetBlob: true,
} }
resource, err := s.Store.FindResource(ctx, resourceFind) resource, err := s.Store.FindResource(ctx, resourceFind)
if err != nil { if err != nil {
@ -180,6 +181,7 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
resourceFind := &api.ResourceFind{ resourceFind := &api.ResourceFind{
ID: &resourceID, ID: &resourceID,
CreatorID: &userID, CreatorID: &userID,
GetBlob: true,
} }
resource, err := s.Store.FindResource(ctx, resourceFind) resource, err := s.Store.FindResource(ctx, resourceFind)
if err != nil { if err != nil {
@ -290,6 +292,7 @@ func (s *Server) registerResourcePublicRoutes(g *echo.Group) {
resourceFind := &api.ResourceFind{ resourceFind := &api.ResourceFind{
ID: &resourceID, ID: &resourceID,
Filename: &filename, Filename: &filename,
GetBlob: true,
} }
resource, err := s.Store.FindResource(ctx, resourceFind) resource, err := s.Store.FindResource(ctx, resourceFind)
if err != nil { if err != nil {

@ -98,9 +98,6 @@ func NewServer(ctx context.Context, profile *profile.Profile) (*Server, error) {
rootGroup := e.Group("") rootGroup := e.Group("")
s.registerRSSRoutes(rootGroup) s.registerRSSRoutes(rootGroup)
webhookGroup := e.Group("/h")
s.registerResourcePublicRoutes(webhookGroup)
publicGroup := e.Group("/o") publicGroup := e.Group("/o")
s.registerResourcePublicRoutes(publicGroup) s.registerResourcePublicRoutes(publicGroup)
registerGetterPublicRoutes(publicGroup) registerGetterPublicRoutes(publicGroup)

@ -295,21 +295,18 @@ func findResourceList(ctx context.Context, tx *sql.Tx, find *api.ResourceFind) (
where, args = append(where, "id in (SELECT resource_id FROM memo_resource WHERE memo_id = ?)"), append(args, *v) where, args = append(where, "id in (SELECT resource_id FROM memo_resource WHERE memo_id = ?)"), append(args, *v)
} }
query := ` fields := []string{"id", "filename", "external_link", "type", "size", "creator_id", "created_ts", "updated_ts"}
if find.GetBlob {
fields = append(fields, "blob")
}
query := fmt.Sprintf(`
SELECT SELECT
id, %s
filename,
blob,
external_link,
type,
size,
creator_id,
created_ts,
updated_ts
FROM resource FROM resource
WHERE ` + strings.Join(where, " AND ") + ` WHERE %s
ORDER BY id DESC ORDER BY id DESC
` `, strings.Join(fields, ", "), strings.Join(where, " AND "))
rows, err := tx.QueryContext(ctx, query, args...) rows, err := tx.QueryContext(ctx, query, args...)
if err != nil { if err != nil {
return nil, FormatError(err) return nil, FormatError(err)
@ -319,16 +316,21 @@ func findResourceList(ctx context.Context, tx *sql.Tx, find *api.ResourceFind) (
resourceRawList := make([]*resourceRaw, 0) resourceRawList := make([]*resourceRaw, 0)
for rows.Next() { for rows.Next() {
var resourceRaw resourceRaw var resourceRaw resourceRaw
if err := rows.Scan( dest := []interface{}{
&resourceRaw.ID, &resourceRaw.ID,
&resourceRaw.Filename, &resourceRaw.Filename,
&resourceRaw.Blob,
&resourceRaw.ExternalLink, &resourceRaw.ExternalLink,
&resourceRaw.Type, &resourceRaw.Type,
&resourceRaw.Size, &resourceRaw.Size,
&resourceRaw.CreatorID, &resourceRaw.CreatorID,
&resourceRaw.CreatedTs, &resourceRaw.CreatedTs,
&resourceRaw.UpdatedTs, &resourceRaw.UpdatedTs,
}
if find.GetBlob {
dest = append(dest, &resourceRaw.Blob)
}
if err := rows.Scan(
dest...,
); err != nil { ); err != nil {
return nil, FormatError(err) return nil, FormatError(err)
} }

Loading…
Cancel
Save