Skip to content

Commit

Permalink
refactoring: rename to Session.OutputName
Browse files Browse the repository at this point in the history
  • Loading branch information
vbauerster committed Jul 23, 2024
1 parent bea835d commit 35a0e56
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 36 deletions.
34 changes: 17 additions & 17 deletions getparty.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,12 @@ func (cmd *Cmd) Run(args []string, version, commit string) (err error) {
}
ctx, cancel := context.WithCancel(cmd.Ctx)
p.ctx = ctx
p.order = i + 1
p.cancel = cancel
p.firstHttp200 = firstHttp200
p.partialOK = partialOK
p.order = i + 1
p.name = fmt.Sprintf("P%02d", p.order)
p.sessionOutputName = session.OutputFileName
p.sessionOutputName = session.OutputName
p.name = fmt.Sprintf("P%02d", i+1)
p.maxTry = cmd.options.MaxRetry
p.progress = progress
p.totalBarIncr = totalBarIncr
Expand Down Expand Up @@ -344,9 +344,9 @@ func (cmd Cmd) makeSessionHandler(session *Session, progress *mpb.Progress) func
session.Elapsed += time.Since(start)
var name string
if isPanic {
name = session.OutputFileName + ".panic"
name = session.OutputName + ".panic"
} else {
name = session.OutputFileName + ".json"
name = session.OutputName + ".json"
}
err := session.dumpState(name)
if err != nil {
Expand All @@ -361,7 +361,7 @@ func (cmd Cmd) makeSessionHandler(session *Session, progress *mpb.Progress) func
}
} else {
log = func() {
cmd.loggers[INFO].Printf("%q saved [%d/%d]", session.OutputFileName, session.ContentLength, total)
cmd.loggers[INFO].Printf("%q saved [%d/%d]", session.OutputName, session.ContentLength, total)
}
}
}
Expand Down Expand Up @@ -458,7 +458,7 @@ func (cmd *Cmd) getState(userinfo *url.Userinfo, client *http.Client, args []str
if err != nil {
return nil, err
}
state := scratch.OutputFileName + ".json"
state := scratch.OutputName + ".json"
if _, err := os.Stat(state); err == nil {
cmd.options.JSONFileName = state
} else if errors.Is(err, os.ErrNotExist) {
Expand All @@ -468,7 +468,7 @@ func (cmd *Cmd) getState(userinfo *url.Userinfo, client *http.Client, args []str
return nil, err
}
if exist {
err = cmd.overwriteIfConfirmed(scratch.OutputFileName)
err = cmd.overwriteIfConfirmed(scratch.OutputName)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -600,15 +600,15 @@ func (cmd Cmd) follow(client *http.Client, rawURL string) (session *Session, err
}

session = &Session{
location: location,
URL: rawURL,
OutputFileName: name,
ContentMD5: resp.Header.Get(hContentMD5),
AcceptRanges: resp.Header.Get(hAcceptRanges),
ContentType: resp.Header.Get(hContentType),
StatusCode: resp.StatusCode,
ContentLength: resp.ContentLength,
Redirected: redirected,
location: location,
URL: rawURL,
OutputName: name,
ContentMD5: resp.Header.Get(hContentMD5),
AcceptRanges: resp.Header.Get(hAcceptRanges),
ContentType: resp.Header.Get(hContentType),
StatusCode: resp.StatusCode,
ContentLength: resp.ContentLength,
Redirected: redirected,
}

return false, resp.Body.Close()
Expand Down
4 changes: 2 additions & 2 deletions part.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ type Part struct {
Elapsed time.Duration

ctx context.Context
sessionOutputName string
name string
order int
maxTry uint
progress *mpb.Progress
Expand All @@ -45,6 +43,8 @@ type Part struct {
partialOK func()
firstHttp200 chan int
httpStatusOK bool
sessionOutputName string
name string
}

func (p Part) newBar(curTry *uint32, single bool, msgCh chan string) (*mpb.Bar, error) {
Expand Down
34 changes: 17 additions & 17 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ import (

// Session represents download session state
type Session struct {
location string
URL string
OutputFileName string
ContentMD5 string
AcceptRanges string
ContentType string
StatusCode int
ContentLength int64
Redirected bool
Elapsed time.Duration
HeaderMap map[string]string
Parts []*Part
location string
URL string
OutputName string
ContentMD5 string
AcceptRanges string
ContentType string
StatusCode int
ContentLength int64
Redirected bool
Elapsed time.Duration
HeaderMap map[string]string
Parts []*Part
}

func (s *Session) calcParts(parts uint) error {
Expand Down Expand Up @@ -102,23 +102,23 @@ func (s Session) summary(loggers [LEVELS]*log.Logger) {
}
}
if len(s.Parts) != 0 {
loggers[INFO].Printf("Saving to: %q", s.OutputFileName)
loggers[INFO].Printf("Saving to: %q", s.OutputName)
}
if !s.isResumable() {
loggers[WARN].Println("Session is not resumable")
}
}

func (s Session) isOutputFileExist() (bool, error) {
stat, err := os.Stat(s.OutputFileName)
stat, err := os.Stat(s.OutputName)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return false, nil
}
return false, err
}
if stat.IsDir() {
return true, errors.Wrapf(os.ErrInvalid, "%q is a directory", s.OutputFileName)
return true, errors.Wrapf(os.ErrInvalid, "%q is a directory", s.OutputName)
}
return true, nil
}
Expand All @@ -139,7 +139,7 @@ func (s Session) checkSizeOfEachPart() error {
continue
}
p.order = i + 1
p.sessionOutputName = s.OutputFileName
p.sessionOutputName = s.OutputName
stat, err := os.Stat(p.fileName())
if err != nil {
return err
Expand Down Expand Up @@ -190,7 +190,7 @@ func (s Session) concatenateParts(progress *mpb.Progress) (err error) {
return err
}

dst, err := os.OpenFile(s.OutputFileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
dst, err := os.OpenFile(s.OutputName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
Expand Down

0 comments on commit 35a0e56

Please sign in to comment.