Skip to content

Commit 0772775

Browse files
authored
[feature] Clean up/uncache remote media (#407)
* Add whereNotEmptyAndNotNull * Add GetRemoteOlderThanDays * Add GetRemoteOlderThanDays * Add PruneRemote to Manager interface * Start implementing PruneRemote * add new attachment + status to tests * fix up and test GetRemoteOlderThan * fix bad import * PruneRemote: return number pruned * add Cached column to mediaattachment * update + test pruneRemote * update mediaTest * use Cached column * upstep bun to latest version * embed structs in mediaAttachment * migrate mediaAttachment to new format * don't default cached to true * select only remote media * update db dependencies * step bun back to last working version * update pruneRemote to use Cached field * fix storage path of test attachments * add recache logic to manager * fix trimmed aspect ratio * test prune and recache * return errwithcode * tidy up different paths for emoji vs attachment * fix incorrect thumbnail type being stored * expose TransportController to media processor * implement tee-ing recached content * add thoughts of dog to test fedi attachments * test get remote files * add comment on PruneRemote * add postData cleanup to recache * test thumbnail fetching * add incredible diagram * go mod tidy * buffer pipes for recache streaming * test for client stops reading after 1kb * add media-remote-cache-days to config * add cron package * wrap logrus so it's available to cron * start and stop cron jobs gracefully
1 parent 100f128 commit 0772775

File tree

424 files changed

+637501
-176899
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

424 files changed

+637501
-176899
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ The following libraries and frameworks are used by GoToSocial, with gratitude
197197
- [nfnt/resize](https://github.com/nfnt/resize); convenient image resizing. [ISC License](https://spdx.org/licenses/ISC.html).
198198
- [oklog/ulid](https://github.com/oklog/ulid); sequential, database-friendly ID generation. [Apache-2.0 License](https://spdx.org/licenses/Apache-2.0.html).
199199
- [ReneKroon/ttlcache](https://github.com/ReneKroon/ttlcache); in-memory caching. [MIT License](https://spdx.org/licenses/MIT.html).
200+
- [robfig/cron](https://github.com/robfig/cron); cron job scheduling. [MIT License](https://spdx.org/licenses/MIT.html).
200201
- [russross/blackfriday](https://github.com/russross/blackfriday); markdown parsing for statuses. [Simplified BSD License](https://spdx.org/licenses/BSD-2-Clause.html).
201202
- [sirupsen/logrus](https://github.com/sirupsen/logrus); logging. [MIT License](https://spdx.org/licenses/MIT.html).
202203
- [spf13/cobra](https://github.com/spf13/cobra); command-line tooling. [Apache-2.0 License](https://spdx.org/licenses/Apache-2.0.html).

cmd/gotosocial/flag/server.go

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ func Media(cmd *cobra.Command, values config.Values) {
6363
cmd.Flags().Int(config.Keys.MediaVideoMaxSize, values.MediaVideoMaxSize, usage.MediaVideoMaxSize)
6464
cmd.Flags().Int(config.Keys.MediaDescriptionMinChars, values.MediaDescriptionMinChars, usage.MediaDescriptionMinChars)
6565
cmd.Flags().Int(config.Keys.MediaDescriptionMaxChars, values.MediaDescriptionMaxChars, usage.MediaDescriptionMaxChars)
66+
cmd.Flags().Int(config.Keys.MediaRemoteCacheDays, values.MediaRemoteCacheDays, usage.MediaRemoteCacheDays)
6667
}
6768

6869
// Storage attaches flags pertaining to storage config.

cmd/gotosocial/flag/usage.go

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ var usage = config.KeyNames{
4747
MediaVideoMaxSize: "Max size of accepted videos in bytes",
4848
MediaDescriptionMinChars: "Min required chars for an image description",
4949
MediaDescriptionMaxChars: "Max permitted chars for an image description",
50+
MediaRemoteCacheDays: "Number of days to locally cache media from remote instances. If set to 0, remote media will be kept indefinitely.",
5051
StorageBackend: "Storage backend to use for media attachments",
5152
StorageLocalBasePath: "Full path to an already-created directory where gts should store/retrieve media files. Subfolders will be created within this dir.",
5253
StatusesMaxChars: "Max permitted characters for posted statuses",

docs/configuration/media.md

+11
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,15 @@ media-description-min-chars: 0
2828
# Examples: [500, 1000, 1500]
2929
# Default: 500
3030
media-description-max-chars: 500
31+
32+
# Int. Number of days to cache media from remote instances before they are removed from the cache.
33+
# A job will run every day at midnight to clean up any remote media older than the given amount of days.
34+
#
35+
# When remote media is removed from the cache, it is deleted from storage but the database entries for the media
36+
# are kept so that it can be fetched again if requested by a user.
37+
#
38+
# If this is set to 0, then media from remote instances will be cached indefinitely.
39+
# Examples: [30, 60, 7, 0]
40+
# Default: 30
41+
media-remote-cache-days: 30
3142
```

example/config.yaml

+11
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,17 @@ media-description-min-chars: 0
205205
# Default: 500
206206
media-description-max-chars: 500
207207

208+
# Int. Number of days to cache media from remote instances before they are removed from the cache.
209+
# A job will run every day at midnight to clean up any remote media older than the given amount of days.
210+
#
211+
# When remote media is removed from the cache, it is deleted from storage but the database entries for the media
212+
# are kept so that it can be fetched again if requested by a user.
213+
#
214+
# If this is set to 0, then media from remote instances will be cached indefinitely.
215+
# Examples: [30, 60, 7, 0]
216+
# Default: 30
217+
media-remote-cache-days: 30
218+
208219
##########################
209220
##### STORAGE CONFIG #####
210221
##########################

go.mod

+10-9
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ require (
1818
github.com/google/uuid v1.3.0
1919
github.com/gorilla/websocket v1.4.2
2020
github.com/h2non/filetype v1.1.3
21-
github.com/jackc/pgconn v1.10.1
22-
github.com/jackc/pgx/v4 v4.14.1
21+
github.com/jackc/pgconn v1.11.0
22+
github.com/jackc/pgx/v4 v4.15.0
2323
github.com/microcosm-cc/bluemonday v1.0.16
2424
github.com/mitchellh/mapstructure v1.4.3
2525
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
2626
github.com/oklog/ulid v1.3.1
27+
github.com/robfig/cron/v3 v3.0.1
2728
github.com/russross/blackfriday/v2 v2.1.0
2829
github.com/sirupsen/logrus v1.8.1
2930
github.com/spf13/cobra v1.2.1
@@ -34,15 +35,15 @@ require (
3435
github.com/superseriousbusiness/exif-terminator v0.1.0
3536
github.com/superseriousbusiness/oauth2/v4 v4.3.2-SSB
3637
github.com/tdewolff/minify/v2 v2.9.22
37-
github.com/uptrace/bun v1.0.19
38+
github.com/uptrace/bun v1.0.20
3839
github.com/uptrace/bun/dialect/pgdialect v1.0.19
3940
github.com/uptrace/bun/dialect/sqlitedialect v1.0.19
4041
github.com/wagslane/go-password-validator v0.3.0
4142
golang.org/x/crypto v0.0.0-20211209193657-4570a0811e8b
4243
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8
4344
golang.org/x/text v0.3.7
4445
gopkg.in/mcuadros/go-syslog.v2 v2.3.0
45-
modernc.org/sqlite v1.14.2
46+
modernc.org/sqlite v1.14.6
4647
mvdan.cc/xurls/v2 v2.3.0
4748
)
4849

@@ -84,7 +85,7 @@ require (
8485
github.com/jackc/pgpassfile v1.0.0 // indirect
8586
github.com/jackc/pgproto3/v2 v2.2.0 // indirect
8687
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
87-
github.com/jackc/pgtype v1.9.1 // indirect
88+
github.com/jackc/pgtype v1.10.0 // indirect
8889
github.com/jinzhu/inflection v1.0.0 // indirect
8990
github.com/json-iterator/go v1.1.12 // indirect
9091
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
@@ -108,7 +109,7 @@ require (
108109
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
109110
golang.org/x/mod v0.5.1 // indirect
110111
golang.org/x/net v0.0.0-20211209124913-491a49abca63 // indirect
111-
golang.org/x/sys v0.0.0-20211210111614-af8b64212486 // indirect
112+
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect
112113
golang.org/x/tools v0.1.8 // indirect
113114
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
114115
google.golang.org/appengine v1.6.7 // indirect
@@ -118,9 +119,9 @@ require (
118119
gopkg.in/yaml.v2 v2.4.0 // indirect
119120
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
120121
lukechampine.com/uint128 v1.1.1 // indirect
121-
modernc.org/cc/v3 v3.35.18 // indirect
122-
modernc.org/ccgo/v3 v3.12.92 // indirect
123-
modernc.org/libc v1.11.101 // indirect
122+
modernc.org/cc/v3 v3.35.22 // indirect
123+
modernc.org/ccgo/v3 v3.15.13 // indirect
124+
modernc.org/libc v1.14.5 // indirect
124125
modernc.org/mathutil v1.4.1 // indirect
125126
modernc.org/memory v1.0.5 // indirect
126127
modernc.org/opt v0.1.1 // indirect

0 commit comments

Comments
 (0)