-
Notifications
You must be signed in to change notification settings - Fork 74
/
main.go
176 lines (154 loc) · 4.7 KB
/
main.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
package main
import (
"fmt"
"io"
"math"
"os"
"path"
"sync"
"github.com/disintegration/imaging"
"github.com/t2bot/matrix-media-repo/archival/v2archive"
"github.com/t2bot/matrix-media-repo/cmd/homeserver_offline_exporters/_common"
"github.com/t2bot/matrix-media-repo/common/config"
"github.com/t2bot/matrix-media-repo/common/rcontext"
"github.com/t2bot/matrix-media-repo/homeserver_interop/synapse"
"github.com/t2bot/matrix-media-repo/thumbnailing"
"github.com/t2bot/matrix-media-repo/util"
)
type thumbnailSize struct {
width int
height int
method string
}
var synapseDefaultSizes = []thumbnailSize{
{32, 32, "crop"},
{96, 96, "crop"},
{320, 240, "scale"},
{640, 480, "scale"},
{800, 600, "scale"},
}
func main() {
cfg := _common.InitExportPsqlFlatFile("Synapse", "media_store_path")
ctx := rcontext.InitialNoConfig()
ctx.Config.Thumbnails = config.ThumbnailsConfig{
Types: []string{
"image/jpeg",
"image/jpg",
"image/png",
"image/apng",
"image/gif",
"image/heif",
"image/heic",
"image/webp",
"image/bmp",
"image/tiff",
},
MaxPixels: 32000000, // 32M
MaxSourceBytes: 10485760, // 10MB
}
ctx.Log.Debug("Connecting to homeserver database...")
hsDb, err := synapse.OpenDatabase(cfg.ConnectionString)
if err != nil {
panic(err)
}
err = _common.ProcessArchiveDirectory(ctx, cfg.ServerName, cfg.SourcePath, func(record *v2archive.ManifestRecord, f io.ReadCloser) error {
defer f.Close()
mxc := util.MxcUri(record.Origin, record.MediaId)
if record.SizeBytes > math.MaxInt32 {
ctx.Log.Warnf("%s is potentially too large for Synapse to handle. See https://github.com/matrix-org/synapse/issues/12023 for details.", mxc)
}
if ok, err := hsDb.HasMedia(record.MediaId); err != nil {
return err
} else if ok {
ctx.Log.Infof("%s has already been exported to Synapse", mxc)
return nil
}
// For MediaID AABBCCDD :
// $exportPath/local_content/AA/BB/CCDD
ctx.Log.Infof("Copying %s", mxc)
directories := path.Join(cfg.ExportPath, "local_content", record.MediaId[0:2], record.MediaId[2:4])
err = os.MkdirAll(directories, 0655)
if err != nil {
return err
}
filePath := path.Join(directories, record.MediaId[4:])
file, err := os.Create(filePath)
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(file, f)
if err != nil {
return err
}
if err = hsDb.InsertMedia(record.MediaId, record.ContentType, record.SizeBytes, record.CreatedTs, record.FileName, record.Uploader); err != nil {
return err
}
if !cfg.GenerateThumbnails {
return nil // we're done here
}
// Try to generate thumbnails concurrently
ctx.Log.Infof("Generating thumbnails for %s", mxc)
thumbWg := &sync.WaitGroup{}
dirLock := &sync.Mutex{}
thumbWg.Add(len(synapseDefaultSizes))
for _, size := range synapseDefaultSizes {
go func(s thumbnailSize) {
defer thumbWg.Done()
src, err := os.Open(filePath)
if err != nil {
ctx.Log.Warn("Error preparing thumbnail. ", s, err)
return
}
defer src.Close()
thumb, err := thumbnailing.GenerateThumbnail(src, record.ContentType, s.width, s.height, s.method, false, ctx)
if err != nil {
ctx.Log.Debug("Error generating thumbnail (you can probably ignore this). ", s, err)
return
}
img, err := imaging.Decode(thumb.Reader)
if err != nil {
ctx.Log.Warn("Error processing thumbnail. ", s, err)
return
}
// Same as media, but different directory
// $exportPath/local_thumbnails/AA/BB/CCDD/$thumbFile
dirLock.Lock()
defer dirLock.Unlock()
thumbDir := path.Join(cfg.ExportPath, "local_thumbnails", record.MediaId[0:2], record.MediaId[2:4], record.MediaId[4:])
err = os.MkdirAll(thumbDir, 0655)
if err != nil {
ctx.Log.Warn("Error creating thumbnail directories. ", s, err)
return
}
thumbFile, err := os.Create(path.Join(thumbDir, fmt.Sprintf("%d-%d-image-png-%s", img.Bounds().Max.X, img.Bounds().Max.Y, s.method)))
if err != nil {
ctx.Log.Warn("Error creating thumbnail. ", s, err)
return
}
defer thumbFile.Close()
err = imaging.Encode(thumbFile, img, imaging.PNG)
if err != nil {
ctx.Log.Warn("Error writing thumbnail. ", s, err)
return
}
pos, err := thumbFile.Seek(0, io.SeekCurrent)
if err != nil {
ctx.Log.Warn("Error seeking within thumbnail. ", s, err)
return
}
if err = hsDb.InsertThumbnail(record.MediaId, img.Bounds().Max.X, img.Bounds().Max.Y, "image/png", s.method, pos); err != nil {
ctx.Log.Warn("Error recording thumbnail. ", s, err)
return
}
}(size)
}
thumbWg.Wait()
// Done
return nil
})
if err != nil {
ctx.Log.Fatal(err)
}
ctx.Log.Info("Done! If there's no warnings above, you're probably fine.")
}