Skip to content

armgit / memos

Public Imported from GitHub
0

Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.

chore: return raw text for html (#782)

boojackb704c2080934
diff --git a/server/resource.go b/server/resource.go
index 9021b044b..589561a2d 100644
--- a/server/resource.go
+++ b/server/resource.go
@@ -43,10 +43,6 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
}
filename := file.Filename
- if strings.HasSuffix(filename, ".html") {
- return echo.NewHTTPError(http.StatusBadRequest, "html file is not allowed")
- }
-
filetype := file.Header.Get("Content-Type")
size := file.Size
src, err := file.Open()
@@ -169,7 +165,7 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
return nil
})
- g.DELETE("/resource/:resourceId", func(c echo.Context) error {
+ g.PATCH("/resource/:resourceId", func(c echo.Context) error {
ctx := c.Request().Context()
userID, ok := c.Get(getUserIDContextKey()).(int)
if !ok {
@@ -181,31 +177,36 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("resourceId"))).SetInternal(err)
}
- resource, err := s.Store.FindResource(ctx, &api.ResourceFind{
+ resourceFind := &api.ResourceFind{
ID: &resourceID,
CreatorID: &userID,
- })
- if err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find resource").SetInternal(err)
}
- if resource == nil {
- return echo.NewHTTPError(http.StatusNotFound, "Not find resource").SetInternal(err)
+ if _, err := s.Store.FindResource(ctx, resourceFind); err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find resource").SetInternal(err)
}
- resourceDelete := &api.ResourceDelete{
- ID: resourceID,
+ currentTs := time.Now().Unix()
+ resourcePatch := &api.ResourcePatch{
+ ID: resourceID,
+ UpdatedTs: &currentTs,
}
- if err := s.Store.DeleteResource(ctx, resourceDelete); err != nil {
- if common.ErrorCode(err) == common.NotFound {
- return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Resource ID not found: %d", resourceID))
- }
- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to delete resource").SetInternal(err)
+ if err := json.NewDecoder(c.Request().Body).Decode(resourcePatch); err != nil {
+ return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch resource request").SetInternal(err)
}
- return c.JSON(http.StatusOK, true)
+ resource, err := s.Store.PatchResource(ctx, resourcePatch)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to patch resource").SetInternal(err)
+ }
+
+ c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
+ if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(resource)); err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode resource response").SetInternal(err)
+ }
+ return nil
})
- g.PATCH("/resource/:resourceId", func(c echo.Context) error {
+ g.DELETE("/resource/:resourceId", func(c echo.Context) error {
ctx := c.Request().Context()
userID, ok := c.Get(getUserIDContextKey()).(int)
if !ok {
@@ -217,33 +218,28 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("resourceId"))).SetInternal(err)
}
- resourceFind := &api.ResourceFind{
+ resource, err := s.Store.FindResource(ctx, &api.ResourceFind{
ID: &resourceID,
CreatorID: &userID,
- }
- if _, err := s.Store.FindResource(ctx, resourceFind); err != nil {
+ })
+ if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find resource").SetInternal(err)
}
-
- currentTs := time.Now().Unix()
- resourcePatch := &api.ResourcePatch{
- ID: resourceID,
- UpdatedTs: &currentTs,
- }
- if err := json.NewDecoder(c.Request().Body).Decode(resourcePatch); err != nil {
- return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch resource request").SetInternal(err)
+ if resource == nil {
+ return echo.NewHTTPError(http.StatusNotFound, "Not find resource").SetInternal(err)
}
- resource, err := s.Store.PatchResource(ctx, resourcePatch)
- if err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to patch resource").SetInternal(err)
+ resourceDelete := &api.ResourceDelete{
+ ID: resourceID,
}
-
- c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
- if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(resource)); err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode resource response").SetInternal(err)
+ if err := s.Store.DeleteResource(ctx, resourceDelete); err != nil {
+ if common.ErrorCode(err) == common.NotFound {
+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Resource ID not found: %d", resourceID))
+ }
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to delete resource").SetInternal(err)
}
- return nil
+
+ return c.JSON(http.StatusOK, true)
})
}
@@ -267,8 +263,12 @@ func (s *Server) registerResourcePublicRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch resource ID: %v", resourceID)).SetInternal(err)
}
+ if strings.HasPrefix(resource.Type, echo.MIMETextHTML) {
+ c.Response().Writer.Header().Set("Content-Type", echo.MIMETextPlain)
+ } else {
+ c.Response().Writer.Header().Set("Content-Type", resource.Type)
+ }
c.Response().Writer.WriteHeader(http.StatusOK)
- c.Response().Writer.Header().Set("Content-Type", resource.Type)
c.Response().Writer.Header().Set(echo.HeaderCacheControl, "max-age=31536000, immutable")
if _, err := c.Response().Writer.Write(resource.Blob); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to write response").SetInternal(err)
diff --git a/store/db/db.go b/store/db/db.go
index 370a863b4..71b72383c 100644
--- a/store/db/db.go
+++ b/store/db/db.go
@@ -43,11 +43,10 @@ func (db *DB) Open(ctx context.Context) (err error) {
}
// Connect to the database without foreign_key.
- sqliteDB, err := sql.Open("sqlite3", db.profile.DSN+"?_foreign_keys=0")
+ sqliteDB, err := sql.Open("sqlite3", db.profile.DSN+"?cache=shared&_foreign_keys=0&_busy_timeout=9999999")
if err != nil {
return fmt.Errorf("failed to open db with dsn: %s, err: %w", db.profile.DSN, err)
}
- sqliteDB.SetMaxOpenConns(1)
db.Db = sqliteDB
if db.profile.Mode == "dev" {