77 "net/http"
88
99 "github.com/go-chi/render"
10- revactx "github.com/opencloud-eu/reva/v2/pkg/ctx"
1110 "github.com/opencloud-eu/reva/v2/pkg/storage/utils/metadata"
1211
1312 "github.com/opencloud-eu/opencloud/pkg/log"
2120 GetPhoto (ctx context.Context , id string ) ([]byte , error )
2221
2322 // UpdatePhoto retrieves the requested photo
24- UpdatePhoto (ctx context.Context , id string , rc io.Reader ) error
23+ UpdatePhoto (ctx context.Context , id string , r io.Reader ) error
2524
2625 // DeletePhoto deletes the requested photo
2726 DeletePhoto (ctx context.Context , id string ) error
3433
3534 // ErrNoBytes is returned when no bytes are found
3635 ErrNoBytes = errors .New ("no bytes" )
37-
38- // ErrNoUser is returned when no user is found
39- ErrNoUser = errors .New ("no user found" )
4036)
4137
4238// UsersUserProfilePhotoService is the implementation of the UsersUserProfilePhotoProvider interface
@@ -71,8 +67,8 @@ func (s UsersUserProfilePhotoService) DeletePhoto(ctx context.Context, id string
7167}
7268
7369// UpdatePhoto updates the requested photo
74- func (s UsersUserProfilePhotoService ) UpdatePhoto (ctx context.Context , id string , rc io.Reader ) error {
75- photo , err := io .ReadAll (rc )
70+ func (s UsersUserProfilePhotoService ) UpdatePhoto (ctx context.Context , id string , r io.Reader ) error {
71+ photo , err := io .ReadAll (r )
7672 if err != nil {
7773 return err
7874 }
@@ -98,66 +94,61 @@ func NewUsersUserProfilePhotoApi(usersUserProfilePhotoService UsersUserProfilePh
9894 }, nil
9995}
10096
101- // GetProfilePhoto provides the requested photo
102- func (api UsersUserProfilePhotoApi ) GetProfilePhoto (w http.ResponseWriter , r * http.Request ) {
103- id , ok := api .getUserID (w , r )
104- if ! ok {
105- return
106- }
107-
108- photo , err := api .usersUserProfilePhotoService .GetPhoto (r .Context (), id )
109- if err != nil {
110- api .logger .Debug ().Err (err )
111- errorcode .GeneralException .Render (w , r , http .StatusNotFound , "failed to get photo" )
112- return
97+ // GetProfilePhoto creates a handler which renders the corresponding photo
98+ func (api UsersUserProfilePhotoApi ) GetProfilePhoto (h HTTPDataHandler [string ]) http.HandlerFunc {
99+ return func (w http.ResponseWriter , r * http.Request ) {
100+ v , ok := h (w , r )
101+ if ! ok {
102+ return
103+ }
104+
105+ photo , err := api .usersUserProfilePhotoService .GetPhoto (r .Context (), v )
106+ if err != nil {
107+ api .logger .Debug ().Err (err )
108+ errorcode .GeneralException .Render (w , r , http .StatusNotFound , "failed to get photo" )
109+ return
110+ }
111+
112+ render .Status (r , http .StatusOK )
113+ _ , _ = w .Write (photo )
113114 }
114-
115- render .Status (r , http .StatusOK )
116- _ , _ = w .Write (photo )
117115}
118116
119- // UpsertProfilePhoto updates or inserts (initial create) the requested photo
120- func (api UsersUserProfilePhotoApi ) UpsertProfilePhoto (w http.ResponseWriter , r * http.Request ) {
121- id , ok := api .getUserID (w , r )
122- if ! ok {
123- return
124- }
125-
126- if err := api .usersUserProfilePhotoService .UpdatePhoto (r .Context (), id , r .Body ); err != nil {
127- api .logger .Debug ().Err (err )
128- errorcode .GeneralException .Render (w , r , http .StatusNotFound , "failed to update photo" )
129- return
117+ // UpsertProfilePhoto creates a handler which updates or creates the corresponding photo
118+ func (api UsersUserProfilePhotoApi ) UpsertProfilePhoto (h HTTPDataHandler [string ]) http.HandlerFunc {
119+ return func (w http.ResponseWriter , r * http.Request ) {
120+ v , ok := h (w , r )
121+ if ! ok {
122+ return
123+ }
124+
125+ if err := api .usersUserProfilePhotoService .UpdatePhoto (r .Context (), v , r .Body ); err != nil {
126+ api .logger .Debug ().Err (err )
127+ errorcode .GeneralException .Render (w , r , http .StatusInternalServerError , "failed to update photo" )
128+ return
129+ }
130+ defer func () {
131+ _ = r .Body .Close ()
132+ }()
133+
134+ render .Status (r , http .StatusOK )
130135 }
131- defer func () {
132- _ = r .Body .Close ()
133- }()
134-
135- render .Status (r , http .StatusOK )
136136}
137137
138- // DeleteProfilePhoto deletes the requested photo
139- func (api UsersUserProfilePhotoApi ) DeleteProfilePhoto (w http.ResponseWriter , r * http.Request ) {
140- id , ok := api .getUserID (w , r )
141- if ! ok {
142- return
138+ // DeleteProfilePhoto creates a handler which deletes the corresponding photo
139+ func (api UsersUserProfilePhotoApi ) DeleteProfilePhoto (h HTTPDataHandler [string ]) http.HandlerFunc {
140+ return func (w http.ResponseWriter , r * http.Request ) {
141+ v , ok := h (w , r )
142+ if ! ok {
143+ return
144+ }
145+
146+ if err := api .usersUserProfilePhotoService .DeletePhoto (r .Context (), v ); err != nil {
147+ api .logger .Debug ().Err (err )
148+ errorcode .GeneralException .Render (w , r , http .StatusInternalServerError , "failed to delete photo" )
149+ return
150+ }
151+
152+ render .Status (r , http .StatusOK )
143153 }
144-
145- if err := api .usersUserProfilePhotoService .DeletePhoto (r .Context (), id ); err != nil {
146- api .logger .Debug ().Err (err )
147- errorcode .GeneralException .Render (w , r , http .StatusNotFound , "failed to delete photo" )
148- return
149- }
150-
151- render .Status (r , http .StatusOK )
152- }
153-
154- func (api UsersUserProfilePhotoApi ) getUserID (w http.ResponseWriter , r * http.Request ) (string , bool ) {
155- u , ok := revactx .ContextGetUser (r .Context ())
156- if ! ok {
157- api .logger .Debug ().Msg (ErrNoUser .Error ())
158- errorcode .GeneralException .Render (w , r , http .StatusMethodNotAllowed , ErrNoUser .Error ())
159- return "" , false
160- }
161-
162- return u .GetId ().GetOpaqueId (), true
163154}
0 commit comments