Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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