mirror of
https://github.com/go-gitea/gitea.git
synced 2024-11-21 01:14:31 +08:00
296814e887
* Use git plumbing for upload: #5621 repo_editor.go: UploadRepoFile
* Use git plumbing for upload: #5621 repo_editor.go: GetDiffPreview
* Use git plumbing for upload: #5621 repo_editor.go: DeleteRepoFile
* Use git plumbing for upload: #5621 repo_editor.go: UploadRepoFiles
* Move branch checkout functions out of repo_editor.go as they are no longer used there
* BUGFIX: The default permissions should be 100644
This is a change from the previous code but is more in keeping
with the default behaviour of git.
Signed-off-by: Andrew Thornton <art27@cantab.net>
* Standardise cleanUploadFilename to more closely match git
See verify_path in: 7f4e641693/read-cache.c (L951)
Signed-off-by: Andrew Thornton <art27@cantab.net>
* Redirect on bad paths
Signed-off-by: Andrew Thornton <art27@cantab.net>
* Refactor to move the uploading functions out to a module
Signed-off-by: Andrew Thornton <art27@cantab.net>
* Add LFS support
Signed-off-by: Andrew Thornton <art27@cantab.net>
* Update upload.go attribution header
Upload.go is essentially the remnants of repo_editor.go. The remaining code is essentially unchanged from the Gogs code, hence the Gogs attribution.
* Delete upload files after session committed
* Ensure that GIT_AUTHOR_NAME etc. are valid for git
see #5774
Signed-off-by: Andrew Thornton <art27@cantab.net>
* Add in test cases per @lafriks comment
* Add space between gitea and github imports
Signed-off-by: Andrew Thornton <art27@cantab.net>
* more examples in TestCleanUploadName
Signed-off-by: Andrew Thornton <art27@cantab.net>
* fix formatting
Signed-off-by: Andrew Thornton <art27@cantab.net>
* Set the SSH_ORIGINAL_COMMAND to ensure hooks are run
Signed-off-by: Andrew Thornton <art27@cantab.net>
* Switch off SSH_ORIGINAL_COMMAND
Signed-off-by: Andrew Thornton <art27@cantab.net>
156 lines
3.8 KiB
Go
156 lines
3.8 KiB
Go
// Copyright 2016 The Gogs Authors. All rights reserved.
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
// Use of this source code is governed by a MIT-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package models
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"mime/multipart"
|
|
"os"
|
|
"path"
|
|
|
|
"github.com/Unknwon/com"
|
|
gouuid "github.com/satori/go.uuid"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
)
|
|
|
|
// ____ ___ .__ .___ ___________.___.__
|
|
// | | \______ | | _________ __| _/ \_ _____/| | | ____ ______
|
|
// | | /\____ \| | / _ \__ \ / __ | | __) | | | _/ __ \ / ___/
|
|
// | | / | |_> > |_( <_> ) __ \_/ /_/ | | \ | | |_\ ___/ \___ \
|
|
// |______/ | __/|____/\____(____ /\____ | \___ / |___|____/\___ >____ >
|
|
// |__| \/ \/ \/ \/ \/
|
|
//
|
|
|
|
// 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
|
|
}
|
|
|
|
// GetUploadByUUID returns the Upload by UUID
|
|
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
|
|
}
|
|
|
|
// GetUploadsByUUIDs returns multiple uploads by UUIDS
|
|
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)
|
|
}
|
|
|
|
// DeleteUploads deletes multiple uploads
|
|
func DeleteUploads(uploads ...*Upload) (err error) {
|
|
if len(uploads) == 0 {
|
|
return nil
|
|
}
|
|
|
|
sess := x.NewSession()
|
|
defer sess.Close()
|
|
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)
|
|
}
|
|
|
|
if err = sess.Commit(); err != nil {
|
|
return 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 nil
|
|
}
|
|
|
|
// DeleteUploadByUUID deletes a upload by UUID
|
|
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 := DeleteUploads(upload); err != nil {
|
|
return fmt.Errorf("DeleteUpload: %v", err)
|
|
}
|
|
|
|
return nil
|
|
}
|