Skip to content

Commit eabbdca

Browse files
committed
Refactor to move the uploading functions out to a module
Signed-off-by: Andrew Thornton <[email protected]>
1 parent 5b4d740 commit eabbdca

File tree

8 files changed

+828
-775
lines changed

8 files changed

+828
-775
lines changed

models/repo_editor.go

Lines changed: 0 additions & 771 deletions
This file was deleted.

models/upload.go

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

modules/uploader/delete.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// Copyright 2019 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package uploader
6+
7+
import (
8+
"fmt"
9+
10+
"code.gitea.io/git"
11+
"code.gitea.io/gitea/models"
12+
)
13+
14+
// DeleteRepoFileOptions holds the repository delete file options
15+
type DeleteRepoFileOptions struct {
16+
LastCommitID string
17+
OldBranch string
18+
NewBranch string
19+
TreePath string
20+
Message string
21+
}
22+
23+
// DeleteRepoFile deletes a file in the given repository
24+
func DeleteRepoFile(repo *models.Repository, doer *models.User, opts DeleteRepoFileOptions) error {
25+
t, err := NewTemporaryUploadRepository(repo)
26+
defer t.Close()
27+
if err != nil {
28+
return err
29+
}
30+
if err := t.Clone(opts.OldBranch); err != nil {
31+
return err
32+
}
33+
if err := t.SetDefaultIndex(); err != nil {
34+
return err
35+
}
36+
37+
filesInIndex, err := t.LsFiles(opts.TreePath)
38+
if err != nil {
39+
return fmt.Errorf("UpdateRepoFile: %v", err)
40+
}
41+
42+
inFilelist := false
43+
for _, file := range filesInIndex {
44+
if file == opts.TreePath {
45+
inFilelist = true
46+
}
47+
}
48+
if !inFilelist {
49+
return git.ErrNotExist{RelPath: opts.TreePath}
50+
}
51+
52+
if err := t.RemoveFilesFromIndex(opts.TreePath); err != nil {
53+
return err
54+
}
55+
56+
// Now write the tree
57+
treeHash, err := t.WriteTree()
58+
if err != nil {
59+
return err
60+
}
61+
62+
// Now commit the tree
63+
commitHash, err := t.CommitTree(doer, treeHash, opts.Message)
64+
if err != nil {
65+
return err
66+
}
67+
68+
// Then push this tree to NewBranch
69+
if err := t.Push(doer, commitHash, opts.NewBranch); err != nil {
70+
return err
71+
}
72+
73+
// Simulate push event.
74+
oldCommitID := opts.LastCommitID
75+
if opts.NewBranch != opts.OldBranch {
76+
oldCommitID = git.EmptySHA
77+
}
78+
79+
if err = repo.GetOwner(); err != nil {
80+
return fmt.Errorf("GetOwner: %v", err)
81+
}
82+
err = models.PushUpdate(
83+
opts.NewBranch,
84+
models.PushUpdateOptions{
85+
PusherID: doer.ID,
86+
PusherName: doer.Name,
87+
RepoUserName: repo.Owner.Name,
88+
RepoName: repo.Name,
89+
RefFullName: git.BranchPrefix + opts.NewBranch,
90+
OldCommitID: oldCommitID,
91+
NewCommitID: commitHash,
92+
},
93+
)
94+
if err != nil {
95+
return fmt.Errorf("PushUpdate: %v", err)
96+
}
97+
98+
// FIXME: Should we UpdateRepoIndexer(repo) here?
99+
return nil
100+
}

modules/uploader/diff.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2019 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package uploader
6+
7+
import (
8+
"strings"
9+
10+
"code.gitea.io/gitea/models"
11+
)
12+
13+
// GetDiffPreview produces and returns diff result of a file which is not yet committed.
14+
func GetDiffPreview(repo *models.Repository, branch, treePath, content string) (*models.Diff, error) {
15+
t, err := NewTemporaryUploadRepository(repo)
16+
defer t.Close()
17+
if err != nil {
18+
return nil, err
19+
}
20+
if err := t.Clone(branch); err != nil {
21+
return nil, err
22+
}
23+
if err := t.SetDefaultIndex(); err != nil {
24+
return nil, err
25+
}
26+
27+
// Add the object to the database
28+
objectHash, err := t.HashObject(strings.NewReader(content))
29+
if err != nil {
30+
return nil, err
31+
}
32+
33+
// Add the object to the index
34+
if err := t.AddObjectToIndex("100644", objectHash, treePath); err != nil {
35+
return nil, err
36+
}
37+
return t.DiffIndex()
38+
}

0 commit comments

Comments
 (0)