This repository has been archived by the owner on Feb 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
wopilocking.go
182 lines (154 loc) · 6.33 KB
/
wopilocking.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
180
181
182
package app
import (
"net/http"
"time"
rpcv1beta1 "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
providerv1beta1 "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
typesv1beta1 "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"
)
const (
// WOPI Locks generally have a lock duration of 30 minutes and will be refreshed before expiration if needed
// https://docs.microsoft.com/en-us/microsoft-365/cloud-storage-partner-program/rest/concepts#lock
lockDuration time.Duration = 30 * time.Minute
)
// GetLock returns a lock or an empty string if no lock exists
// https://docs.microsoft.com/en-us/microsoft-365/cloud-storage-partner-program/rest/files/getlock
func GetLock(app *demoApp, w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
wopiContext, _ := WopiContextFromCtx(ctx)
req := &providerv1beta1.GetLockRequest{
Ref: &wopiContext.FileReference,
}
resp, err := app.gwc.GetLock(
ctx,
req,
)
if err != nil {
app.Logger.Error().Err(err).Str("FileReference", wopiContext.FileReference.String()).Msg("GetLock failed")
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
if resp.Status.Code != rpcv1beta1.Code_CODE_OK {
app.Logger.Error().Str("status_code", resp.Status.Code.String()).Str("status_msg", resp.Status.Message).Str("FileReference", wopiContext.FileReference.String()).Msg("GetLock failed")
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
lockID := ""
if resp.Lock != nil {
lockID = resp.Lock.LockId
}
w.Header().Set(HeaderWopiLock, lockID)
http.Error(w, http.StatusText(http.StatusOK), http.StatusOK)
}
// Lock returns a WOPI lock or performs an unlock and relock
// https://docs.microsoft.com/en-us/microsoft-365/cloud-storage-partner-program/rest/files/lock
// https://docs.microsoft.com/en-us/microsoft-365/cloud-storage-partner-program/rest/files/unlockandrelock
func Lock(app *demoApp, w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
wopiContext, _ := WopiContextFromCtx(ctx)
// TODO: handle un- and relock
lockID := r.Header.Get(HeaderWopiLock)
if lockID == "" {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
req := &providerv1beta1.SetLockRequest{
Ref: &wopiContext.FileReference,
Lock: &providerv1beta1.Lock{
LockId: lockID,
AppName: app.Config.AppLockName,
Type: providerv1beta1.LockType_LOCK_TYPE_WRITE,
Expiration: &typesv1beta1.Timestamp{
Seconds: uint64(time.Now().Add(lockDuration).Unix()),
},
},
}
app.Logger.Debug().Str("lock_id", lockID).Str("FileReference", wopiContext.FileReference.String()).Msg("Performing SetLock")
resp, err := app.gwc.SetLock(
ctx,
req,
)
if err != nil {
app.Logger.Error().Err(err).Str("lock_id", lockID).Str("FileReference", wopiContext.FileReference.String()).Msg("SetLock failed")
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
switch resp.Status.Code {
case rpcv1beta1.Code_CODE_OK:
http.Error(w, http.StatusText(http.StatusOK), http.StatusOK)
return
case rpcv1beta1.Code_CODE_FAILED_PRECONDITION:
// already locked
req := &providerv1beta1.GetLockRequest{
Ref: &wopiContext.FileReference,
}
resp, err := app.gwc.GetLock(
ctx,
req,
)
if err != nil {
app.Logger.Error().Err(err).Str("lock_id", lockID).Str("FileReference", wopiContext.FileReference.String()).Msg("SetLock failed, fallback to GetLock failed too")
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
if resp.Status.Code != rpcv1beta1.Code_CODE_OK {
app.Logger.Error().Str("status_code", resp.Status.Code.String()).Str("status_msg", resp.Status.Message).Str("lock_id", lockID).Str("FileReference", wopiContext.FileReference.String()).Msg("SetLock failed, fallback to GetLock failed too")
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
if resp.Lock != nil {
if resp.Lock.LockId != lockID {
w.Header().Set(HeaderWopiLock, resp.Lock.LockId)
http.Error(w, http.StatusText(http.StatusConflict), http.StatusConflict)
return
}
// TODO: according to the spec we need to treat this as a RefreshLock
http.Error(w, http.StatusText(http.StatusOK), http.StatusOK)
return
}
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
default:
app.Logger.Error().Str("status_code", resp.Status.Code.String()).Str("status_msg", resp.Status.Message).Str("lock_id", lockID).Str("FileReference", wopiContext.FileReference.String()).Msg("SetLock failed")
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
}
// RefreshLock refreshes a provided lock for 30 minutes
// https://docs.microsoft.com/en-us/microsoft-365/cloud-storage-partner-program/rest/files/refreshlock
func RefreshLock(app *demoApp, w http.ResponseWriter, r *http.Request) {
// TODO: implement
http.Error(w, http.StatusText(http.StatusNotImplemented), http.StatusNotImplemented)
}
// UnLock removes a given lock from a file
// https://docs.microsoft.com/en-us/microsoft-365/cloud-storage-partner-program/rest/files/unlock
func UnLock(app *demoApp, w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
wopiContext, _ := WopiContextFromCtx(ctx)
lockID := r.Header.Get(HeaderWopiLock)
if lockID == "" {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
req := &providerv1beta1.UnlockRequest{
Ref: &wopiContext.FileReference,
Lock: &providerv1beta1.Lock{
LockId: lockID,
AppName: app.Config.AppLockName,
},
}
resp, err := app.gwc.Unlock(
ctx,
req,
)
if err != nil {
app.Logger.Error().Err(err).Str("lock_id", lockID).Str("FileReference", wopiContext.FileReference.String()).Msg("UnLock failed")
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
if resp.Status.Code != rpcv1beta1.Code_CODE_OK {
app.Logger.Error().Str("status_code", resp.Status.Code.String()).Str("status_msg", resp.Status.Message).Str("lock_id", lockID).Str("FileReference", wopiContext.FileReference.String()).Msg("UnLock failed")
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
http.Error(w, http.StatusText(http.StatusOK), http.StatusOK)
}