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.

upload.go 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package models
  6. import (
  7. "fmt"
  8. "io"
  9. "mime/multipart"
  10. "os"
  11. "path"
  12. "code.gitea.io/gitea/models/db"
  13. "code.gitea.io/gitea/modules/log"
  14. "code.gitea.io/gitea/modules/setting"
  15. "code.gitea.io/gitea/modules/util"
  16. gouuid "github.com/google/uuid"
  17. )
  18. // ____ ___ .__ .___ ___________.___.__
  19. // | | \______ | | _________ __| _/ \_ _____/| | | ____ ______
  20. // | | /\____ \| | / _ \__ \ / __ | | __) | | | _/ __ \ / ___/
  21. // | | / | |_> > |_( <_> ) __ \_/ /_/ | | \ | | |_\ ___/ \___ \
  22. // |______/ | __/|____/\____(____ /\____ | \___ / |___|____/\___ >____ >
  23. // |__| \/ \/ \/ \/ \/
  24. //
  25. // Upload represent a uploaded file to a repo to be deleted when moved
  26. type Upload struct {
  27. ID int64 `xorm:"pk autoincr"`
  28. UUID string `xorm:"uuid UNIQUE"`
  29. Name string
  30. }
  31. func init() {
  32. db.RegisterModel(new(Upload))
  33. }
  34. // UploadLocalPath returns where uploads is stored in local file system based on given UUID.
  35. func UploadLocalPath(uuid string) string {
  36. return path.Join(setting.Repository.Upload.TempPath, uuid[0:1], uuid[1:2], uuid)
  37. }
  38. // LocalPath returns where uploads are temporarily stored in local file system.
  39. func (upload *Upload) LocalPath() string {
  40. return UploadLocalPath(upload.UUID)
  41. }
  42. // NewUpload creates a new upload object.
  43. func NewUpload(name string, buf []byte, file multipart.File) (_ *Upload, err error) {
  44. upload := &Upload{
  45. UUID: gouuid.New().String(),
  46. Name: name,
  47. }
  48. localPath := upload.LocalPath()
  49. if err = os.MkdirAll(path.Dir(localPath), os.ModePerm); err != nil {
  50. return nil, fmt.Errorf("MkdirAll: %v", err)
  51. }
  52. fw, err := os.Create(localPath)
  53. if err != nil {
  54. return nil, fmt.Errorf("Create: %v", err)
  55. }
  56. defer fw.Close()
  57. if _, err = fw.Write(buf); err != nil {
  58. return nil, fmt.Errorf("Write: %v", err)
  59. } else if _, err = io.Copy(fw, file); err != nil {
  60. return nil, fmt.Errorf("Copy: %v", err)
  61. }
  62. if _, err := db.GetEngine(db.DefaultContext).Insert(upload); err != nil {
  63. return nil, err
  64. }
  65. return upload, nil
  66. }
  67. // GetUploadByUUID returns the Upload by UUID
  68. func GetUploadByUUID(uuid string) (*Upload, error) {
  69. upload := &Upload{}
  70. has, err := db.GetEngine(db.DefaultContext).Where("uuid=?", uuid).Get(upload)
  71. if err != nil {
  72. return nil, err
  73. } else if !has {
  74. return nil, ErrUploadNotExist{0, uuid}
  75. }
  76. return upload, nil
  77. }
  78. // GetUploadsByUUIDs returns multiple uploads by UUIDS
  79. func GetUploadsByUUIDs(uuids []string) ([]*Upload, error) {
  80. if len(uuids) == 0 {
  81. return []*Upload{}, nil
  82. }
  83. // Silently drop invalid uuids.
  84. uploads := make([]*Upload, 0, len(uuids))
  85. return uploads, db.GetEngine(db.DefaultContext).In("uuid", uuids).Find(&uploads)
  86. }
  87. // DeleteUploads deletes multiple uploads
  88. func DeleteUploads(uploads ...*Upload) (err error) {
  89. if len(uploads) == 0 {
  90. return nil
  91. }
  92. ctx, committer, err := db.TxContext()
  93. if err != nil {
  94. return err
  95. }
  96. defer committer.Close()
  97. ids := make([]int64, len(uploads))
  98. for i := 0; i < len(uploads); i++ {
  99. ids[i] = uploads[i].ID
  100. }
  101. if _, err = db.GetEngine(ctx).
  102. In("id", ids).
  103. Delete(new(Upload)); err != nil {
  104. return fmt.Errorf("delete uploads: %v", err)
  105. }
  106. if err = committer.Commit(); err != nil {
  107. return err
  108. }
  109. for _, upload := range uploads {
  110. localPath := upload.LocalPath()
  111. isFile, err := util.IsFile(localPath)
  112. if err != nil {
  113. log.Error("Unable to check if %s is a file. Error: %v", localPath, err)
  114. }
  115. if !isFile {
  116. continue
  117. }
  118. if err := util.Remove(localPath); err != nil {
  119. return fmt.Errorf("remove upload: %v", err)
  120. }
  121. }
  122. return nil
  123. }
  124. // DeleteUploadByUUID deletes a upload by UUID
  125. func DeleteUploadByUUID(uuid string) error {
  126. upload, err := GetUploadByUUID(uuid)
  127. if err != nil {
  128. if IsErrUploadNotExist(err) {
  129. return nil
  130. }
  131. return fmt.Errorf("GetUploadByUUID: %v", err)
  132. }
  133. if err := DeleteUploads(upload); err != nil {
  134. return fmt.Errorf("DeleteUpload: %v", err)
  135. }
  136. return nil
  137. }