Skip to content
Open
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
53 changes: 53 additions & 0 deletions lib/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package lib

import (
"fmt"
"io/fs"
"net"
"net/http"
"net/url"
Expand Down Expand Up @@ -52,6 +53,8 @@ type Command struct {
options OptionMapType
configOptions OptionMapType
inputKeySecret string
isFilter bool
bFilter bool
}

// Commander is the interface of all commands
Expand Down Expand Up @@ -846,6 +849,56 @@ func (cmd *Command) getOSSTagging(strTagging string) ([]oss.Tag, error) {
return tags, nil
}

func (cmd *Command) checkFilter() error {
startTime, _ := GetInt(OptionStartTime, cmd.options)
endTime, _ := GetInt(OptionEndTime, cmd.options)
maxSize, _ := GetInt(OptionMaxSize, cmd.options)
minSize, _ := GetInt(OptionMinSize, cmd.options)

if startTime != 0 || endTime != 0 || maxSize != 0 || minSize != 0 {
cmd.isFilter = true
}
if endTime > 0 && startTime > endTime {
return fmt.Errorf("--start-time %d is larger than --end-time %d", startTime, endTime)
}

if maxSize > 0 && minSize > maxSize {
return fmt.Errorf("--min-size %d is larger than --max-size %d", minSize, maxSize)
}
return nil
}

func (cmd *Command) filterLocalFile(fileInfo fs.FileInfo) bool {
filterMap := cmd.getFilterMap()
return CheckLocalFile(filterMap, fileInfo)
}

func (cmd *Command) filterObject(object oss.ObjectProperties) bool {
filterMap := cmd.getFilterMap()
return CheckObject(filterMap, object)
}

func (cmd *Command) getFilterMap() map[string]int64 {
startTime, _ := GetInt(OptionStartTime, cmd.options)
endTime, _ := GetInt(OptionEndTime, cmd.options)
maxSize, _ := GetInt(OptionMaxSize, cmd.options)
minSize, _ := GetInt(OptionMinSize, cmd.options)
filterMap := map[string]int64{}
if endTime != 0 {
filterMap[OptionEndTime] = endTime
}
if startTime != 0 {
filterMap[OptionStartTime] = startTime
}
if maxSize != 0 {
filterMap[OptionMaxSize] = maxSize
}
if minSize != 0 {
filterMap[OptionMinSize] = minSize
}
return filterMap
}

// GetAllCommands returns all commands list
func GetAllCommands() []interface{} {
return []interface{}{
Expand Down
205 changes: 205 additions & 0 deletions lib/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2928,3 +2928,208 @@ func (s *OssutilCommandSuite) TestCommandObjectProducer(c *C) {
c.Assert(true, Equals, false)
}
}

//Test FilterLocalFile
func (s *OssutilCommandSuite) TestFilterLocalFileWithSizeAndTime(c *C) {
str := int64(0)
minSize := int64(10)
maxSize := int64(20)
maxTime := strconv.Itoa(int(time.Now().Unix()))
minTime := strconv.Itoa(int(time.Now().Add(-90 * time.Second).Unix()))
minSize1 := int64(2)
f, _ := os.Stat(uploadFileName)
testLogger.Println(f.Size())
testLogger.Println(f.ModTime())

copyCommand.command.options = OptionMapType{
OptionMinSize: &minSize,
}

next := copyCommand.command.filterLocalFile(f)
c.Assert(next, Equals, false)

copyCommand.command.options = OptionMapType{
OptionMinSize: &minSize1,
}

next = copyCommand.command.filterLocalFile(f)
c.Assert(next, Equals, true)

copyCommand.command.options = OptionMapType{
OptionMaxSize: &minSize1,
OptionMinSize: &str,
}

next = copyCommand.command.filterLocalFile(f)
c.Assert(next, Equals, false)

copyCommand.command.options = OptionMapType{
OptionMaxSize: &minSize,
OptionMinSize: &str,
}

next = copyCommand.command.filterLocalFile(f)
c.Assert(next, Equals, true)

copyCommand.command.options = OptionMapType{
OptionMinSize: &minSize,
OptionMaxSize: &maxSize,
}

next = copyCommand.command.filterLocalFile(f)
c.Assert(next, Equals, false)

copyCommand.command.options = OptionMapType{
OptionMinSize: &minSize1,
OptionMaxSize: &maxSize,
}
next = copyCommand.command.filterLocalFile(f)
c.Assert(next, Equals, true)

copyCommand.command.options = OptionMapType{
OptionStartTime: &minTime,
OptionMinSize: &str,
OptionMaxSize: &str,
}
next = copyCommand.command.filterLocalFile(f)
c.Assert(next, Equals, true)

copyCommand.command.options = OptionMapType{
OptionStartTime: &maxTime,
OptionMinSize: &str,
OptionMaxSize: &str,
}
next = copyCommand.command.filterLocalFile(f)
c.Assert(next, Equals, false)

copyCommand.command.options = OptionMapType{
OptionEndTime: &maxTime,
OptionMinSize: &str,
OptionMaxSize: &str,
OptionStartTime: &str,
}
next = copyCommand.command.filterLocalFile(f)
c.Assert(next, Equals, true)

copyCommand.command.options = OptionMapType{
OptionEndTime: &maxTime,
OptionStartTime: &minTime,
OptionMinSize: &str,
OptionMaxSize: &str,
}
next = copyCommand.command.filterLocalFile(f)
c.Assert(next, Equals, true)

copyCommand.command.options = OptionMapType{
OptionEndTime: &maxTime,
OptionStartTime: &minTime,
OptionMinSize: &minSize1,
OptionMaxSize: &maxSize,
}
next = copyCommand.command.filterLocalFile(f)
c.Assert(next, Equals, true)
}

//Test FilterObject
func (s *OssutilCommandSuite) TestFilterObjectWithSizeAndTime(c *C) {
str := int64(0)
minSize := int64(10)
maxSize := int64(20)
maxTime := strconv.Itoa(int(time.Now().Unix()))
minTime := strconv.Itoa(int(time.Now().Add(-90 * time.Second).Unix()))
minSize1 := int64(2)

var f oss.ObjectProperties
f.Size = 3
f.LastModified = time.Now().Add(-50 * time.Second)

copyCommand.command.options = OptionMapType{
OptionMinSize: &minSize,
}

next := copyCommand.command.filterObject(f)
c.Assert(next, Equals, false)

copyCommand.command.options = OptionMapType{
OptionMinSize: &minSize1,
}

testLogger.Println(copyCommand.command.options)

next = copyCommand.command.filterObject(f)
c.Assert(next, Equals, true)

copyCommand.command.options = OptionMapType{
OptionMaxSize: &minSize1,
OptionMinSize: &str,
}

next = copyCommand.command.filterObject(f)
c.Assert(next, Equals, false)

copyCommand.command.options = OptionMapType{
OptionMaxSize: &minSize,
OptionMinSize: &str,
}

next = copyCommand.command.filterObject(f)
c.Assert(next, Equals, true)

copyCommand.command.options = OptionMapType{
OptionMinSize: &minSize,
OptionMaxSize: &maxSize,
}

next = copyCommand.command.filterObject(f)
c.Assert(next, Equals, false)

copyCommand.command.options = OptionMapType{
OptionMinSize: &minSize1,
OptionMaxSize: &maxSize,
}
next = copyCommand.command.filterObject(f)
c.Assert(next, Equals, true)

copyCommand.command.options = OptionMapType{
OptionStartTime: &minTime,
OptionMinSize: &str,
OptionMaxSize: &str,
}
next = copyCommand.command.filterObject(f)
c.Assert(next, Equals, true)

copyCommand.command.options = OptionMapType{
OptionStartTime: &maxTime,
OptionMinSize: &str,
OptionMaxSize: &str,
}
next = copyCommand.command.filterObject(f)
c.Assert(next, Equals, false)

copyCommand.command.options = OptionMapType{
OptionEndTime: &maxTime,
OptionMinSize: &str,
OptionMaxSize: &str,
OptionStartTime: &str,
}
next = copyCommand.command.filterObject(f)
c.Assert(next, Equals, true)

copyCommand.command.options = OptionMapType{
OptionEndTime: &maxTime,
OptionStartTime: &minTime,
OptionMinSize: &str,
OptionMaxSize: &str,
}
next = copyCommand.command.filterObject(f)
c.Assert(next, Equals, true)

copyCommand.command.options = OptionMapType{
OptionEndTime: &maxTime,
OptionStartTime: &minTime,
OptionMinSize: &minSize1,
OptionMaxSize: &maxSize,
}
next = copyCommand.command.filterObject(f)
c.Assert(next, Equals, true)
}
2 changes: 2 additions & 0 deletions lib/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ const (
OptionQueryParam = "queryParam"
OptionForcePathStyle = "forcePathStyle"
OptionRuntime = "runtime"
OptionMaxSize = "maxSize"
OptionMinSize = "minSize"
)

// the elements show in stat object
Expand Down
Loading