-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcsv.go
80 lines (67 loc) · 1.66 KB
/
csv.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
package smugmug
import (
"encoding/csv"
"fmt"
"os"
log "github.com/sirupsen/logrus"
)
// METADATA_FNAME is the name of the CSV file used to store files metadata
const METADATA_FNAME = "metadata.csv"
var csvHeader = []string{
"Filename",
"Type",
"ArchivedUri",
"Caption",
"Keywords",
"Latitude",
"Longitude",
}
// createMetadataCSV creates the metadata CSV file and writes the header line
func createMetadataCSV(fpath string) error {
file, err := os.Create(fpath)
if err != nil {
return fmt.Errorf("failed creating metadata CSV file: %v", err)
}
defer file.Close()
writer := csv.NewWriter(file)
defer writer.Flush()
return writer.Write(csvHeader)
}
// buildMetadata returns the data to be added to the metadata CSV file
func (w *Worker) buildMetadata(a albumImage, folder string) []string {
ftype := "image"
if a.IsVideo {
ftype = "video"
}
return []string{
fmt.Sprintf("%s/%s", folder, a.Name()),
ftype,
a.ArchivedUri,
a.Caption,
a.Keywords,
a.Latitude,
a.Longitude,
}
}
// writeToCSV writes images metadata to CSV file
func (w *Worker) writeToCSV(images []albumImage, folder string) {
w.csvLock.Lock()
defer w.csvLock.Unlock()
file, err := os.OpenFile(w.cfg.metadataFile, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
if err != nil {
log.Errorf("cannot open metadata CSV file: %v", err)
}
defer file.Close()
writer := csv.NewWriter(file)
defer func() {
writer.Flush()
if err := writer.Error(); err != nil {
log.Errorf("error flushing metadata file: %v", err)
}
}()
for _, img := range images {
if err := writer.Write(w.buildMetadata(img, folder)); err != nil {
log.Errorf("cannot write to metadata CSV file: %v", err)
}
}
}