aboutsummaryrefslogtreecommitdiffstats
path: root/queue/item.go
blob: 7ea6e7835f05456a78da18a853373642c0e88c19 (plain) (blame)
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
package queue

import (
	"path/filepath"
	"time"

	"github.com/mpolden/lftpq/parser"
)

type Item struct {
	RemotePath string
	LocalPath  string
	ModTime    time.Time
	Transfer   bool
	Reason     string
	Media      parser.Media
	Duplicate  bool
	Merged     bool
	localDir   LocalDir
}

func (i *Item) isEmpty(readDir readDir) bool {
	dirs, _ := readDir(i.LocalPath)
	return len(dirs) == 0
}

func (i *Item) accept(reason string) {
	i.Transfer = true
	i.Reason = reason
}

func (i *Item) reject(reason string) {
	i.Transfer = false
	i.Reason = reason
}

func (i *Item) duplicates(readDir readDir) []Item {
	var items []Item
	parent := filepath.Join(i.LocalPath, "..")
	dirs, _ := readDir(parent)
	for _, fi := range dirs {
		// Ignore self
		if filepath.Base(i.RemotePath) == fi.Name() {
			continue
		}
		path := filepath.Join(parent, fi.Name())
		item, err := newItem(path, i.ModTime, i.localDir)
		if err != nil {
			item.reject(err.Error())
		} else {
			item.accept("Merged=true") // Make it considerable for deduplication
			item.Merged = true
		}
		// Ignore unequal media
		if !i.Media.Equal(item.Media) {
			continue
		}
		items = append(items, item)
	}
	return items
}

func newItem(remotePath string, modTime time.Time, localDir LocalDir) (Item, error) {
	item := Item{RemotePath: remotePath, ModTime: modTime, Reason: "no match", localDir: localDir}
	media, err := localDir.Media(remotePath)
	if err != nil {
		return Item{}, err
	}
	item.Media = media
	item.LocalPath, err = media.PathIn(localDir.Template)
	if err != nil {
		return Item{}, err
	}
	return item, nil
}