Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(processor): rewrite hardlink success logic #150

Merged
merged 5 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions internal/domain/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const (
StatusSuccessfulMatch StatusCode = 250
StatusSuccessfulHardlink StatusCode = 250
StatusFailedHardlink StatusCode = 440
StatusFailedMatchToTorrentEps StatusCode = 445
StatusClientNotFound StatusCode = 472
StatusGetClientError StatusCode = 471
StatusDecodingError StatusCode = 470
Expand Down Expand Up @@ -67,6 +68,8 @@ func (s StatusCode) String() string {
return "successful match"
case StatusFailedHardlink:
return "could not create hardlinks"
case StatusFailedMatchToTorrentEps:
return "could not match episodes to files in pack"
case StatusClientNotFound:
return "could not find client in config"
case StatusGetClientError:
Expand Down Expand Up @@ -120,6 +123,7 @@ var NotificationStatusMap = map[string][]StatusCode{
},
NotificationLevelError: {
StatusFailedHardlink,
StatusFailedMatchToTorrentEps,
StatusClientNotFound,
StatusGetClientError,
StatusDecodingError,
Expand Down
34 changes: 19 additions & 15 deletions internal/http/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,19 +338,18 @@ func (p *processor) processSeasonPack() (domain.StatusCode, error) {
return domain.StatusSuccessfulMatch, nil
}

hardlinkRespSet := make(map[domain.StatusCode]bool)
successfulHardlink := false

for _, match := range matches {
if err := utils.CreateHardlink(match.clientEpPath, match.announcedEpPath); err != nil {
p.log.Error().Err(err).Msgf("error creating hardlink: %s", match.clientEpPath)
hardlinkRespSet[domain.StatusFailedHardlink] = true
continue
}
p.log.Log().Msgf("created hardlink: source(%s), target(%s)", match.clientEpPath, match.announcedEpPath)
hardlinkRespSet[domain.StatusSuccessfulHardlink] = true
successfulHardlink = true
}

if !hardlinkRespSet[domain.StatusSuccessfulHardlink] {
if !successfulHardlink {
return domain.StatusFailedHardlink, domain.StatusFailedHardlink.Error()
}

Expand Down Expand Up @@ -450,7 +449,8 @@ func (p *processor) parseTorrent() (domain.StatusCode, error) {
return domain.StatusNoMatches, domain.StatusNoMatches.Error()
}

hardlinkRespSet := make(map[domain.StatusCode]bool)
successfulEpMatch := false
successfulHardlink := false

var matchedEpPath string
var matchErr error
Expand All @@ -471,25 +471,29 @@ func (p *processor) parseTorrent() (domain.StatusCode, error) {
continue
}
targetEpPath = filepath.Join(targetPackDir, matchedEpPath)
successfulEpMatch = true

if err = utils.CreateHardlink(match.clientEpPath, targetEpPath); err != nil {
p.log.Error().Err(err).Msgf("error creating hardlink: %s", match.clientEpPath)
continue
}
p.log.Log().Msgf("created hardlink: source(%s), target(%s)", match.clientEpPath, targetEpPath)
successfulHardlink = true

break
}
if matchErr != nil {
p.log.Error().Err(matchErr).Msgf("error matching episode to file in pack, skipping hardlink: %s",
p.log.Error().Msgf("error matching episode to file in pack, skipping hardlink: %s",
filepath.Base(match.clientEpPath))
hardlinkRespSet[domain.StatusFailedHardlink] = true
continue
}
}

if err = utils.CreateHardlink(match.clientEpPath, targetEpPath); err != nil {
p.log.Error().Err(err).Msgf("error creating hardlink: %s", match.clientEpPath)
hardlinkRespSet[domain.StatusFailedHardlink] = true
continue
}
p.log.Log().Msgf("created hardlink: source(%s), target(%s)", match.clientEpPath, targetEpPath)
hardlinkRespSet[domain.StatusSuccessfulHardlink] = true
if !successfulEpMatch {
return domain.StatusFailedMatchToTorrentEps, domain.StatusFailedMatchToTorrentEps.Error()
}

if !hardlinkRespSet[domain.StatusSuccessfulHardlink] {
if !successfulHardlink {
return domain.StatusFailedHardlink, domain.StatusFailedHardlink.Error()
}

Expand Down
Loading