|
|
@ -11,6 +11,7 @@ import ( |
|
|
|
"github.com/h2non/filetype" |
|
|
|
"io" |
|
|
|
"log" |
|
|
|
"strings" |
|
|
|
"time" |
|
|
|
) |
|
|
|
|
|
|
@ -51,10 +52,12 @@ func (s *FileService) List(ctx context.Context, filter models.FileFilter) ([]*mo |
|
|
|
if filter.Public != nil { |
|
|
|
if *filter.Public == false { |
|
|
|
if filter.Author == nil || *filter.Author == "" { |
|
|
|
return nil, ErrPrivateNoAuthor |
|
|
|
if token == nil { |
|
|
|
return nil, ErrUnauthorized |
|
|
|
} |
|
|
|
|
|
|
|
if !token.PermittedUser(*filter.Author, "member", "file.list") { |
|
|
|
filter.Author = &token.UserID |
|
|
|
} else if !token.PermittedUser(*filter.Author, "member", "file.list") { |
|
|
|
return nil, ErrUnauthorized |
|
|
|
} |
|
|
|
} |
|
|
@ -125,17 +128,53 @@ func (s *FileService) Edit(ctx context.Context, id string, name *string, public |
|
|
|
|
|
|
|
err = s.authService.CheckPermission(ctx, "edit", file) |
|
|
|
if err != nil { |
|
|
|
if !file.Public { |
|
|
|
return nil, repositories.ErrNotFound |
|
|
|
} |
|
|
|
|
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
return s.files.Update(ctx, *file, models.FileUpdate{ |
|
|
|
file, err = s.files.Update(ctx, *file, models.FileUpdate{ |
|
|
|
Name: name, |
|
|
|
Public: public, |
|
|
|
}) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
s.changeService.Submit(ctx, models.ChangeModelFile, "edit", file.Public, changekeys.Listed(file), file) |
|
|
|
|
|
|
|
return file, nil |
|
|
|
} |
|
|
|
|
|
|
|
func (s *FileService) Remove(ctx context.Context, id string) (*models.File, error) { |
|
|
|
file, err := s.files.Find(ctx, id) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
err = s.authService.CheckPermission(ctx, "remove", file) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
if file.Kind == "upload" { |
|
|
|
split := strings.Split(file.URL, "/") |
|
|
|
name := split[len(split)-1] |
|
|
|
|
|
|
|
if strings.HasPrefix(name, "U") { |
|
|
|
err := s.space.RemoveFile(name) |
|
|
|
if err != nil { |
|
|
|
log.Printf("Failed to remove file %s for id %s: %s", name, id, err) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
err = s.files.Delete(ctx, *file) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
s.changeService.Submit(ctx, models.ChangeModelFile, "remove", file.Public, changekeys.Listed(file), file) |
|
|
|
|
|
|
|
return file, nil |
|
|
|
} |
|
|
|
|
|
|
|
// concatReader is a quick and dirty reader for reading the head and the file.
|
|
|
|