chore: use resource name in frontend

This commit is contained in:
Steven
2024-01-21 10:55:49 +08:00
parent 582cc6609c
commit 4d48f50815
10 changed files with 168 additions and 147 deletions

View File

@@ -43,26 +43,22 @@ func NewService(profile *profile.Profile, store *store.Store) *Service {
}
func (s *Service) RegisterResourcePublicRoutes(g *echo.Group) {
g.GET("/r/:resourceId", s.streamResource)
g.GET("/r/:resourceId/*", s.streamResource)
g.GET("/r/:resourceName", s.streamResource)
g.GET("/r/:resourceName/*", s.streamResource)
}
func (s *Service) streamResource(c echo.Context) error {
ctx := c.Request().Context()
resourceID, err := util.ConvertStringToInt32(c.Param("resourceId"))
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("resourceId"))).SetInternal(err)
}
resourceName := c.Param("resourceName")
resource, err := s.Store.GetResource(ctx, &store.FindResource{
ID: &resourceID,
GetBlob: true,
ResourceName: &resourceName,
GetBlob: true,
})
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find resource by ID: %v", resourceID)).SetInternal(err)
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find resource by id: %s", resourceName)).SetInternal(err)
}
if resource == nil {
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Resource not found: %d", resourceID))
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Resource not found: %s", resourceName))
}
// Check the related memo visibility.
if resource.MemoID != nil {

View File

@@ -27,7 +27,8 @@ import (
)
type Resource struct {
ID int32 `json:"id"`
ID int32 `json:"id"`
Name string `json:"name"`
// Standard fields
CreatorID int32 `json:"creatorId"`
@@ -368,6 +369,7 @@ func replacePathTemplate(path, filename string) string {
func convertResourceFromStore(resource *store.Resource) *Resource {
return &Resource{
ID: resource.ID,
Name: resource.ResourceName,
CreatorID: resource.CreatorID,
CreatedTs: resource.CreatedTs,
UpdatedTs: resource.UpdatedTs,

View File

@@ -138,7 +138,7 @@ func (s *APIV1Service) generateRSSFromMemoList(ctx context.Context, memoList []*
if resource.ExternalLink != "" {
enclosure.Url = resource.ExternalLink
} else {
enclosure.Url = baseURL + "/o/r/" + fmt.Sprintf("%d", resource.ID)
enclosure.Url = baseURL + "/o/r/" + resource.Name
}
enclosure.Length = strconv.Itoa(int(resource.Size))
enclosure.Type = resource.Type

View File

@@ -148,6 +148,7 @@ func (s *APIV2Service) convertResourceFromStore(ctx context.Context, resource *s
return &apiv2pb.Resource{
Id: resource.ID,
Name: resource.ResourceName,
CreateTime: timestamppb.New(time.Unix(resource.CreatedTs, 0)),
Filename: resource.Filename,
ExternalLink: resource.ExternalLink,