Skip to content

Commit

Permalink
fix(up): remove impl
Browse files Browse the repository at this point in the history
  • Loading branch information
iyear committed Nov 26, 2023
1 parent 9f6512e commit af2415f
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 34 deletions.
68 changes: 42 additions & 26 deletions app/up/iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package up

import (
"context"
"io"
"os"
"path/filepath"

Expand All @@ -20,23 +19,25 @@ type file struct {
}

type iter struct {
files []*file
to peers.Peer
photo bool
files []*file
to peers.Peer
photo bool
remove bool

cur int
err error
file *uploader.Elem
}

func newIter(files []*file, to peers.Peer, photo bool) *iter {
func newIter(files []*file, to peers.Peer, photo, remove bool) *iter {
return &iter{
files: files,
cur: 0,
err: nil,
file: nil,
to: to,
photo: photo,
files: files,
cur: 0,
err: nil,
file: nil,
to: to,
photo: photo,
remove: remove,
}
}

Expand Down Expand Up @@ -73,29 +74,32 @@ func (i *iter) Next(ctx context.Context) bool {
return false
}

var thumb io.ReadSeekCloser = nopReadSeekCloser{}
var thumb uploader.File = nopFile{}
// has thumbnail
if cur.thumb != "" {
tMime, err := mimetype.DetectFile(cur.thumb)
if err != nil || !utils.Media.IsImage(tMime.String()) { // TODO(iyear): jpg only
i.err = errors.Wrapf(err, "invalid thumbnail file: %v", cur.thumb)
return false
}
thumb, err = os.Open(cur.thumb)
thumbFile, err := os.Open(cur.thumb)
if err != nil {
i.err = errors.Wrap(err, "open thumbnail file")
return false
}

thumb = uploaderFile{thumbFile}
}

i.file = &uploader.Elem{
File: f,
Thumb: thumb,
Name: filepath.Base(f.Name()),
MIME: fMime.String(),
Size: stat.Size(),
To: i.to,
Photo: i.photo,
File: uploaderFile{f},
Thumb: thumb,
Name: filepath.Base(f.Name()),
MIME: fMime.String(),
Size: stat.Size(),
To: i.to,
Photo: i.photo,
Remove: i.remove,
}

return true
Expand All @@ -109,16 +113,28 @@ func (i *iter) Err() error {
return i.err
}

type nopReadSeekCloser struct{}
type nopFile struct{}

func (nopFile) Read(_ []byte) (n int, err error) {
return 0, errors.New("nopFile")
}

func (nopReadSeekCloser) Read(_ []byte) (n int, err error) {
return 0, errors.New("nopReadSeekCloser")
func (nopFile) Seek(_ int64, _ int) (int64, error) {
return 0, errors.New("nopFile")
}

func (nopReadSeekCloser) Seek(_ int64, _ int) (int64, error) {
return 0, errors.New("nopReadSeekCloser")
func (nopFile) Close() error {
return nil
}

func (nopReadSeekCloser) Close() error {
func (nopFile) Remove() error {
return nil
}

type uploaderFile struct {
*os.File
}

func (u uploaderFile) Remove() error {
return os.Remove(u.Name())
}
2 changes: 1 addition & 1 deletion app/up/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func Run(ctx context.Context, opts *Options) error {
Client: pool.Default(ctx),
PartSize: viper.GetInt(consts.FlagPartSize),
Threads: viper.GetInt(consts.FlagThreads),
Iter: newIter(files, to, opts.Photo),
Iter: newIter(files, to, opts.Photo, opts.Remove),
Progress: newProgress(upProgress),
}

Expand Down
20 changes: 13 additions & 7 deletions pkg/uploader/iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@ type Iter interface {
Err() error
}

type File interface {
io.ReadSeekCloser
Remove() error
}

type Elem struct {
File io.ReadSeekCloser
Thumb io.ReadSeekCloser
Name string
MIME string
Size int64
To peers.Peer
Photo bool
File File
Thumb File
Name string
MIME string
Size int64
To peers.Peer
Photo bool
Remove bool
}
7 changes: 7 additions & 0 deletions pkg/uploader/uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ func (u *Uploader) Upload(ctx context.Context, limit int) error {
}

func (u *Uploader) upload(ctx context.Context, elem *Elem) (rerr error) {
defer func() {
if rerr == nil && elem.Remove {
multierr.AppendInto(&rerr, elem.File.Remove())
multierr.AppendInto(&rerr, elem.Thumb.Remove())
}
}()

defer multierr.AppendInvoke(&rerr, multierr.Close(elem.File))
defer multierr.AppendInvoke(&rerr, multierr.Close(elem.Thumb))

Expand Down

0 comments on commit af2415f

Please sign in to comment.