本站源代码
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.

156 lines
3.8KB

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