-
Notifications
You must be signed in to change notification settings - Fork 141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a route for uploading avatars #4499
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package vfsafero | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
"os" | ||
|
||
"github.com/cozy/cozy-stack/model/vfs" | ||
"github.com/spf13/afero" | ||
) | ||
|
||
// NewAvatarFs creates a new avatar filesystem base on a afero.Fs. | ||
func NewAvatarFs(fs afero.Fs) vfs.Avatarer { | ||
return &avatar{fs} | ||
} | ||
|
||
type avatar struct { | ||
fs afero.Fs | ||
} | ||
|
||
type avatarUpload struct { | ||
afero.File | ||
fs afero.Fs | ||
tmpname string | ||
} | ||
|
||
func (u *avatarUpload) Close() error { | ||
if err := u.File.Close(); err != nil { | ||
_ = u.fs.Remove(u.tmpname) | ||
return err | ||
} | ||
return u.fs.Rename(u.tmpname, "avatar") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: a constant storing the avatar filename would be nice |
||
} | ||
|
||
func (a *avatar) CreateAvatar(contentType string) (io.WriteCloser, error) { | ||
f, err := afero.TempFile(a.fs, "/", "avatar") | ||
if err != nil { | ||
return nil, err | ||
} | ||
tmpname := f.Name() | ||
u := &avatarUpload{ | ||
File: f, | ||
fs: a.fs, | ||
tmpname: tmpname, | ||
} | ||
return u, nil | ||
} | ||
|
||
func (a *avatar) AvatarExists() (bool, error) { | ||
infos, err := a.fs.Stat("avatar") | ||
if os.IsNotExist(err) { | ||
return false, nil | ||
} | ||
if err != nil { | ||
return false, err | ||
} | ||
return infos.Size() > 0, nil | ||
} | ||
|
||
func (a *avatar) ServeAvatarContent(w http.ResponseWriter, req *http.Request) error { | ||
s, err := a.fs.Stat("avatar") | ||
if err != nil { | ||
return err | ||
} | ||
if s.Size() == 0 { | ||
return os.ErrInvalid | ||
} | ||
f, err := a.fs.Open("avatar") | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
http.ServeContent(w, req, "avatar", s.ModTime(), f) | ||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package vfsswift | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/cozy/cozy-stack/model/vfs" | ||
"github.com/cozy/cozy-stack/pkg/prefixer" | ||
"github.com/ncw/swift/v2" | ||
) | ||
|
||
// NewAvatarFsV3 creates a new avatar filesystem base on swift. | ||
// | ||
// This version stores the avatar in the same container as the main data | ||
// container. | ||
func NewAvatarFsV3(c *swift.Connection, db prefixer.Prefixer) vfs.Avatarer { | ||
return &avatarV3{ | ||
c: c, | ||
container: swiftV3ContainerPrefix + db.DBPrefix(), | ||
ctx: context.Background(), | ||
} | ||
} | ||
|
||
type avatarV3 struct { | ||
c *swift.Connection | ||
container string | ||
ctx context.Context | ||
} | ||
|
||
func (a *avatarV3) CreateAvatar(contentType string) (io.WriteCloser, error) { | ||
return a.c.ObjectCreate(a.ctx, a.container, "avatar", true, "", contentType, nil) | ||
} | ||
|
||
func (a *avatarV3) ServeAvatarContent(w http.ResponseWriter, req *http.Request) error { | ||
f, o, err := a.c.ObjectOpen(a.ctx, a.container, "avatar", false, nil) | ||
if err != nil { | ||
return wrapSwiftErr(err) | ||
} | ||
defer f.Close() | ||
|
||
w.Header().Set("Etag", fmt.Sprintf(`"%s"`, o["Etag"])) | ||
w.Header().Set("Content-Type", o["Content-Type"]) | ||
http.ServeContent(w, req, "avatar", unixEpochZero, &backgroundSeeker{f}) | ||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ import ( | |
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
|
@@ -253,6 +254,31 @@ func isMovedError(err error) bool { | |
return ok && j.Code == "moved" | ||
} | ||
|
||
func (h *HTTPHandler) UploadAvatar(c echo.Context) error { | ||
inst := middlewares.GetInstance(c) | ||
if err := middlewares.AllowWholeType(c, http.MethodPut, consts.Settings); err != nil { | ||
return err | ||
} | ||
header := c.Request().Header | ||
size := c.Request().ContentLength | ||
if size > 20_000_000 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could have a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe this should be an error returned by |
||
return jsonapi.BadRequest(errors.New("Avatar is too big")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should return a 413 |
||
} | ||
contentType := header.Get(echo.HeaderContentType) | ||
f, err := inst.AvatarFS().CreateAvatar(contentType) | ||
if err != nil { | ||
return jsonapi.InternalServerError(err) | ||
} | ||
_, err = io.Copy(f, c.Request().Body) | ||
if cerr := f.Close(); cerr != nil && err == nil { | ||
err = cerr | ||
} | ||
if err != nil { | ||
return jsonapi.InternalServerError(err) | ||
} | ||
return c.NoContent(http.StatusNoContent) | ||
} | ||
|
||
// Register all the `/settings` routes to the given router. | ||
func (h *HTTPHandler) Register(router *echo.Group) { | ||
router.GET("/disk-usage", h.diskUsage) | ||
|
@@ -281,6 +307,8 @@ func (h *HTTPHandler) Register(router *echo.Group) { | |
router.PUT("/instance/sign_tos", h.updateInstanceTOS) | ||
router.DELETE("/instance/moved_from", h.clearMovedFrom) | ||
|
||
router.PUT("/avatar", h.UploadAvatar) | ||
|
||
router.GET("/flags", h.getFlags) | ||
|
||
router.GET("/sessions", h.getSessions) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
avatarFs
seems more appropriate since this struct does not old data about avatars themselves but rather implements the interface allowing to manage them.