GraphQL API and utilities for the rpdata project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 lines
4.2 KiB

  1. package file
  2. import (
  3. "context"
  4. "crypto/rand"
  5. "encoding/binary"
  6. "errors"
  7. "io"
  8. "strconv"
  9. "time"
  10. "git.aiterp.net/rpdata/api/internal/store"
  11. "github.com/globalsign/mgo"
  12. "github.com/globalsign/mgo/bson"
  13. )
  14. var fileCollection *mgo.Collection
  15. // A File is a record of a file stored in the Space.
  16. type File struct {
  17. ID string `bson:"_id" json:"id"`
  18. Time time.Time `bson:"time" json:"time"`
  19. Kind string `bson:"kind" json:"kind"`
  20. Public bool `bson:"public" json:"public"`
  21. Name string `bson:"name" json:"name"`
  22. MimeType string `bson:"mimeType" json:"mimeType"`
  23. Size int64 `bson:"size" json:"size"`
  24. Author string `bson:"author" json:"author"`
  25. URL string `bson:"url,omitempty" json:"url,omitempty"`
  26. }
  27. // Edit edits the file, changing up to both the two mutable properties
  28. func (file *File) Edit(name *string, public *bool) error {
  29. changes := bson.M{}
  30. changedFile := *file
  31. if name != nil && *name != file.Name {
  32. changes["name"] = *name
  33. changedFile.Name = *name
  34. }
  35. if public != nil && *public != file.Public {
  36. changes["public"] = *public
  37. changedFile.Public = *public
  38. }
  39. if len(changes) == 0 {
  40. return nil
  41. }
  42. err := fileCollection.UpdateId(file.ID, bson.M{"$set": changes})
  43. if err != nil {
  44. return err
  45. }
  46. *file = changedFile
  47. return nil
  48. }
  49. // Delete removes the file information from the database, and deletes the file.
  50. func (file *File) Delete() error {
  51. err := fileCollection.RemoveId(file.ID)
  52. if err != nil {
  53. return err
  54. }
  55. if file.Kind == "upload" {
  56. err = store.RemoveFile("files", file.ID)
  57. if err != nil {
  58. return err
  59. }
  60. }
  61. file.URL = ""
  62. return nil
  63. }
  64. // Upload adds a file to the space.
  65. func Upload(ctx context.Context, name, mimeType, author string, size int64, input io.Reader) (File, error) {
  66. if name == "" {
  67. date := time.Now().UTC().Format("Jan 02 2006 15:04:05 MST")
  68. name = "Unnamed file (" + date + ")"
  69. }
  70. if mimeType == "" {
  71. mimeType = "binary/octet-stream"
  72. }
  73. id := makeFileID()
  74. path, err := store.UploadFile(ctx, "files", id, mimeType, input, size)
  75. if err != nil {
  76. return File{}, err
  77. }
  78. file := File{
  79. ID: id,
  80. Kind: "upload",
  81. Time: time.Now(),
  82. Public: false,
  83. Author: author,
  84. Name: name,
  85. MimeType: mimeType,
  86. Size: size,
  87. URL: store.URLFromPath(path),
  88. }
  89. err = fileCollection.Insert(file)
  90. if err != nil {
  91. return File{}, err
  92. }
  93. return file, nil
  94. }
  95. // FindID finds a file by ID
  96. func FindID(id string) (File, error) {
  97. file := File{}
  98. err := fileCollection.FindId(id).One(&file)
  99. if err != nil {
  100. return File{}, err
  101. }
  102. return file, nil
  103. }
  104. // List lists files according to the standard lookup. By default it's just the author's own files,
  105. // but if `public` is true it will alos include files made public by other authors. If `mimeTypes` contains
  106. // any, it will limit the results to that. If `author` is empty, it will only list public files
  107. func List(author string, public bool, mimeTypes []string) ([]File, error) {
  108. query := bson.M{}
  109. if author != "" {
  110. if public {
  111. query["$or"] = []bson.M{
  112. bson.M{"author": author},
  113. bson.M{"public": true},
  114. }
  115. } else {
  116. query["author"] = author
  117. }
  118. } else {
  119. if !public {
  120. return nil, errors.New("No author specified, and public is unset")
  121. }
  122. query["public"] = true
  123. }
  124. if len(mimeTypes) > 0 {
  125. query["mimeTypes"] = bson.M{"$in": mimeTypes}
  126. }
  127. return listFiles(query)
  128. }
  129. func listFiles(query interface{}) ([]File, error) {
  130. list := make([]File, 0, 32)
  131. err := fileCollection.Find(query).All(&list)
  132. if err != nil {
  133. return nil, err
  134. }
  135. return list, nil
  136. }
  137. // makeFileID makes a random file ID that's 32 characters long
  138. func makeFileID() string {
  139. result := "F" + strconv.FormatInt(time.Now().UnixNano(), 36)
  140. offset := 0
  141. data := make([]byte, 32)
  142. rand.Read(data)
  143. for len(result) < 32 {
  144. result += strconv.FormatUint(binary.LittleEndian.Uint64(data[offset:]), 36)
  145. offset += 8
  146. if offset >= 32 {
  147. rand.Read(data)
  148. offset = 0
  149. }
  150. }
  151. return result[:32]
  152. }
  153. func init() {
  154. store.HandleInit(func(db *mgo.Database) {
  155. fileCollection = db.C("file.headers")
  156. fileCollection.EnsureIndexKey("author")
  157. fileCollection.EnsureIndexKey("public")
  158. fileCollection.EnsureIndexKey("author", "public")
  159. fileCollection.EnsureIndexKey("kind")
  160. })
  161. }