aboutsummaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorUnknwon <u@gogs.io>2016-08-30 05:07:50 -0700
committerUnknwon <u@gogs.io>2016-08-30 05:23:59 -0700
commit643142acab44d46aa6c001c90ad5d307a8662b99 (patch)
tree8e26f8959af5175e865d71a35df5dc087ff14dc8 /models
parent7c31f235da287b0ba1499986758332c2c346deb0 (diff)
downloadgitea-643142acab44d46aa6c001c90ad5d307a8662b99.tar.gz
gitea-643142acab44d46aa6c001c90ad5d307a8662b99.zip
Web editor: support upload files
Diffstat (limited to 'models')
-rw-r--r--models/error.go8
-rw-r--r--models/issue.go15
-rw-r--r--models/models.go2
-rw-r--r--models/pull.go6
-rw-r--r--models/repo.go194
-rw-r--r--models/repo_editor.go227
6 files changed, 239 insertions, 213 deletions
diff --git a/models/error.go b/models/error.go
index 240e606776..dd1d4a668d 100644
--- a/models/error.go
+++ b/models/error.go
@@ -652,10 +652,8 @@ func (err ErrTeamAlreadyExist) Error() string {
//
type ErrUploadNotExist struct {
- ID int64
- UUID string
- UserID int64
- RepoID int64
+ ID int64
+ UUID string
}
func IsErrUploadNotExist(err error) bool {
@@ -664,5 +662,5 @@ func IsErrUploadNotExist(err error) bool {
}
func (err ErrUploadNotExist) Error() string {
- return fmt.Sprintf("attachment does not exist [id: %d, uuid: %s, user_id: %d, repo_id: %d]", err.ID, err.UUID, err.UserID, err.RepoID)
+ return fmt.Sprintf("attachment does not exist [id: %d, uuid: %s]", err.ID, err.UUID)
}
diff --git a/models/issue.go b/models/issue.go
index a90ebaf866..208204b076 100644
--- a/models/issue.go
+++ b/models/issue.go
@@ -1685,11 +1685,12 @@ func NewAttachment(name string, buf []byte, file multipart.File) (_ *Attachment,
Name: name,
}
- if err = os.MkdirAll(path.Dir(attach.LocalPath()), os.ModePerm); err != nil {
+ localPath := attach.LocalPath()
+ if err = os.MkdirAll(path.Dir(localPath), os.ModePerm); err != nil {
return nil, fmt.Errorf("MkdirAll: %v", err)
}
- fw, err := os.Create(attach.LocalPath())
+ fw, err := os.Create(localPath)
if err != nil {
return nil, fmt.Errorf("Create: %v", err)
}
@@ -1701,17 +1702,11 @@ func NewAttachment(name string, buf []byte, file multipart.File) (_ *Attachment,
return nil, fmt.Errorf("Copy: %v", err)
}
- sess := x.NewSession()
- defer sessionRelease(sess)
- if err := sess.Begin(); err != nil {
- return nil, err
- }
-
- if _, err := sess.Insert(attach); err != nil {
+ if _, err := x.Insert(attach); err != nil {
return nil, err
}
- return attach, sess.Commit()
+ return attach, nil
}
func getAttachmentByUUID(e Engine, uuid string) (*Attachment, error) {
diff --git a/models/models.go b/models/models.go
index 582c4403ad..481e53e36e 100644
--- a/models/models.go
+++ b/models/models.go
@@ -60,7 +60,7 @@ var (
func init() {
tables = append(tables,
new(User), new(PublicKey), new(AccessToken),
- new(Repository), new(DeployKey), new(Collaboration), new(Access),
+ new(Repository), new(DeployKey), new(Collaboration), new(Access), new(Upload),
new(Watch), new(Star), new(Follow), new(Action),
new(Issue), new(PullRequest), new(Comment), new(Attachment), new(IssueUser),
new(Label), new(IssueLabel), new(Milestone),
diff --git a/models/pull.go b/models/pull.go
index fa9a44897a..64b347554c 100644
--- a/models/pull.go
+++ b/models/pull.go
@@ -339,9 +339,6 @@ var patchConflicts = []string{
// testPatch checks if patch can be merged to base repository without conflit.
// FIXME: make a mechanism to clean up stable local copies.
func (pr *PullRequest) testPatch() (err error) {
- repoWorkingPool.CheckIn(com.ToStr(pr.BaseRepoID))
- defer repoWorkingPool.CheckOut(com.ToStr(pr.BaseRepoID))
-
if pr.BaseRepo == nil {
pr.BaseRepo, err = GetRepositoryByID(pr.BaseRepoID)
if err != nil {
@@ -360,6 +357,9 @@ func (pr *PullRequest) testPatch() (err error) {
return nil
}
+ repoWorkingPool.CheckIn(com.ToStr(pr.BaseRepoID))
+ defer repoWorkingPool.CheckOut(com.ToStr(pr.BaseRepoID))
+
log.Trace("PullRequest[%d].testPatch (patchPath): %s", pr.ID, patchPath)
if err := pr.BaseRepo.UpdateLocalCopyBranch(pr.BaseBranch); err != nil {
diff --git a/models/repo.go b/models/repo.go
index 2c19afbd17..a26ba92482 100644
--- a/models/repo.go
+++ b/models/repo.go
@@ -9,9 +9,7 @@ import (
"errors"
"fmt"
"html/template"
- "io"
"io/ioutil"
- "mime/multipart"
"os"
"os/exec"
"path"
@@ -29,7 +27,6 @@ import (
git "github.com/gogits/git-module"
api "github.com/gogits/go-gogs-client"
- gouuid "github.com/satori/go.uuid"
"github.com/gogits/gogs/modules/bindata"
"github.com/gogits/gogs/modules/log"
@@ -2272,197 +2269,6 @@ func (repo *Repository) GetForks() ([]*Repository, error) {
return forks, x.Find(&forks, &Repository{ForkID: repo.ID})
}
-// ____ ___ .__ .___ ___________.___.__
-// | | \______ | | _________ __| _/ \_ _____/| | | ____ ______
-// | | /\____ \| | / _ \__ \ / __ | | __) | | | _/ __ \ / ___/
-// | | / | |_> > |_( <_> ) __ \_/ /_/ | | \ | | |_\ ___/ \___ \
-// |______/ | __/|____/\____(____ /\____ | \___ / |___|____/\___ >____ >
-// |__| \/ \/ \/ \/ \/
-//
-
-// uploadRepoFiles uploads new files to repository.
-func (repo *Repository) UploadRepoFiles(doer *User, oldBranchName, branchName, treePath, message string, uuids []string) (err error) {
- repoWorkingPool.CheckIn(com.ToStr(repo.ID))
- defer repoWorkingPool.CheckOut(com.ToStr(repo.ID))
-
- localPath := repo.LocalCopyPath()
-
- if err = discardLocalRepoBranchChanges(localPath, oldBranchName); err != nil {
- return fmt.Errorf("discardLocalRepoChanges: %v", err)
- } else if err = repo.UpdateLocalCopyBranch(oldBranchName); err != nil {
- return fmt.Errorf("UpdateLocalCopyBranch: %v", err)
- }
-
- if oldBranchName != branchName {
- repo.CheckoutNewBranch(oldBranchName, branchName)
- }
-
- dirPath := path.Join(localPath, treePath)
- os.MkdirAll(dirPath, os.ModePerm)
-
- // Copy uploaded files into repository.
- for _, uuid := range uuids {
- upload, err := getUpload(uuid, doer.ID, repo.ID)
- if err != nil {
- if IsErrUploadNotExist(err) {
- continue
- }
- return fmt.Errorf("getUpload[%s]: %v", uuid, err)
- }
- uuidPath := upload.LocalPath()
- filePath := dirPath + "/" + upload.Name
- if err := os.Rename(uuidPath, filePath); err != nil {
- DeleteUpload(upload, true)
- return fmt.Errorf("Rename[%s -> %s]: %v", uuidPath, filePath, err)
- }
- DeleteUpload(upload, false) // false because we have moved the file
- }
-
- if len(message) == 0 {
- message = "Add files to '" + treePath + "'"
- }
-
- if err = git.AddChanges(localPath, true); err != nil {
- return fmt.Errorf("AddChanges: %v", err)
- } else if err = git.CommitChanges(localPath, git.CommitChangesOptions{
- Committer: doer.NewGitSig(),
- Message: message,
- }); err != nil {
- return fmt.Errorf("CommitChanges: %v", err)
- } else if err = git.Push(localPath, "origin", branchName); err != nil {
- return fmt.Errorf("Push: %v", err)
- }
-
- return nil
-}
-
-// Upload represent a uploaded file to a repo to be deleted when moved
-type Upload struct {
- ID int64 `xorm:"pk autoincr"`
- UUID string `xorm:"uuid UNIQUE"`
- UID int64 `xorm:"INDEX"`
- RepoID int64 `xorm:"INDEX"`
- Name string
- Created time.Time `xorm:"-"`
- CreatedUnix int64
-}
-
-func (u *Upload) BeforeInsert() {
- u.CreatedUnix = time.Now().UTC().Unix()
-}
-
-func (u *Upload) AfterSet(colName string, _ xorm.Cell) {
- switch colName {
- case "created_unix":
- u.Created = time.Unix(u.CreatedUnix, 0).Local()
- }
-}
-
-// UploadLocalPath returns where uploads is stored in local file system based on given UUID.
-func UploadLocalPath(uuid string) string {
- return path.Join(setting.Repository.Upload.TempPath, uuid[0:1], uuid[1:2], uuid)
-}
-
-// LocalPath returns where uploads are temporarily stored in local file system.
-func (upload *Upload) LocalPath() string {
- return UploadLocalPath(upload.UUID)
-}
-
-// NewUpload creates a new upload object.
-func NewUpload(name string, buf []byte, file multipart.File, userId, repoId int64) (_ *Upload, err error) {
- up := &Upload{
- UUID: gouuid.NewV4().String(),
- Name: name,
- UID: userId,
- RepoID: repoId,
- }
-
- if err = os.MkdirAll(path.Dir(up.LocalPath()), os.ModePerm); err != nil {
- return nil, fmt.Errorf("MkdirAll: %v", err)
- }
-
- fw, err := os.Create(up.LocalPath())
- if err != nil {
- return nil, fmt.Errorf("Create: %v", err)
- }
- defer fw.Close()
-
- if _, err = fw.Write(buf); err != nil {
- return nil, fmt.Errorf("Write: %v", err)
- } else if _, err = io.Copy(fw, file); err != nil {
- return nil, fmt.Errorf("Copy: %v", err)
- }
-
- sess := x.NewSession()
- defer sessionRelease(sess)
- if err := sess.Begin(); err != nil {
- return nil, err
- }
- if _, err := sess.Insert(up); err != nil {
- return nil, err
- }
-
- return up, sess.Commit()
-}
-
-// RemoveUpload removes the file by UUID
-func RemoveUpload(uuid string, userId, repoId int64) (err error) {
- sess := x.NewSession()
- defer sessionRelease(sess)
- if err := sess.Begin(); err != nil {
- return err
- }
- upload, err := getUpload(uuid, userId, repoId)
- if err != nil {
- return fmt.Errorf("getUpload[%s]: %v", uuid, err)
- }
-
- if err := DeleteUpload(upload, true); err != nil {
- return fmt.Errorf("DeleteUpload[%s]: %v", uuid, err)
- }
-
- return nil
-}
-
-func getUpload(uuid string, userID, repoID int64) (*Upload, error) {
- up := &Upload{UUID: uuid, UID: userID, RepoID: repoID}
- has, err := x.Get(up)
- if err != nil {
- return nil, err
- } else if !has {
- return nil, ErrUploadNotExist{0, uuid, userID, repoID}
- }
- return up, nil
-}
-
-// GetUpload returns Upload by given UUID.
-func GetUpload(uuid string, userId, repoId int64) (*Upload, error) {
- return getUpload(uuid, userId, repoId)
-}
-
-// DeleteUpload deletes the given upload
-func DeleteUpload(u *Upload, remove bool) error {
- _, err := DeleteUploads([]*Upload{u}, remove)
- return err
-}
-
-// DeleteUploads deletes the given uploads
-func DeleteUploads(uploads []*Upload, remove bool) (int, error) {
- for i, u := range uploads {
- if remove {
- if err := os.Remove(u.LocalPath()); err != nil {
- return i, err
- }
- }
-
- if _, err := x.Delete(u); err != nil {
- return i, err
- }
- }
-
- return len(uploads), nil
-}
-
// __________ .__
// \______ \____________ ____ ____ | |__
// | | _/\_ __ \__ \ / \_/ ___\| | \
diff --git a/models/repo_editor.go b/models/repo_editor.go
index 31b94f631e..dfdb864a1c 100644
--- a/models/repo_editor.go
+++ b/models/repo_editor.go
@@ -6,7 +6,9 @@ package models
import (
"fmt"
+ "io"
"io/ioutil"
+ "mime/multipart"
"os"
"os/exec"
"path"
@@ -14,6 +16,7 @@ import (
"time"
"github.com/Unknwon/com"
+ gouuid "github.com/satori/go.uuid"
git "github.com/gogits/git-module"
@@ -291,3 +294,227 @@ func (repo *Repository) DeleteRepoFile(doer *User, opts DeleteRepoFileOptions) (
return nil
}
+
+// ____ ___ .__ .___ ___________.___.__
+// | | \______ | | _________ __| _/ \_ _____/| | | ____ ______
+// | | /\____ \| | / _ \__ \ / __ | | __) | | | _/ __ \ / ___/
+// | | / | |_> > |_( <_> ) __ \_/ /_/ | | \ | | |_\ ___/ \___ \
+// |______/ | __/|____/\____(____ /\____ | \___ / |___|____/\___ >____ >
+// |__| \/ \/ \/ \/ \/
+//
+
+// Upload represent a uploaded file to a repo to be deleted when moved
+type Upload struct {
+ ID int64 `xorm:"pk autoincr"`
+ UUID string `xorm:"uuid UNIQUE"`
+ Name string
+}
+
+// UploadLocalPath returns where uploads is stored in local file system based on given UUID.
+func UploadLocalPath(uuid string) string {
+ return path.Join(setting.Repository.Upload.TempPath, uuid[0:1], uuid[1:2], uuid)
+}
+
+// LocalPath returns where uploads are temporarily stored in local file system.
+func (upload *Upload) LocalPath() string {
+ return UploadLocalPath(upload.UUID)
+}
+
+// NewUpload creates a new upload object.
+func NewUpload(name string, buf []byte, file multipart.File) (_ *Upload, err error) {
+ upload := &Upload{
+ UUID: gouuid.NewV4().String(),
+ Name: name,
+ }
+
+ localPath := upload.LocalPath()
+ if err = os.MkdirAll(path.Dir(localPath), os.ModePerm); err != nil {
+ return nil, fmt.Errorf("MkdirAll: %v", err)
+ }
+
+ fw, err := os.Create(localPath)
+ if err != nil {
+ return nil, fmt.Errorf("Create: %v", err)
+ }
+ defer fw.Close()
+
+ if _, err = fw.Write(buf); err != nil {
+ return nil, fmt.Errorf("Write: %v", err)
+ } else if _, err = io.Copy(fw, file); err != nil {
+ return nil, fmt.Errorf("Copy: %v", err)
+ }
+
+ if _, err := x.Insert(upload); err != nil {
+ return nil, err
+ }
+
+ return upload, nil
+}
+
+func GetUploadByUUID(uuid string) (*Upload, error) {
+ upload := &Upload{UUID: uuid}
+ has, err := x.Get(upload)
+ if err != nil {
+ return nil, err
+ } else if !has {
+ return nil, ErrUploadNotExist{0, uuid}
+ }
+ return upload, nil
+}
+
+func GetUploadsByUUIDs(uuids []string) ([]*Upload, error) {
+ if len(uuids) == 0 {
+ return []*Upload{}, nil
+ }
+
+ // Silently drop invalid uuids.
+ uploads := make([]*Upload, 0, len(uuids))
+ return uploads, x.In("uuid", uuids).Find(&uploads)
+}
+
+func DeleteUploads(uploads ...*Upload) (err error) {
+ if len(uploads) == 0 {
+ return nil
+ }
+
+ sess := x.NewSession()
+ defer sessionRelease(sess)
+ if err = sess.Begin(); err != nil {
+ return err
+ }
+
+ ids := make([]int64, len(uploads))
+ for i := 0; i < len(uploads); i++ {
+ ids[i] = uploads[i].ID
+ }
+ if _, err = sess.In("id", ids).Delete(new(Upload)); err != nil {
+ return fmt.Errorf("delete uploads: %v", err)
+ }
+
+ for _, upload := range uploads {
+ localPath := upload.LocalPath()
+ if !com.IsFile(localPath) {
+ continue
+ }
+
+ if err := os.Remove(localPath); err != nil {
+ return fmt.Errorf("remove upload: %v", err)
+ }
+ }
+
+ return sess.Commit()
+}
+
+func DeleteUpload(u *Upload) error {
+ return DeleteUploads(u)
+}
+
+func DeleteUploadByUUID(uuid string) error {
+ upload, err := GetUploadByUUID(uuid)
+ if err != nil {
+ if IsErrUploadNotExist(err) {
+ return nil
+ }
+ return fmt.Errorf("GetUploadByUUID: %v", err)
+ }
+
+ if err := DeleteUpload(upload); err != nil {
+ return fmt.Errorf("DeleteUpload: %v", err)
+ }
+
+ return nil
+}
+
+type UploadRepoFileOptions struct {
+ LastCommitID string
+ OldBranch string
+ NewBranch string
+ TreePath string
+ Message string
+ Files []string // In UUID format.
+}
+
+func (repo *Repository) UploadRepoFiles(doer *User, opts UploadRepoFileOptions) (err error) {
+ if len(opts.Files) == 0 {
+ return nil
+ }
+
+ uploads, err := GetUploadsByUUIDs(opts.Files)
+ if err != nil {
+ return fmt.Errorf("GetUploadsByUUIDs [uuids: %v]: %v", opts.Files, err)
+ }
+
+ repoWorkingPool.CheckIn(com.ToStr(repo.ID))
+ defer repoWorkingPool.CheckOut(com.ToStr(repo.ID))
+
+ if err = repo.DiscardLocalRepoBranchChanges(opts.OldBranch); err != nil {
+ return fmt.Errorf("DiscardLocalRepoBranchChanges [branch: %s]: %v", opts.OldBranch, err)
+ } else if err = repo.UpdateLocalCopyBranch(opts.OldBranch); err != nil {
+ return fmt.Errorf("UpdateLocalCopyBranch [branch: %s]: %v", opts.OldBranch, err)
+ }
+
+ if opts.OldBranch != opts.NewBranch {
+ if err = repo.CheckoutNewBranch(opts.OldBranch, opts.NewBranch); err != nil {
+ return fmt.Errorf("CheckoutNewBranch [old_branch: %s, new_branch: %s]: %v", opts.OldBranch, opts.NewBranch, err)
+ }
+ }
+
+ localPath := repo.LocalCopyPath()
+ dirPath := path.Join(localPath, opts.TreePath)
+ os.MkdirAll(dirPath, os.ModePerm)
+
+ // Copy uploaded files into repository.
+ for _, upload := range uploads {
+ tmpPath := upload.LocalPath()
+ targetPath := path.Join(dirPath, upload.Name)
+ if !com.IsFile(tmpPath) {
+ continue
+ }
+
+ if err = com.Copy(tmpPath, targetPath); err != nil {
+ return fmt.Errorf("Copy: %v", err)
+ }
+ }
+
+ if err = git.AddChanges(localPath, true); err != nil {
+ return fmt.Errorf("git add --all: %v", err)
+ } else if err = git.CommitChanges(localPath, git.CommitChangesOptions{
+ Committer: doer.NewGitSig(),
+ Message: opts.Message,
+ }); err != nil {
+ return fmt.Errorf("CommitChanges: %v", err)
+ } else if err = git.Push(localPath, "origin", opts.NewBranch); err != nil {
+ return fmt.Errorf("git push origin %s: %v", opts.NewBranch, err)
+ }
+
+ gitRepo, err := git.OpenRepository(repo.RepoPath())
+ if err != nil {
+ log.Error(4, "OpenRepository: %v", err)
+ return nil
+ }
+ commit, err := gitRepo.GetBranchCommit(opts.NewBranch)
+ if err != nil {
+ log.Error(4, "GetBranchCommit [branch: %s]: %v", opts.NewBranch, err)
+ return nil
+ }
+
+ // Simulate push event.
+ pushCommits := &PushCommits{
+ Len: 1,
+ Commits: []*PushCommit{CommitToPushCommit(commit)},
+ }
+ if err := CommitRepoAction(CommitRepoActionOptions{
+ PusherName: doer.Name,
+ RepoOwnerID: repo.MustOwner().ID,
+ RepoName: repo.Name,
+ RefFullName: git.BRANCH_PREFIX + opts.NewBranch,
+ OldCommitID: opts.LastCommitID,
+ NewCommitID: commit.ID.String(),
+ Commits: pushCommits,
+ }); err != nil {
+ log.Error(4, "CommitRepoAction: %v", err)
+ return nil
+ }
+
+ return DeleteUploads(uploads...)
+}