-
Notifications
You must be signed in to change notification settings - Fork 0
/
webdav_sync.go
208 lines (172 loc) · 4.65 KB
/
webdav_sync.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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package main
import (
"fmt"
"github.com/studio-b12/gowebdav"
"io"
"log/slog"
"net/url"
"os"
"path"
"strings"
"time"
)
type RemoteImage struct {
Url string
Mtime time.Time
Size int64
}
type WebDAVSync struct {
rootDir string
remoteUrlRoot string
client *gowebdav.Client
fileMap map[string]RemoteImage
}
var _ ImageSyncer = &WebDAVSync{}
func NewWebDAVSync(client *gowebdav.Client, rootDir, remoteUrlRoot string) *WebDAVSync {
return &WebDAVSync{
client: client,
rootDir: rootDir,
remoteUrlRoot: remoteUrlRoot,
fileMap: make(map[string]RemoteImage),
}
}
func (w *WebDAVSync) BuildImageMap() error {
err := w.readList("")
if err != nil {
return err
}
return nil
}
func (w *WebDAVSync) readList(curRelPath string) error {
fileInfos, err := w.client.ReadDir(curRelPath)
if err != nil {
return err
}
for _, fi := range fileInfos {
if fi.IsDir() {
dirName := fi.Name()
// Make sure we're not getting hacked
_, err := EnsurePathIsRelativeToItsLocation(dirName, false)
if err != nil {
return fmt.Errorf("DANGER! Received a malicious filename from WebDAV: %s", dirName)
}
err = w.readList(path.Join(curRelPath, dirName))
if err != nil {
return err
}
} else {
w.fileMap[path.Join(curRelPath, fi.Name())] = RemoteImage{
Url: path.Join(curRelPath, fi.Name()),
Mtime: fi.ModTime(),
Size: fi.Size(),
}
}
}
return nil
}
func (w *WebDAVSync) EnsureLocalImageIsUploaded(img LocalImage) (string, error) {
// Check if we already have this image
if remoteImg, ok := w.fileMap[img.relPath]; ok {
// Check if it's the same image
if remoteImg.Size == img.size && !remoteImg.Mtime.Before(img.mtime) {
return remoteImg.Url, nil
}
}
slog.Default().Info("Uploading new or changed local image", slog.String("path", img.relPath),
slog.String("name", img.fullPath), slog.Int64("size", img.size))
file, _ := os.Open(img.fullPath)
defer func() { _ = file.Close() }()
webDavPath, err := url.JoinPath(w.remoteUrlRoot, img.relPath)
if err != nil {
return "", err
}
err = w.client.WriteStream(img.relPath, file, 0644)
if err != nil {
return "", err
}
w.fileMap[img.fullPath] = RemoteImage{
Url: webDavPath,
Mtime: img.mtime,
Size: img.size,
}
slog.Default().Info("Uploaded local image", slog.String("url", webDavPath))
return webDavPath, nil
}
func (w *WebDAVSync) DownloadAndSaveImage(fullImageUrl string, postDatePart string, postSlug string) (string, error) {
// Check if image is relative to the post
if !strings.HasPrefix(fullImageUrl, w.remoteUrlRoot) {
return "", nil
}
relPath := strings.TrimPrefix(fullImageUrl, w.remoteUrlRoot)
relPath = strings.TrimPrefix(relPath, "/")
sanitizedRelPath, err := EnsurePathIsRelativeToItsLocation(relPath, false)
if err != nil || sanitizedRelPath == "" {
return "", fmt.Errorf("failed to sanitize the path: %w", err)
}
existing, ok := w.fileMap[relPath]
if ok {
localFile, err := os.Stat(path.Join(w.rootDir, sanitizedRelPath))
if err == nil {
if localFile.Size() == existing.Size {
slog.Default().Info("The image already exists", slog.String("dest", sanitizedRelPath))
return sanitizedRelPath, nil
}
if localFile.ModTime().After(existing.Mtime) {
slog.Default().Info("The local image is newer, skipping the download",
slog.String("dest", sanitizedRelPath))
return sanitizedRelPath, nil
}
}
}
slog.Default().Info("Downloading image", slog.String("dest", sanitizedRelPath))
sanitizedAbsPath := path.Join(w.rootDir, sanitizedRelPath)
sanitizedAbsDir := path.Dir(sanitizedAbsPath)
if sanitizedAbsDir != "" {
slog.Default().Info("Ensuring that the image directory exists", slog.String("dir", sanitizedAbsDir))
err = os.MkdirAll(sanitizedAbsDir, 0755)
if err != nil {
return "", err
}
}
stat, err := w.client.Stat(relPath)
if err != nil {
return "", err
}
err = w.doDownload(sanitizedAbsPath, sanitizedRelPath, stat.ModTime())
if err != nil {
return "", err
}
slog.Default().Info("Downloaded the image", slog.String("dest", sanitizedRelPath))
w.fileMap[relPath] = RemoteImage{
Url: fullImageUrl,
Mtime: stat.ModTime(),
Size: stat.Size(),
}
return sanitizedRelPath, nil
}
func (w *WebDAVSync) doDownload(absFilePath string, relPath string, mtime time.Time) error {
reader, err := w.client.ReadStream(relPath)
if err != nil {
return err
}
file, err := os.Create(absFilePath)
if err != nil {
return err
}
defer func() {
if file != nil {
_ = file.Close()
}
}()
_, err = io.Copy(file, reader)
if err != nil {
return err
}
_ = file.Close()
file = nil
err = os.Chtimes(absFilePath, time.Time{}, mtime)
if err != nil {
return err
}
return nil
}