Web editor: support upload files

This commit is contained in:
Unknwon 2016-08-30 05:07:50 -07:00
parent 7c31f235da
commit 643142acab
24 changed files with 503 additions and 600 deletions

View file

@ -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
}
// __________ .__
// \______ \____________ ____ ____ | |__
// | | _/\_ __ \__ \ / \_/ ___\| | \