generic.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package repository
  2. import (
  3. "io"
  4. "strings"
  5. "fyne.io/fyne/v2"
  6. )
  7. // splitNonEmpty works exactly like strings.Split(), but only returns non-empty
  8. // components.
  9. func splitNonEmpty(str, sep string) []string {
  10. components := []string{}
  11. for _, v := range strings.Split(str, sep) {
  12. if len(v) > 0 {
  13. components = append(components, v)
  14. }
  15. }
  16. return components
  17. }
  18. // GenericParent can be used as a common-case implementation of
  19. // HierarchicalRepository.Parent(). It will create a parent URI based on
  20. // IETF RFC3986.
  21. //
  22. // In short, the URI is separated into it's component parts, the path component
  23. // is split along instances of '/', and the trailing element is removed. The
  24. // result is concatenated and parsed as a new URI.
  25. //
  26. // If the URI path is empty or '/', then a nil URI is returned, along with
  27. // ErrURIRoot.
  28. //
  29. // NOTE: this function should not be called except by an implementation of
  30. // the Repository interface - using this for unknown URIs may break.
  31. //
  32. // Since: 2.0
  33. func GenericParent(u fyne.URI) (fyne.URI, error) {
  34. p := u.Path()
  35. if p == "" || p == "/" {
  36. return nil, ErrURIRoot
  37. }
  38. components := splitNonEmpty(p, "/")
  39. newURI := u.Scheme() + "://" + u.Authority()
  40. // there will be at least one component, since we know we don't have
  41. // '/' or ''.
  42. newURI += "/"
  43. if len(components) > 1 {
  44. newURI += strings.Join(components[:len(components)-1], "/")
  45. }
  46. // stick the query and fragment back on the end
  47. if q := u.Query(); len(q) > 0 {
  48. newURI += "?" + q
  49. }
  50. if f := u.Fragment(); len(f) > 0 {
  51. newURI += "#" + f
  52. }
  53. // NOTE: we specifically want to use ParseURI, rather than &uri{},
  54. // since the repository for the URI we just created might be a
  55. // CustomURIRepository that implements it's own ParseURI.
  56. return ParseURI(newURI)
  57. }
  58. // GenericChild can be used as a common-case implementation of
  59. // HierarchicalRepository.Child(). It will create a child URI by separating the
  60. // URI into it's component parts as described in IETF RFC 3986, then appending
  61. // "/" + component to the path, then concatenating the result and parsing it as
  62. // a new URI.
  63. //
  64. // NOTE: this function should not be called except by an implementation of
  65. // the Repository interface - using this for unknown URIs may break.
  66. //
  67. // Since: 2.0
  68. func GenericChild(u fyne.URI, component string) (fyne.URI, error) {
  69. // split into components and add the new one
  70. components := splitNonEmpty(u.Path(), "/")
  71. components = append(components, component)
  72. // generate the scheme, authority, and path
  73. newURI := u.Scheme() + "://" + u.Authority()
  74. newURI += "/" + strings.Join(components, "/")
  75. // stick the query and fragment back on the end
  76. if q := u.Query(); len(q) > 0 {
  77. newURI += "?" + q
  78. }
  79. if f := u.Fragment(); len(f) > 0 {
  80. newURI += "#" + f
  81. }
  82. // NOTE: we specifically want to use ParseURI, rather than &uri{},
  83. // since the repository for the URI we just created might be a
  84. // CustomURIRepository that implements it's own ParseURI.
  85. return ParseURI(newURI)
  86. }
  87. // GenericCopy can be used a common-case implementation of
  88. // CopyableRepository.Copy(). It will perform the copy by obtaining a reader
  89. // for the source URI, a writer for the destination URI, then writing the
  90. // contents of the source to the destination.
  91. //
  92. // For obvious reasons, the destination URI must have a registered
  93. // WritableRepository.
  94. //
  95. // NOTE: this function should not be called except by an implementation of
  96. // the Repository interface - using this for unknown URIs may break.
  97. //
  98. // Since: 2.0
  99. func GenericCopy(source fyne.URI, destination fyne.URI) error {
  100. // Look up repositories for the source and destination.
  101. srcrepo, err := ForURI(source)
  102. if err != nil {
  103. return err
  104. }
  105. dstrepo, err := ForURI(destination)
  106. if err != nil {
  107. return err
  108. }
  109. // The destination must be writable.
  110. destwrepo, ok := dstrepo.(WritableRepository)
  111. if !ok {
  112. return ErrOperationNotSupported
  113. }
  114. // Create a reader and a writer.
  115. srcReader, err := srcrepo.Reader(source)
  116. if err != nil {
  117. return err
  118. }
  119. defer srcReader.Close()
  120. dstWriter, err := destwrepo.Writer(destination)
  121. if err != nil {
  122. return err
  123. }
  124. defer dstWriter.Close()
  125. // Perform the copy.
  126. _, err = io.Copy(dstWriter, srcReader)
  127. return err
  128. }
  129. // GenericMove can be used a common-case implementation of
  130. // MovableRepository.Move(). It will perform the move by obtaining a reader
  131. // for the source URI, a writer for the destination URI, then writing the
  132. // contents of the source to the destination. Following this, the source
  133. // will be deleted using WritableRepository.Delete.
  134. //
  135. // For obvious reasons, the source and destination URIs must both be writable.
  136. //
  137. // NOTE: this function should not be called except by an implementation of
  138. // the Repository interface - using this for unknown URIs may break.
  139. //
  140. // Since: 2.0
  141. func GenericMove(source fyne.URI, destination fyne.URI) error {
  142. // This looks a lot like GenericCopy(), but I duplicated the code
  143. // to avoid having to look up the repositories more than once.
  144. // Look up repositories for the source and destination.
  145. srcrepo, err := ForURI(source)
  146. if err != nil {
  147. return err
  148. }
  149. dstrepo, err := ForURI(destination)
  150. if err != nil {
  151. return err
  152. }
  153. // The source and destination must both be writable, since the source
  154. // is being deleted, which requires WritableRepository.
  155. destwrepo, ok := dstrepo.(WritableRepository)
  156. if !ok {
  157. return ErrOperationNotSupported
  158. }
  159. srcwrepo, ok := srcrepo.(WritableRepository)
  160. if !ok {
  161. return ErrOperationNotSupported
  162. }
  163. // Create the reader and writer to perform the copy operation.
  164. srcReader, err := srcrepo.Reader(source)
  165. if err != nil {
  166. return err
  167. }
  168. dstWriter, err := destwrepo.Writer(destination)
  169. if err != nil {
  170. return err
  171. }
  172. defer dstWriter.Close()
  173. // Perform the copy.
  174. _, err = io.Copy(dstWriter, srcReader)
  175. if err != nil {
  176. return err
  177. }
  178. // Finally, delete the source only if the move finished without error.
  179. srcReader.Close()
  180. return srcwrepo.Delete(source)
  181. }