forked from fyne-io/fyne
/
file.go
45 lines (35 loc) · 808 Bytes
/
file.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
package glfw
import (
"fmt"
"os"
"path/filepath"
"fyne.io/fyne"
"fyne.io/fyne/storage"
)
type file struct {
*os.File
path string
}
func (d *gLDriver) FileReaderForURI(uri fyne.URI) (fyne.URIReadCloser, error) {
return openFile(uri, false)
}
func (f *file) Name() string {
return filepath.Base(f.path)
}
func (f *file) URI() fyne.URI {
return storage.NewURI("file://" + f.path)
}
func (d *gLDriver) FileWriterForURI(uri fyne.URI) (fyne.URIWriteCloser, error) {
return openFile(uri, true)
}
func openFile(uri fyne.URI, create bool) (*file, error) {
if uri.Scheme() != "file" {
return nil, fmt.Errorf("invalid URI for file: %s", uri)
}
path := uri.String()[7:]
f, err := os.Open(path)
if err != nil && create {
f, err = os.Create(path)
}
return &file{File: f, path: path}, err
}