chore: get resource blob optional (#991)

This commit is contained in:
boojack 2023-01-25 16:11:02 +08:00 committed by GitHub
parent dd5a23e36e
commit 6c3ff6de63
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 17 deletions

View file

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

View file

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

View file

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

View file

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