aacb5d6202487b74dd1a9cb2923e12eff100174b — Louis Solofrizzo 29 days ago dba5389 master
sorter: Add the ability to pass custom commands for video and gif sorting

Signed-off-by: Louis Solofrizzo <lsolofrizzo@online.net>
3 files changed, 62 insertions(+), 3 deletions(-)

R worker/{example-conf.yml => -conf.yml}
M sorter/config.go
M sorter/main.go
R worker/example-conf.yml => example-conf.yml +4 -0
@@ 38,3 38,7 @@ static: /usr/share/kart/static
    database: /var/db/kart-web.db.sqlite
    desc: Some description
+ 
+ sorter:
+   video: vlc %s --play-and-exit
+   gif: sxiv -a %s

M sorter/config.go => sorter/config.go +5 -0
@@ 19,6 19,11 @@ Scripts struct {
  		Directory string `yaml:"directory"`
  	} `yaml:"scripts"`
+ 
+ 	Sorter struct {
+ 		Video string `yaml:"video"`
+ 		Gif   string `yaml:"gif"`
+ 	} `yaml:"sorter"`
  }
  
  var Config WorkerConfig

M sorter/main.go => sorter/main.go +53 -3
@@ 44,9 44,52 @@   	fmt.Printf("Launching video sorter...\n")
  	for object := range client.ListObjects(Config.S3.Bucket, Config.S3.Prefix, false, doneCh) {
- 		if strings.HasSuffix(object.Key, ".mp4") || strings.HasSuffix(object.Key, ".gif") {
+ 		if strings.HasSuffix(object.Key, ".mp4") {
  		begin:
- 			exec.Command("vlc", "https://"+Config.S3.Endpoint+"/"+Config.S3.Bucket+"/"+object.Key, "--play-and-exit").Output()
+ 			cmd := fmt.Sprintf(Config.Sorter.Video, "https://"+Config.S3.Endpoint+"/"+Config.S3.Bucket+"/"+object.Key)
+ 			cmd_split := strings.Split(cmd, " ")
+ 			exec.Command(cmd_split[0], cmd_split[1:]...).Output()
+ 			reader := bufio.NewReader(os.Stdin)
+ 			fmt.Print("[k]eep, [T]rash, [r]eplay?: ")
+ 			text, _ := reader.ReadString('\n')
+ 			if text[0] == '\n' || text[0] == 'T' || text[0] == 't' {
+ 				client.RemoveObject(Config.S3.Bucket, object.Key)
+ 			} else if text[0] == 'k' || text[0] == 'K' {
+ 				id := strings.ReplaceAll(object.Key, Config.S3.Prefix, "")
+ 				src := minio.NewSourceInfo(Config.S3.Bucket, Config.S3.Prefix+id, nil)
+ 				dst, err := minio.NewDestinationInfo(Config.S3.Bucket, Config.S3.GoodPrefix+id, nil, nil)
+ 				err = client.CopyObject(dst, src)
+ 				if err == nil {
+ 					_, err = exec.Command(Config.Scripts.Directory+"/set-object-acl-public-read.sh",
+ 						Config.S3.Bucket, Config.S3.GoodPrefix+id,
+ 						"https://"+Config.S3.Endpoint, Config.S3.Id,
+ 						Config.S3.Secret, Config.S3.Region).Output()
+ 
+ 					if err != nil {
+ 						fmt.Printf("Error set-object-acl: %s\n", err.Error())
+ 						return
+ 					}
+ 
+ 					client.RemoveObject(Config.S3.Bucket, Config.S3.Prefix+id)
+ 				}
+ 			} else if text[0] == 'r' || text[0] == 'R' {
+ 				goto begin
+ 			}
+ 		}
+ 	}
+ }
+ 
+ func sortGif(client *minio.Client) {
+ 	doneCh := make(chan struct{})
+ 	defer close(doneCh)
+ 
+ 	fmt.Printf("Launching gif sorter...\n")
+ 	for object := range client.ListObjects(Config.S3.Bucket, Config.S3.Prefix, false, doneCh) {
+ 		if strings.HasSuffix(object.Key, ".gif") {
+ 		begin:
+ 			cmd := fmt.Sprintf(Config.Sorter.Gif, "https://"+Config.S3.Endpoint+"/"+Config.S3.Bucket+"/"+object.Key)
+ 			cmd_split := strings.Split(cmd, " ")
+ 			exec.Command(cmd_split[0], cmd_split[1:]...).Output()
  			reader := bufio.NewReader(os.Stdin)
  			fmt.Print("[k]eep, [T]rash, [r]eplay?: ")
  			text, _ := reader.ReadString('\n')


@@ 97,7 140,8 @@ func main() {
  	filename := flag.String("config", "/etc/kart-worker.conf", "The yaml configuration file")
  	check := flag.Bool("check", false, "Check images in the bucket")
- 	video := flag.Bool("video", false, "Check images in the bucket")
+ 	video := flag.Bool("video", false, "Launch video sorter")
+ 	gif := flag.Bool("gif", false, "Launch gif sorter")
  	keep := flag.String("keep", "id", "The image to keep")
  	throw := flag.String("throw", "id", "The image to throw")
  


@@ 120,8 164,14 @@ return
  	}
  
+ 	if *gif {
+ 		sortGif(client)
+ 		return
+ 	}
+ 
  	if *keep == "id" && *throw == "id" {
  		launchSorter(client, *filename)
+ 		sortGif(client)
  		sortVideo(client)
  	}