68 lines
1.5 KiB
Go
68 lines
1.5 KiB
Go
|
|
package storage
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"fmt"
|
||
|
|
"strings"
|
||
|
|
)
|
||
|
|
|
||
|
|
const PublishAttachmentKeyPrefix = "publish-attachments/"
|
||
|
|
|
||
|
|
const MaxPublishAttachments = 20
|
||
|
|
|
||
|
|
func NormalizeImageKeys(single string, list []string) []string {
|
||
|
|
out := make([]string, 0, len(list)+1)
|
||
|
|
seen := make(map[string]struct{}, len(list)+1)
|
||
|
|
add := func(key string) {
|
||
|
|
key = strings.TrimSpace(key)
|
||
|
|
if key == "" {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
if _, ok := seen[key]; ok {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
seen[key] = struct{}{}
|
||
|
|
out = append(out, key)
|
||
|
|
}
|
||
|
|
add(single)
|
||
|
|
for _, key := range list {
|
||
|
|
add(key)
|
||
|
|
}
|
||
|
|
if len(out) > MaxPublishAttachments {
|
||
|
|
return out[:MaxPublishAttachments]
|
||
|
|
}
|
||
|
|
return out
|
||
|
|
}
|
||
|
|
|
||
|
|
func ValidatePublishAttachmentKey(key string) error {
|
||
|
|
key = strings.TrimSpace(key)
|
||
|
|
if key == "" {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
if !strings.HasPrefix(key, PublishAttachmentKeyPrefix) {
|
||
|
|
return fmt.Errorf("invalid publish attachment key")
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// PurgePublishAttachment deletes an ephemeral publish attachment; ignores empty/invalid keys.
|
||
|
|
func PurgePublishAttachment(ctx context.Context, store ObjectDeleter, key string) error {
|
||
|
|
key = strings.TrimSpace(key)
|
||
|
|
if key == "" {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
if err := ValidatePublishAttachmentKey(key); err != nil {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
if store == nil {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
return store.DeleteObject(ctx, key)
|
||
|
|
}
|
||
|
|
|
||
|
|
// PurgePublishAttachments deletes multiple ephemeral publish attachments.
|
||
|
|
func PurgePublishAttachments(ctx context.Context, store ObjectDeleter, keys []string) {
|
||
|
|
for _, key := range keys {
|
||
|
|
_ = PurgePublishAttachment(ctx, store, key)
|
||
|
|
}
|
||
|
|
}
|