-
Notifications
You must be signed in to change notification settings - Fork 0
/
photos.go
63 lines (59 loc) · 1.98 KB
/
photos.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
package dayone2md
import (
"fmt"
"log/slog"
"os"
"path/filepath"
"regexp"
"strings"
)
// savePhotos - TODO too complex.
func savePhotos(entryPhotos []Photo, outputDir string, pg photoGetter, catalog *fileCataloger) (map[string]string, error) {
// save photos
photos := make(map[string]string)
for _, photo := range entryPhotos {
slog.Debug("savePhotos", "photo.md5", photo.MD5, "photo.type", photo.Type, "photo.filename", photo.Filename)
// find file in export photos directory
sourceFile := fmt.Sprintf("%s.%s", photo.MD5, photo.Type)
targetFile := photo.Filename
if targetFile == "" {
targetFile = sourceFile
}
targetFile = filepath.Join("photos", targetFile)
photos[photo.Identifier] = targetFile
// copy file from archive to destination
targetPath := filepath.Join(outputDir, targetFile)
slog.Debug("savePhotos2", "sourceFile", sourceFile, "targetFile", targetFile)
if catalog.ShouldWritePhoto(sourceFile, targetFile, pg) {
slog.Info("copying photo", "file", targetPath)
if err := copyPhoto(sourceFile, targetPath, pg); err != nil {
return nil, fmt.Errorf("cannot copy photo (%s): %w", targetFile, err)
}
}
catalog.AddGeneratedFile(targetFile)
}
return photos, nil
}
func replacePhotoLinks(text string, photos map[string]string) string {
// replace dayone photo references with markdown image references in text
re := regexp.MustCompile(`dayone\-moment\:\/\/(\w+)`)
submatches := re.FindAllStringSubmatch(text, -1)
for _, submatch := range submatches {
subtext, id := submatch[0], submatch[1]
if filename, ok := photos[id]; ok {
link := filepath.Join(".", filename)
text = strings.Replace(text, subtext, link, 1)
}
}
return text
}
func copyPhoto(source, destination string, pg photoGetter) error {
srcData, err := pg.GetPhoto(source)
if err != nil {
return fmt.Errorf("cannot open photo source file: %w", err)
}
if err := os.WriteFile(destination, srcData, 0o600); err != nil {
return fmt.Errorf("cannot copy photo: %w", err)
}
return nil
}