forked from cozy/cozy-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
open.go
124 lines (111 loc) · 3.77 KB
/
open.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
package note
import (
"github.com/cozy/cozy-stack/client/request"
"github.com/cozy/cozy-stack/model/instance"
"github.com/cozy/cozy-stack/model/sharing"
"github.com/cozy/cozy-stack/pkg/consts"
"github.com/cozy/cozy-stack/pkg/couchdb"
"github.com/cozy/cozy-stack/pkg/jsonapi"
)
type apiNoteURL struct {
DocID string `json:"_id,omitempty"`
NoteID string `json:"note_id"`
Protocol string `json:"protocol"`
Subdomain string `json:"subdomain"`
Instance string `json:"instance"`
Sharecode string `json:"sharecode,omitempty"`
PublicName string `json:"public_name,omitempty"`
}
func (n *apiNoteURL) ID() string { return n.DocID }
func (n *apiNoteURL) Rev() string { return "" }
func (n *apiNoteURL) DocType() string { return consts.NotesURL }
func (n *apiNoteURL) Clone() couchdb.Doc { cloned := *n; return &cloned }
func (n *apiNoteURL) SetID(id string) { n.DocID = id }
func (n *apiNoteURL) SetRev(rev string) {}
func (n *apiNoteURL) Relationships() jsonapi.RelationshipMap { return nil }
func (n *apiNoteURL) Included() []jsonapi.Object { return nil }
func (n *apiNoteURL) Links() *jsonapi.LinksList { return nil }
func (n *apiNoteURL) Fetch(field string) []string { return nil }
// Opener can be used to find the parameters for creating the URL where the
// note can be opened.
type Opener struct {
*sharing.FileOpener
}
// Open will return an Opener for the given file.
func Open(inst *instance.Instance, fileID string) (*Opener, error) {
file, err := inst.VFS().FileByID(fileID)
if err != nil {
return nil, err
}
// Check that the file is a note
if _, err := fromMetadata(file); err != nil {
return nil, err
}
opener, err := sharing.NewFileOpener(inst, file)
if err != nil {
return nil, err
}
return &Opener{opener}, nil
}
// GetResult looks if the note can be opened locally or not, which code can be
// used in case of a shared note, and other parameters.. and returns the information.
func (o *Opener) GetResult(memberIndex int, readOnly bool) (jsonapi.Object, error) {
var result *apiNoteURL
var err error
if o.ShouldOpenLocally() {
result, err = o.openLocalNote(memberIndex, readOnly)
} else {
result, err = o.openSharedNote()
}
if err != nil {
return nil, err
}
// Enforce DocID and PublicName with local values
result.DocID = o.File.ID()
if name, err := o.Inst.PublicName(); err == nil {
result.PublicName = name
}
return result, nil
}
func (o *Opener) openLocalNote(memberIndex int, readOnly bool) (*apiNoteURL, error) {
// If the note came from another cozy via a sharing that is now revoked, we
// may need to recreate the trigger.
_ = setupTrigger(o.Inst, o.File.ID())
code, err := o.GetSharecode(memberIndex, readOnly)
if err != nil {
return nil, err
}
params := o.OpenLocalFile(code)
doc := apiNoteURL{
NoteID: params.FileID,
Protocol: params.Protocol,
Subdomain: params.Subdomain,
Instance: params.Instance,
Sharecode: params.Sharecode,
}
return &doc, nil
}
func (o *Opener) openSharedNote() (*apiNoteURL, error) {
prepared, err := o.PrepareRequestForSharedFile()
if err != nil {
return nil, err
}
if prepared.Opts == nil {
return o.openLocalNote(prepared.MemberIndex, prepared.ReadOnly)
}
prepared.Opts.Path = "/notes/" + prepared.XoredID + "/open"
res, err := request.Req(prepared.Opts)
if res != nil && res.StatusCode/100 == 4 {
res, err = sharing.RefreshToken(o.Inst, err, o.Sharing, prepared.Creator,
prepared.Creds, prepared.Opts, nil)
}
if err != nil {
return nil, sharing.ErrInternalServerError
}
defer res.Body.Close()
var doc apiNoteURL
if _, err := jsonapi.Bind(res.Body, &doc); err != nil {
return nil, err
}
return &doc, nil
}