-
Notifications
You must be signed in to change notification settings - Fork 3
/
extract.go
50 lines (42 loc) · 1.01 KB
/
extract.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package extract
import (
"fmt"
"io"
"os"
"path/filepath"
"github.com/tympanix/supper/types"
)
// ErrNotArchive is an error which indicates the file was not an archive
type ErrNotArchive struct {
error
}
// IsNotArchive returns true if the error is of ErrNotArchive type
func IsNotArchive(err error) bool {
if err == nil {
return false
}
_, ok := err.(*ErrNotArchive)
return ok
}
// MediaFromArchive representa a media item which is located inside a
// compressed archive
type MediaFromArchive struct {
os.FileInfo
types.Media
io.ReadCloser
}
// OpenMediaArchive opens the file as an archive and exposes the media files
// within. If the file is not recognized as any of the known archive formats,
// an error is returned
func OpenMediaArchive(path string) (types.MediaArchive, error) {
ext := filepath.Ext(path)
switch ext {
case ".zip":
return NewZipArchive(path)
case ".rar":
return NewRarArchive(path)
}
return nil, &ErrNotArchive{
fmt.Errorf("%s: not of any known archive formats", path),
}
}