docs.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package internal
  2. import (
  3. "errors"
  4. "fyne.io/fyne/v2"
  5. "fyne.io/fyne/v2/storage"
  6. )
  7. var errNoAppID = errors.New("storage API requires a unique ID, use app.NewWithID()")
  8. // Docs is an internal implementation of the document features of the Storage interface.
  9. // It is based on top of the current `file` repository and is rooted at RootDocURI.
  10. type Docs struct {
  11. RootDocURI fyne.URI
  12. }
  13. // Create will create a new document ready for writing, you must write something and close the returned writer
  14. // for the create process to complete.
  15. // If the document for this app with that name already exists a storage.ErrAlreadyExists error will be returned.
  16. func (d *Docs) Create(name string) (fyne.URIWriteCloser, error) {
  17. if d.RootDocURI == nil {
  18. return nil, errNoAppID
  19. }
  20. err := d.ensureRootExists()
  21. if err != nil {
  22. return nil, err
  23. }
  24. u, err := storage.Child(d.RootDocURI, name)
  25. if err != nil {
  26. return nil, err
  27. }
  28. exists, err := storage.Exists(u)
  29. if err != nil {
  30. return nil, err
  31. }
  32. if exists {
  33. return nil, storage.ErrAlreadyExists
  34. }
  35. return storage.Writer(u)
  36. }
  37. // List returns all documents that have been saved by the current application.
  38. // Remember to use `app.NewWithID` so that your storage is unique.
  39. func (d *Docs) List() []string {
  40. if d.RootDocURI == nil {
  41. return nil
  42. }
  43. uris, err := storage.List(d.RootDocURI)
  44. if err != nil {
  45. return nil
  46. }
  47. ret := make([]string, len(uris))
  48. for i, u := range uris {
  49. ret[i] = u.Name()
  50. }
  51. return ret
  52. }
  53. // Open will grant access to the contents of the named file. If an error occurs it is returned instead.
  54. func (d *Docs) Open(name string) (fyne.URIReadCloser, error) {
  55. if d.RootDocURI == nil {
  56. return nil, errNoAppID
  57. }
  58. u, err := storage.Child(d.RootDocURI, name)
  59. if err != nil {
  60. return nil, err
  61. }
  62. return storage.Reader(u)
  63. }
  64. // Remove will delete the document with the specified name, if it exists
  65. func (d *Docs) Remove(name string) error {
  66. if d.RootDocURI == nil {
  67. return errNoAppID
  68. }
  69. u, err := storage.Child(d.RootDocURI, name)
  70. if err != nil {
  71. return err
  72. }
  73. return storage.Delete(u)
  74. }
  75. // Save will open a document ready for writing, you close the returned writer for the save to complete.
  76. // If the document for this app with that name does not exist a storage.ErrNotExists error will be returned.
  77. func (d *Docs) Save(name string) (fyne.URIWriteCloser, error) {
  78. if d.RootDocURI == nil {
  79. return nil, errNoAppID
  80. }
  81. u, err := storage.Child(d.RootDocURI, name)
  82. if err != nil {
  83. return nil, err
  84. }
  85. exists, err := storage.Exists(u)
  86. if err != nil {
  87. return nil, err
  88. }
  89. if !exists {
  90. return nil, storage.ErrNotExists
  91. }
  92. return storage.Writer(u)
  93. }
  94. func (d *Docs) ensureRootExists() error {
  95. exists, err := storage.Exists(d.RootDocURI)
  96. if err != nil {
  97. return err
  98. }
  99. if exists {
  100. return nil
  101. }
  102. return storage.CreateListable(d.RootDocURI)
  103. }