aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Polden <mpolden@mpolden.no>2018-07-26 14:09:46 +0200
committerMartin Polden <mpolden@mpolden.no>2018-07-26 14:09:46 +0200
commit616d23ab13699ffe2386af828b33852545ec82e5 (patch)
tree61c1fb7f0c5552939b0f743d112433047d686bfd
parentb6e2947373a52bee7d7d11bc52c024cb644f36d5 (diff)
Use first RAR when unpacking
-rw-r--r--unpacker/unpacker.go19
-rw-r--r--unpacker/unpacker_test.go46
2 files changed, 50 insertions, 15 deletions
diff --git a/unpacker/unpacker.go b/unpacker/unpacker.go
index 6aa0f36..c7e7099 100644
--- a/unpacker/unpacker.go
+++ b/unpacker/unpacker.go
@@ -5,12 +5,15 @@ import (
"io"
"os"
"path/filepath"
+ "regexp"
"github.com/mpolden/sfv"
"github.com/nwaples/rardecode"
"github.com/pkg/errors"
)
+var rarPartRE = regexp.MustCompile(`\.part0*(\d+)\.rar$`)
+
type unpacker struct {
SFV *sfv.SFV
Dir string
@@ -22,7 +25,7 @@ func New(dir string) (*unpacker, error) {
if err != nil {
return nil, err
}
- rar, err := findRAR(sfv)
+ rar, err := findFirstRAR(sfv)
if err != nil {
return nil, err
}
@@ -33,13 +36,19 @@ func New(dir string) (*unpacker, error) {
}, nil
}
-func isRAR(name string) bool {
- return filepath.Ext(name) == ".rar"
+func isRAR(name string) bool { return filepath.Ext(name) == ".rar" }
+
+func isFirstRAR(name string) bool {
+ m := rarPartRE.FindStringSubmatch(name)
+ if len(m) == 2 {
+ return m[1] == "1"
+ }
+ return isRAR(name)
}
-func findRAR(s *sfv.SFV) (string, error) {
+func findFirstRAR(s *sfv.SFV) (string, error) {
for _, c := range s.Checksums {
- if isRAR(c.Path) {
+ if isFirstRAR(c.Path) {
return c.Path, nil
}
}
diff --git a/unpacker/unpacker_test.go b/unpacker/unpacker_test.go
index 25fa3c0..3f0e3ee 100644
--- a/unpacker/unpacker_test.go
+++ b/unpacker/unpacker_test.go
@@ -9,17 +9,43 @@ import (
"github.com/mpolden/sfv"
)
-func TestFindRAR(t *testing.T) {
- sfv := &sfv.SFV{Checksums: []sfv.Checksum{
- sfv.Checksum{Path: "foo.r00"},
- sfv.Checksum{Path: "foo.rar"},
- }}
- archive, err := findRAR(sfv)
- if err != nil {
- t.Fatal(err)
+func TestFindFirstRAR(t *testing.T) {
+ var tests = []struct {
+ sfv *sfv.SFV
+ firstRAR string
+ }{
+ {&sfv.SFV{Checksums: []sfv.Checksum{
+ {Path: "foo.r00"},
+ {Path: "foo.rar"},
+ {Path: "foo.r01"},
+ }}, "foo.rar"},
+ {&sfv.SFV{Checksums: []sfv.Checksum{
+ {Path: "foo.part3.rar"},
+ {Path: "foo.part2.rar"},
+ {Path: "foo.part1.rar"},
+ }}, "foo.part1.rar"},
+ {&sfv.SFV{Checksums: []sfv.Checksum{
+ {Path: "foo.part03.rar"},
+ {Path: "foo.part01.rar"},
+ {Path: "foo.part10.rar"},
+ {Path: "foo.part02.rar"},
+ }}, "foo.part01.rar"},
+ {&sfv.SFV{Checksums: []sfv.Checksum{
+ {Path: "foo.part003.rar"},
+ {Path: "foo.part100.rar"},
+ {Path: "foo.part001.rar"},
+ {Path: "foo.part002.rar"},
+ }}, "foo.part001.rar"},
}
- if want := "foo.rar"; archive != want {
- t.Errorf("want %q, got %q", want, archive)
+
+ for i, tt := range tests {
+ got, err := findFirstRAR(tt.sfv)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if want := tt.firstRAR; want != got {
+ t.Errorf("#%d: want %q, got %q", i, want, got)
+ }
}
}