| 1 |
package attachment |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"io" |
| 6 |
"time" |
| 7 |
|
| 8 |
"heckel.io/ntfy/v2/s3" |
| 9 |
) |
| 10 |
|
| 11 |
type s3Backend struct { |
| 12 |
client *s3.Client |
| 13 |
} |
| 14 |
|
| 15 |
var _ backend = (*s3Backend)(nil) |
| 16 |
|
| 17 |
func newS3Backend(client *s3.Client) *s3Backend { |
| 18 |
return &s3Backend{client: client} |
| 19 |
} |
| 20 |
|
| 21 |
func (b *s3Backend) Put(id string, reader io.Reader, untrustedLength int64) error { |
| 22 |
return b.client.PutObject(context.Background(), id, reader, untrustedLength) |
| 23 |
} |
| 24 |
|
| 25 |
func (b *s3Backend) Get(id string) (io.ReadCloser, int64, error) { |
| 26 |
return b.client.GetObject(context.Background(), id) |
| 27 |
} |
| 28 |
|
| 29 |
func (b *s3Backend) List() ([]object, error) { |
| 30 |
objects, err := b.client.ListObjectsV2(context.Background()) |
| 31 |
if err != nil { |
| 32 |
return nil, err |
| 33 |
} |
| 34 |
result := make([]object, 0, len(objects)) |
| 35 |
for _, obj := range objects { |
| 36 |
result = append(result, object{ |
| 37 |
ID: obj.Key, |
| 38 |
Size: obj.Size, |
| 39 |
LastModified: obj.LastModified, |
| 40 |
}) |
| 41 |
} |
| 42 |
return result, nil |
| 43 |
} |
| 44 |
|
| 45 |
func (b *s3Backend) Delete(ids ...string) error { |
| 46 |
return b.client.DeleteObjects(context.Background(), ids) |
| 47 |
} |
| 48 |
|
| 49 |
func (b *s3Backend) DeleteIncomplete(cutoff time.Time) error { |
| 50 |
return b.client.AbortIncompleteUploads(context.Background(), cutoff) |
| 51 |
} |