-
Notifications
You must be signed in to change notification settings - Fork 137
/
open_office.go
326 lines (301 loc) · 9.98 KB
/
open_office.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package sharing
import (
"bytes"
"net/url"
"github.com/cozy/cozy-stack/client/request"
"github.com/cozy/cozy-stack/model/instance"
"github.com/cozy/cozy-stack/model/office"
"github.com/cozy/cozy-stack/model/settings"
"github.com/cozy/cozy-stack/model/vfs"
"github.com/cozy/cozy-stack/pkg/config/config"
"github.com/cozy/cozy-stack/pkg/consts"
"github.com/cozy/cozy-stack/pkg/couchdb"
"github.com/cozy/cozy-stack/pkg/jsonapi"
jwt "github.com/golang-jwt/jwt/v5"
)
type apiOfficeURL struct {
FileID string `json:"_id,omitempty"`
DocID string `json:"document_id"`
Subdomain string `json:"subdomain"`
Protocol string `json:"protocol"`
Instance string `json:"instance"`
Sharecode string `json:"sharecode,omitempty"`
PublicName string `json:"public_name,omitempty"`
OO *onlyOffice `json:"onlyoffice,omitempty"`
}
type onlyOffice struct {
URL string `json:"url,omitempty"`
Token string `json:"token,omitempty"`
Type string `json:"documentType"`
Doc struct {
Filetype string `json:"filetype,omitempty"`
Key string `json:"key"`
Title string `json:"title,omitempty"`
URL string `json:"url"`
Info struct {
Owner string `json:"owner,omitempty"`
Uploaded string `json:"uploaded,omitempty"`
} `json:"info"`
} `json:"document"`
Editor struct {
Callback string `json:"callbackUrl"`
Lang string `json:"lang,omitempty"`
Mode string `json:"mode"`
Custom struct {
CompactHeader bool `json:"compactHeader"`
Customer struct {
Address string `json:"address"`
Logo string `json:"logo"`
Mail string `json:"mail"`
Name string `json:"name"`
WWW string `json:"www"`
} `json:"customer"`
Feedback bool `json:"feedback"`
ForceSave bool `json:"forcesave"`
GoBack bool `json:"goback"`
} `json:"customization"`
} `json:"editor"`
}
func (o *apiOfficeURL) ID() string { return o.FileID }
func (o *apiOfficeURL) Rev() string { return "" }
func (o *apiOfficeURL) DocType() string { return consts.OfficeURL }
func (o *apiOfficeURL) Clone() couchdb.Doc { cloned := *o; return &cloned }
func (o *apiOfficeURL) SetID(id string) { o.FileID = id }
func (o *apiOfficeURL) SetRev(rev string) {}
func (o *apiOfficeURL) Relationships() jsonapi.RelationshipMap { return nil }
func (o *apiOfficeURL) Included() []jsonapi.Object { return nil }
func (o *apiOfficeURL) Links() *jsonapi.LinksList { return nil }
func (o *apiOfficeURL) Fetch(field string) []string { return nil }
func (o *apiOfficeURL) sign(cfg *config.Office) (string, error) {
if cfg == nil || cfg.InboxSecret == "" {
return "", nil
}
claims := *o.OO
claims.URL = ""
claims.Doc.Filetype = ""
claims.Doc.Title = ""
claims.Doc.Info.Owner = ""
claims.Doc.Info.Uploaded = ""
claims.Editor.Lang = ""
claims.Editor.Custom.Customer.Address = ""
claims.Editor.Custom.Customer.Logo = ""
claims.Editor.Custom.Customer.Mail = ""
claims.Editor.Custom.Customer.Name = ""
claims.Editor.Custom.Customer.WWW = ""
token := jwt.NewWithClaims(jwt.SigningMethodHS256, &claims)
return token.SignedString([]byte(cfg.InboxSecret))
}
func (o *onlyOffice) GetExpirationTime() (*jwt.NumericDate, error) { return nil, nil }
func (o *onlyOffice) GetIssuedAt() (*jwt.NumericDate, error) { return nil, nil }
func (o *onlyOffice) GetNotBefore() (*jwt.NumericDate, error) { return nil, nil }
func (o *onlyOffice) GetIssuer() (string, error) { return "", nil }
func (o *onlyOffice) GetSubject() (string, error) { return "", nil }
func (o *onlyOffice) GetAudience() (jwt.ClaimStrings, error) { return nil, nil }
// OfficeOpener can be used to find the parameters for opening an office document.
type OfficeOpener struct {
*FileOpener
}
// Open will return an OfficeOpener for the given file.
func OpenOffice(inst *instance.Instance, fileID string) (*OfficeOpener, error) {
file, err := inst.VFS().FileByID(fileID)
if err != nil {
return nil, err
}
// Check that the file is an office document
if !isOfficeDocument(file) {
return nil, office.ErrInvalidFile
}
opener, err := NewFileOpener(inst, file)
if err != nil {
return nil, err
}
return &OfficeOpener{opener}, nil
}
// GetResult looks if the file can be opened locally or not, which code can be
// used in case of a shared office document, and other parameters.. and returns
// the information.
func (o *OfficeOpener) GetResult(memberIndex int, readOnly bool) (jsonapi.Object, error) {
var result *apiOfficeURL
var err error
if o.ShouldOpenLocally() {
result, err = o.openLocalDocument(memberIndex, readOnly)
} else {
result, err = o.openSharedDocument()
}
if err != nil {
return nil, err
}
result.FileID = o.File.ID()
return result, nil
}
func (o *OfficeOpener) openLocalDocument(memberIndex int, readOnly bool) (*apiOfficeURL, error) {
cfg := office.GetConfig(o.Inst.ContextName)
if cfg == nil || cfg.OnlyOfficeURL == "" {
return nil, office.ErrNoServer
}
// Create a local result
code, err := o.GetSharecode(memberIndex, readOnly)
if err != nil {
return nil, err
}
params := o.OpenLocalFile(code)
doc := apiOfficeURL{
DocID: params.FileID,
Protocol: params.Protocol,
Subdomain: params.Subdomain,
Instance: params.Instance,
Sharecode: params.Sharecode,
}
// Fill the parameters for the Document Server
mode := "edit"
if readOnly || o.File.Trashed {
mode = "view"
}
download, err := o.downloadURL()
if err != nil {
o.Inst.Logger().WithNamespace("office").
Infof("Cannot build download URL: %s", err)
return nil, ErrInternalServerError
}
key, err := office.GetStore().GetSecretByID(o.Inst, o.File.ID())
if err != nil {
o.Inst.Logger().WithNamespace("office").
Infof("Cannot get secret from store: %s", err)
return nil, ErrInternalServerError
}
if key != "" {
doc, err := office.GetStore().GetDoc(o.Inst, key)
if err != nil {
o.Inst.Logger().WithNamespace("office").
Infof("Cannot get doc from store: %s", err)
return nil, ErrInternalServerError
}
if shouldOpenANewVersion(o.File, doc) {
key = ""
}
}
if key == "" {
detector := office.ConflictDetector{ID: o.File.ID(), Rev: o.File.Rev(), MD5Sum: o.File.MD5Sum}
key, err = office.GetStore().AddDoc(o.Inst, detector)
}
if err != nil {
o.Inst.Logger().WithNamespace("office").
Infof("Cannot add doc to store: %s", err)
return nil, ErrInternalServerError
}
publicName, _ := settings.PublicName(o.Inst)
doc.PublicName = publicName
doc.OO = &onlyOffice{
URL: cfg.OnlyOfficeURL,
Type: documentType(o.File),
}
doc.OO.Doc.Filetype = o.File.Mime
doc.OO.Doc.Key = key
doc.OO.Doc.Title = o.File.DocName
doc.OO.Doc.URL = download
doc.OO.Doc.Info.Owner = publicName
doc.OO.Doc.Info.Uploaded = uploadedDate(o.File)
doc.OO.Editor.Callback = o.Inst.PageURL("/office/callback", nil)
doc.OO.Editor.Lang = o.Inst.Locale
doc.OO.Editor.Mode = mode
doc.OO.Editor.Custom.CompactHeader = true
doc.OO.Editor.Custom.Customer.Address = "\"Le Surena\" Face au 5 Quai Marcel Dassault 92150 Suresnes"
doc.OO.Editor.Custom.Customer.Logo = o.Inst.FromURL(&url.URL{Path: "/assets/icon-192.png"})
doc.OO.Editor.Custom.Customer.Mail = o.Inst.SupportEmailAddress()
doc.OO.Editor.Custom.Customer.Name = "Cozy Cloud"
doc.OO.Editor.Custom.Customer.WWW = "cozy.io"
doc.OO.Editor.Custom.Feedback = false
doc.OO.Editor.Custom.ForceSave = true
doc.OO.Editor.Custom.GoBack = false
token, err := doc.sign(cfg)
if err != nil {
return nil, err
}
doc.OO.Token = token
return &doc, nil
}
func (o *OfficeOpener) openSharedDocument() (*apiOfficeURL, error) {
prepared, err := o.PrepareRequestForSharedFile()
if err != nil {
return nil, err
}
if prepared.Opts == nil {
return o.openLocalDocument(prepared.MemberIndex, prepared.ReadOnly)
}
prepared.Opts.Path = "/office/" + prepared.XoredID + "/open"
res, err := request.Req(prepared.Opts)
if res != nil && res.StatusCode/100 == 4 {
res, err = RefreshToken(o.Inst, err, o.Sharing, prepared.Creator,
prepared.Creds, prepared.Opts, nil)
}
if res != nil && res.StatusCode == 404 {
return o.openLocalDocument(prepared.MemberIndex, prepared.ReadOnly)
}
if err != nil {
o.Inst.Logger().WithNamespace("office").Infof("openSharedDocument error: %s", err)
return nil, ErrInternalServerError
}
defer res.Body.Close()
var doc apiOfficeURL
if _, err := jsonapi.Bind(res.Body, &doc); err != nil {
return nil, err
}
publicName, _ := settings.PublicName(o.Inst)
doc.PublicName = publicName
doc.OO = nil
return &doc, nil
}
// downloadURL returns an URL where the Document Server can download the file.
func (o *OfficeOpener) downloadURL() (string, error) {
path, err := o.File.Path(o.Inst.VFS())
if err != nil {
return "", err
}
secret, err := vfs.GetStore().AddFile(o.Inst, path)
if err != nil {
return "", err
}
return o.Inst.PageURL("/files/downloads/"+secret+"/"+o.File.DocName, nil), nil
}
// uploadedDate returns the uploaded date for a file in the date format used by
// OnlyOffice
func uploadedDate(f *vfs.FileDoc) string {
date := f.CreatedAt
if f.CozyMetadata != nil && f.CozyMetadata.UploadedAt != nil {
date = *f.CozyMetadata.UploadedAt
}
return date.Format("2006-01-02 3:04 PM")
}
// documentType returns the document type parameter for Only Office
// Cf https://api.onlyoffice.com/editors/config/#documentType
func documentType(f *vfs.FileDoc) string {
switch f.Class {
case "spreadsheet":
return "cell"
case "slide":
return "slide"
default:
return "word"
}
}
func isOfficeDocument(f *vfs.FileDoc) bool {
switch f.Class {
case "spreadsheet", "slide", "text":
return true
default:
return false
}
}
func shouldOpenANewVersion(file *vfs.FileDoc, detector *office.ConflictDetector) bool {
if detector == nil {
return true
}
cm := file.CozyMetadata
if cm != nil && cm.UploadedBy != nil && cm.UploadedBy.Slug == office.OOSlug {
return false
}
if bytes.Equal(file.MD5Sum, detector.MD5Sum) {
return false
}
return true
}