-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
124 lines (97 loc) · 3.13 KB
/
list.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package onearchiver
// TODO proper error handling
import (
"fmt"
"path/filepath"
"sort"
"strings"
)
func sortFiles(list []ArchiveFileInfo, orderBy ArchiveOrderBy, orderDir ArchiveOrderDir) []ArchiveFileInfo {
if orderDir == OrderDirNone {
return list
}
switch orderBy {
case OrderByFullPath:
return sortPath(list, orderDir)
case OrderByName:
sort.SliceStable(list, func(i, j int) bool {
if orderDir == OrderDirDesc {
return list[i].Name > list[j].Name
}
return list[i].Name < list[j].Name
})
case OrderByModTime:
sort.SliceStable(list, func(i, j int) bool {
if orderDir == OrderDirDesc {
return list[i].ModTime.After(list[j].ModTime)
}
return list[i].ModTime.Before(list[j].ModTime)
})
case OrderBySize:
sort.SliceStable(list, func(i, j int) bool {
if orderDir == OrderDirDesc {
return list[i].Size > list[j].Size
}
return list[i].Size < list[j].Size
})
}
return list
}
func getFilteredFiles(fileInfo ArchiveFileInfo, listDirectoryPath string, recursive bool) (include bool) {
isInPath := strings.HasPrefix(fileInfo.FullPath, listDirectoryPath)
if isInPath {
// dont return the directory path if it's listDirectoryPath. This will make sure that only files and sub directories are returned
if listDirectoryPath == fileInfo.FullPath {
return false
}
// if recursive mode is true return all files and subdirectories under the filtered path
if recursive {
return true
}
slashSplitListDirectoryPath := strings.Split(listDirectoryPath, PathSep)
slashSplitListDirectoryPathLength := len(slashSplitListDirectoryPath)
slashSplitFullPath := strings.Split(fileInfo.FullPath, PathSep)
slashSplitFullPathLength := len(slashSplitFullPath)
// if directory allow an extra '/' to figure out the subdirectory
if fileInfo.IsDir && slashSplitFullPathLength < slashSplitListDirectoryPathLength+2 {
return true
}
if !fileInfo.IsDir && slashSplitFullPathLength < slashSplitListDirectoryPathLength+1 {
return true
}
}
return false
}
func GetArchiveFileList(meta *ArchiveMeta, read *ArchiveRead) ([]ArchiveFileInfo, error) {
_meta := *meta
_read := *read
var arcObj ArchiveReader
// check whether the archive is encrypted
// if yes, check whether the password is valid
iae, err := IsArchiveEncrypted(meta)
if err != nil {
return nil, err
}
/// if archive is encrypted and if password field is empty
/// then return 'password is required' error
if iae.IsEncrypted && len(_meta.Password) < 1 {
return nil, fmt.Errorf("password is required")
}
/// if archive is encrypted and if the password is invalid
/// then return 'invalid password' error
if iae.IsEncrypted && !iae.IsValidPassword {
return nil, fmt.Errorf("invalid password")
}
ext := filepath.Ext(meta.Filename)
// add a trailing slash to [listDirectoryPath] if missing
if _read.ListDirectoryPath != "" && !strings.HasSuffix(_read.ListDirectoryPath, PathSep) {
_read.ListDirectoryPath = fmt.Sprintf("%s%s", _read.ListDirectoryPath, PathSep)
}
switch ext {
case ".zip":
arcObj = zipArchive{meta: _meta, read: _read}
default:
arcObj = commonArchive{meta: _meta, read: _read}
}
return arcObj.list()
}