|
| 1 | +package sharing |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/cozy/cozy-stack/model/instance" |
| 5 | + "github.com/cozy/cozy-stack/model/settings" |
| 6 | + "github.com/cozy/cozy-stack/pkg/consts" |
| 7 | + "github.com/cozy/cozy-stack/pkg/couchdb" |
| 8 | + "github.com/cozy/cozy-stack/pkg/jsonapi" |
| 9 | +) |
| 10 | + |
| 11 | +// apiEditorURL holds the parameters to build the URL where a file can be |
| 12 | +// opened by an editor. It uses the io.cozy.files doctype, as no dedicated |
| 13 | +// doctype is needed for editors that only need to open a regular file. |
| 14 | +type apiEditorURL struct { |
| 15 | + DocID string `json:"_id,omitempty"` |
| 16 | + FileID string `json:"file_id"` |
| 17 | + Protocol string `json:"protocol"` |
| 18 | + Subdomain string `json:"subdomain"` |
| 19 | + Instance string `json:"instance"` |
| 20 | + Sharecode string `json:"sharecode,omitempty"` |
| 21 | + PublicName string `json:"public_name,omitempty"` |
| 22 | +} |
| 23 | + |
| 24 | +func (e *apiEditorURL) ID() string { return e.DocID } |
| 25 | +func (e *apiEditorURL) Rev() string { return "" } |
| 26 | +func (e *apiEditorURL) DocType() string { return consts.Files } |
| 27 | +func (e *apiEditorURL) Clone() couchdb.Doc { cloned := *e; return &cloned } |
| 28 | +func (e *apiEditorURL) SetID(id string) { e.DocID = id } |
| 29 | +func (e *apiEditorURL) SetRev(rev string) {} |
| 30 | +func (e *apiEditorURL) Relationships() jsonapi.RelationshipMap { return nil } |
| 31 | +func (e *apiEditorURL) Included() []jsonapi.Object { return nil } |
| 32 | +func (e *apiEditorURL) Links() *jsonapi.LinksList { return nil } |
| 33 | +func (e *apiEditorURL) Fetch(field string) []string { return nil } |
| 34 | + |
| 35 | +// EditorOpener can be used to find the parameters for creating the URL where a |
| 36 | +// file can be opened by an editor. |
| 37 | +type EditorOpener struct { |
| 38 | + *FileOpener |
| 39 | +} |
| 40 | + |
| 41 | +// OpenEditor returns an EditorOpener for the given file. |
| 42 | +func OpenEditor(inst *instance.Instance, fileID string) (*EditorOpener, error) { |
| 43 | + file, err := inst.VFS().FileByID(fileID) |
| 44 | + if err != nil { |
| 45 | + return nil, err |
| 46 | + } |
| 47 | + |
| 48 | + opener, err := NewFileOpener(inst, file) |
| 49 | + if err != nil { |
| 50 | + return nil, err |
| 51 | + } |
| 52 | + return &EditorOpener{opener}, nil |
| 53 | +} |
| 54 | + |
| 55 | +// GetResult looks if the file can be opened locally or not, which code can be |
| 56 | +// used in case of a shared file, and other parameters, and returns the |
| 57 | +// information. |
| 58 | +func (o *EditorOpener) GetResult(memberIndex int, readOnly bool) (jsonapi.Object, error) { |
| 59 | + prepared, err := o.PrepareOpenFileRequest(memberIndex, readOnly) |
| 60 | + if err != nil { |
| 61 | + return nil, err |
| 62 | + } |
| 63 | + var result *apiEditorURL |
| 64 | + if prepared.Opts == nil { |
| 65 | + result, err = o.openLocalFile(prepared.MemberIndex, prepared.ReadOnly) |
| 66 | + } else { |
| 67 | + result, err = o.openSharedFile(prepared) |
| 68 | + } |
| 69 | + if err != nil { |
| 70 | + return nil, err |
| 71 | + } |
| 72 | + |
| 73 | + // Keep JSON:API data.id local to the caller. FileID remains the file id on |
| 74 | + // the instance returned in Instance, which can differ for cozy-to-cozy shares. |
| 75 | + result.DocID = o.File.ID() |
| 76 | + if name, err := settings.PublicName(o.Inst); err == nil { |
| 77 | + result.PublicName = name |
| 78 | + } |
| 79 | + return result, nil |
| 80 | +} |
| 81 | + |
| 82 | +func (o *EditorOpener) openLocalFile(memberIndex int, readOnly bool) (*apiEditorURL, error) { |
| 83 | + params, err := o.OpenLocalFileForMember(memberIndex, readOnly) |
| 84 | + if err != nil { |
| 85 | + return nil, err |
| 86 | + } |
| 87 | + doc := apiEditorURL{ |
| 88 | + FileID: params.FileID, |
| 89 | + Protocol: params.Protocol, |
| 90 | + Subdomain: params.Subdomain, |
| 91 | + Instance: params.Instance, |
| 92 | + Sharecode: params.Sharecode, |
| 93 | + } |
| 94 | + return &doc, nil |
| 95 | +} |
| 96 | + |
| 97 | +func (o *EditorOpener) openSharedFile(prepared *PreparedRequest) (*apiEditorURL, error) { |
| 98 | + res, err := o.RequestSharedFile(prepared, "/editor/"+prepared.XoredID+"/open") |
| 99 | + if res != nil { |
| 100 | + defer res.Body.Close() |
| 101 | + } |
| 102 | + if err != nil { |
| 103 | + return nil, ErrInternalServerError |
| 104 | + } |
| 105 | + var doc apiEditorURL |
| 106 | + if _, err := jsonapi.Bind(res.Body, &doc); err != nil { |
| 107 | + return nil, err |
| 108 | + } |
| 109 | + return &doc, nil |
| 110 | +} |
0 commit comments