-
Notifications
You must be signed in to change notification settings - Fork 73
/
delete.go
45 lines (39 loc) · 1.17 KB
/
delete.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
package datastores
import (
"errors"
"os"
"path"
"github.com/minio/minio-go/v7"
"github.com/prometheus/client_golang/prometheus"
"github.com/t2bot/matrix-media-repo/common/config"
"github.com/t2bot/matrix-media-repo/common/rcontext"
"github.com/t2bot/matrix-media-repo/metrics"
)
func Remove(ctx rcontext.RequestContext, ds config.DatastoreConfig, location string) error {
var err error
if ds.Type == "s3" {
var s3c *s3
s3c, err = getS3(ds)
if err != nil {
return err
}
metrics.S3Operations.With(prometheus.Labels{"operation": "RemoveObject"}).Inc()
err = s3c.client.RemoveObject(ctx.Context, s3c.bucket, location, minio.RemoveObjectOptions{})
} else if ds.Type == "file" {
basePath := ds.Options["path"]
err = os.Remove(path.Join(basePath, location))
if err != nil && os.IsNotExist(err) {
return nil // not existing means it was deleted, as far as we care
}
} else {
return errors.New("unknown datastore type - contact developer")
}
return err
}
func RemoveWithDsId(ctx rcontext.RequestContext, dsId string, location string) error {
ds, ok := Get(ctx, dsId)
if !ok {
return errors.New("unknown datastore")
}
return Remove(ctx, ds, location)
}