/
docs.go
128 lines (103 loc) · 2.79 KB
/
docs.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
package internal
import (
"errors"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/storage"
)
var errNoAppID = errors.New("storage API requires a unique ID, use app.NewWithID()")
// Docs is an internal implementation of the document features of the Storage interface.
// It is based on top of the current `file` repository and is rooted at RootDocURI.
type Docs struct {
RootDocURI fyne.URI
}
// Create will create a new document ready for writing, you must write something and close the returned writer
// for the create process to complete.
// If the document for this app with that name already exists a storage.ErrAlreadyExists error will be returned.
func (d *Docs) Create(name string) (fyne.URIWriteCloser, error) {
if d.RootDocURI == nil {
return nil, errNoAppID
}
err := d.ensureRootExists()
if err != nil {
return nil, err
}
u, err := storage.Child(d.RootDocURI, name)
if err != nil {
return nil, err
}
exists, err := storage.Exists(u)
if err != nil {
return nil, err
}
if exists {
return nil, storage.ErrAlreadyExists
}
return storage.Writer(u)
}
// List returns all documents that have been saved by the current application.
// Remember to use `app.NewWithID` so that your storage is unique.
func (d *Docs) List() []string {
if d.RootDocURI == nil {
return nil
}
uris, err := storage.List(d.RootDocURI)
if err != nil {
return nil
}
ret := make([]string, len(uris))
for i, u := range uris {
ret[i] = u.Name()
}
return ret
}
// Open will grant access to the contents of the named file. If an error occurs it is returned instead.
func (d *Docs) Open(name string) (fyne.URIReadCloser, error) {
if d.RootDocURI == nil {
return nil, errNoAppID
}
u, err := storage.Child(d.RootDocURI, name)
if err != nil {
return nil, err
}
return storage.Reader(u)
}
// Remove will delete the document with the specified name, if it exists
func (d *Docs) Remove(name string) error {
if d.RootDocURI == nil {
return errNoAppID
}
u, err := storage.Child(d.RootDocURI, name)
if err != nil {
return err
}
return storage.Delete(u)
}
// Save will open a document ready for writing, you close the returned writer for the save to complete.
// If the document for this app with that name does not exist a storage.ErrNotExists error will be returned.
func (d *Docs) Save(name string) (fyne.URIWriteCloser, error) {
if d.RootDocURI == nil {
return nil, errNoAppID
}
u, err := storage.Child(d.RootDocURI, name)
if err != nil {
return nil, err
}
exists, err := storage.Exists(u)
if err != nil {
return nil, err
}
if !exists {
return nil, storage.ErrNotExists
}
return storage.Writer(u)
}
func (d *Docs) ensureRootExists() error {
exists, err := storage.Exists(d.RootDocURI)
if err != nil {
return err
}
if exists {
return nil
}
return storage.CreateListable(d.RootDocURI)
}