-
Notifications
You must be signed in to change notification settings - Fork 112
/
spaces.go
179 lines (159 loc) · 5.33 KB
/
spaces.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright 2018-2021 CERN
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
package ocdav
import (
"net/http"
"path"
"github.com/cs3org/reva/v2/internal/http/services/owncloud/ocdav/config"
"github.com/cs3org/reva/v2/internal/http/services/owncloud/ocdav/errors"
"github.com/cs3org/reva/v2/internal/http/services/owncloud/ocdav/net"
"github.com/cs3org/reva/v2/internal/http/services/owncloud/ocdav/propfind"
"github.com/cs3org/reva/v2/pkg/appctx"
"github.com/cs3org/reva/v2/pkg/rhttp/router"
"github.com/cs3org/reva/v2/pkg/storagespace"
"github.com/cs3org/reva/v2/pkg/utils"
)
// SpacesHandler handles trashbin requests
type SpacesHandler struct {
gatewaySvc string
namespace string
useLoggedInUserNS bool
}
func (h *SpacesHandler) init(c *config.Config) error {
h.gatewaySvc = c.GatewaySvc
h.namespace = path.Join("/", c.WebdavNamespace)
h.useLoggedInUserNS = true
return nil
}
// Handler handles requests
func (h *SpacesHandler) Handler(s *svc, trashbinHandler *TrashbinHandler) http.Handler {
config := s.Config()
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// ctx := r.Context()
// log := appctx.GetLogger(ctx)
if r.Method == http.MethodOptions {
s.handleOptions(w, r)
return
}
var segment string
segment, r.URL.Path = router.ShiftPath(r.URL.Path)
if segment == "" {
// listing is disabled, no auth will change that
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
if segment == _trashbinPath {
h.handleSpacesTrashbin(w, r, s, trashbinHandler)
return
}
spaceID := segment
// TODO initialize status with http.StatusBadRequest
// TODO initialize err with errors.ErrUnsupportedMethod
var status int // status 0 means the handler already sent the response
var err error
switch r.Method {
case MethodPropfind:
p := propfind.NewHandler(config.PublicURL, s.gatewaySelector, config)
p.HandleSpacesPropfind(w, r, spaceID)
case MethodProppatch:
status, err = s.handleSpacesProppatch(w, r, spaceID)
case MethodLock:
status, err = s.handleSpacesLock(w, r, spaceID)
case MethodUnlock:
status, err = s.handleSpaceUnlock(w, r, spaceID)
case MethodMkcol:
status, err = s.handleSpacesMkCol(w, r, spaceID)
case MethodMove:
s.handleSpacesMove(w, r, spaceID)
case MethodCopy:
s.handleSpacesCopy(w, r, spaceID)
case MethodReport:
s.handleReport(w, r, spaceID)
case http.MethodGet:
s.handleSpacesGet(w, r, spaceID)
case http.MethodPut:
s.handleSpacesPut(w, r, spaceID)
case http.MethodPost:
s.handleSpacesTusPost(w, r, spaceID)
case http.MethodOptions:
s.handleOptions(w, r)
case http.MethodHead:
s.handleSpacesHead(w, r, spaceID)
case http.MethodDelete:
status, err = s.handleSpacesDelete(w, r, spaceID)
default:
http.Error(w, http.StatusText(http.StatusNotImplemented), http.StatusNotImplemented)
}
if status != 0 { // 0 means the handler already sent the response
w.WriteHeader(status)
if status != http.StatusNoContent {
var b []byte
if b, err = errors.Marshal(status, err.Error(), ""); err == nil {
_, err = w.Write(b)
}
}
}
if err != nil {
appctx.GetLogger(r.Context()).Error().Err(err).Msg(err.Error())
}
})
}
func (h *SpacesHandler) handleSpacesTrashbin(w http.ResponseWriter, r *http.Request, s *svc, trashbinHandler *TrashbinHandler) {
ctx := r.Context()
log := appctx.GetLogger(ctx)
var spaceID string
spaceID, r.URL.Path = router.ShiftPath(r.URL.Path)
if spaceID == "" {
// listing is disabled, no auth will change that
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
ref, err := storagespace.ParseReference(spaceID)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
var key string
key, r.URL.Path = router.ShiftPath(r.URL.Path)
switch r.Method {
case MethodPropfind:
trashbinHandler.listTrashbin(w, r, s, &ref, path.Join(_trashbinPath, spaceID), key, r.URL.Path)
case MethodMove:
if key == "" {
http.Error(w, "501 Not implemented", http.StatusNotImplemented)
break
}
// find path in url relative to trash base
baseURI := ctx.Value(net.CtxKeyBaseURI).(string)
baseURI = path.Join(baseURI, spaceID)
dh := r.Header.Get(net.HeaderDestination)
dst, err := net.ParseDestination(baseURI, dh)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
log.Debug().Str("key", key).Str("path", r.URL.Path).Str("dst", dst).Msg("spaces restore")
dstRef := ref
dstRef.Path = utils.MakeRelativePath(dst)
trashbinHandler.restore(w, r, s, &ref, &dstRef, key, r.URL.Path)
case http.MethodDelete:
trashbinHandler.delete(w, r, s, &ref, key, r.URL.Path)
default:
http.Error(w, "501 Not implemented", http.StatusNotImplemented)
}
}