summaryrefslogtreecommitdiff
path: root/src/pool-import/dir-hasher.go
blob: aa204a0a9129c133c8f95293c9db7b0ba31ec46c (plain)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//
//  pool-import
//
//  Copyright (C) 2016 Christian Pointner <equinox@helsinki.at>
//
//  This file is part of pool-import.
//
//  pool-import is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  any later version.
//
//  pool-import is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with pool-import. If not, see <http://www.gnu.org/licenses/>.
//

package main

import (
	"encoding/base64"
	"io"
	"log"
	"os"
	"path/filepath"
	"strings"
	"sync"

	"golang.org/x/crypto/blake2b"
)

type FileMap map[string]string

func (m FileMap) Merge(m2 FileMap) {
	for key, value := range m2 {
		m[key] = value
	}
}

type MusicDir struct {
	root     string
	stdlog   *log.Logger
	filesMtx sync.Mutex
	Files    FileMap
}

func handleEntry(C chan<- string, path string, info os.FileInfo, err error, stdlog *log.Logger) error {
	if err != nil {
		return err
	}
	if info.IsDir() {
		stdlog.Printf("entering directory: %s", path)
		return nil
	}
	if !info.Mode().IsRegular() {
		stdlog.Printf(" - skipping (special file):  %s", info.Name())
		return nil
	}
	switch strings.ToLower(filepath.Ext(path)) {
	case ".flac":
		fallthrough
	case ".ogg":
		fallthrough
	case ".wav":
		fallthrough
	case ".mp3":
		fallthrough
	case ".aac":
		fallthrough
	case ".mp4":
		fallthrough
	case ".m4a":
		C <- path
	default:
		stdlog.Printf(" - skipping (unknown extension):  %s", info.Name())
	}
	return nil
}

func computeHash(path string) (string, error) {
	hash, err := blake2b.New256(nil)
	if err != nil {
		return "", err
	}
	file, err := os.Open(path)
	if err != nil {
		return "", err
	}
	defer file.Close()
	if _, err := io.Copy(hash, file); err != nil {
		return "", err
	}
	return base64.URLEncoding.EncodeToString(hash.Sum(nil)), nil
}

func (d *MusicDir) collectHashes(C <-chan string, numThreads int) (wg *sync.WaitGroup) {
	wg = &sync.WaitGroup{}

	for i := 0; i < numThreads; i++ {
		wg.Add(1)
		go func(num int) {
			defer wg.Done()
			for {
				file, ok := <-C
				if !ok {
					return
				}
				if hash, err := computeHash(file); err != nil {
					d.stdlog.Printf(" - skipping (%v):  %s", err, filepath.Base(file))
				} else {
					d.stdlog.Printf(" - hashed:  %s", filepath.Base(file))
					d.filesMtx.Lock()
					d.Files[hash] = file
					d.filesMtx.Unlock()
				}
			}
		}(i)
	}
	return
}

func (d *MusicDir) ComputeHashes() (err error) {
	C := make(chan string, 10)

	wg := d.collectHashes(C, 4) // TODO: make number of hashing threads configurable

	err = filepath.Walk(d.root, func(path string, info os.FileInfo, err error) error {
		return handleEntry(C, path, info, err, d.stdlog)
	})
	close(C)

	if err != nil {
		return
	}

	wg.Wait()
	return
}

func NewMusicDir(root string, stdlog *log.Logger) (dir *MusicDir) {
	dir = &MusicDir{root: root, stdlog: stdlog}
	dir.Files = make(map[string]string)
	return
}