2020-05-31 02:22:25 +08:00
|
|
|
package bucket
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-06-01 18:07:48 +08:00
|
|
|
"net/url"
|
2020-05-31 02:22:25 +08:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Photo string
|
|
|
|
|
|
|
|
const photoPrefix = "photo/"
|
2020-06-14 17:31:37 +08:00
|
|
|
const photoMetadataPrefix = "photometadata/"
|
2020-05-31 02:22:25 +08:00
|
|
|
|
|
|
|
var ErrorInvalidPhoto = errors.New("invalid photo")
|
|
|
|
|
|
|
|
func (p Photo) Validate() error {
|
|
|
|
if !strings.HasPrefix(string(p), photoPrefix) {
|
|
|
|
return fmt.Errorf("%w: %v", ErrorInvalidPhoto, p)
|
|
|
|
}
|
2020-06-14 17:31:37 +08:00
|
|
|
if strings.HasSuffix(string(p), "/") {
|
|
|
|
return fmt.Errorf("%w: %v", ErrorInvalidPhoto, p)
|
|
|
|
}
|
2020-05-31 02:22:25 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p Photo) String() string {
|
|
|
|
return string(p)
|
|
|
|
}
|
2020-05-31 18:24:35 +08:00
|
|
|
|
2020-06-14 17:31:37 +08:00
|
|
|
func (p Photo) Path() *url.URL {
|
|
|
|
return &url.URL{
|
2020-06-01 18:07:48 +08:00
|
|
|
Path: string(p),
|
|
|
|
}
|
2020-05-31 18:24:35 +08:00
|
|
|
}
|
2020-05-31 21:51:09 +08:00
|
|
|
|
2020-06-14 17:31:37 +08:00
|
|
|
type PhotoMetadata string
|
2020-05-31 21:51:09 +08:00
|
|
|
|
2020-06-14 17:31:37 +08:00
|
|
|
func (p Photo) Metadata(name string) PhotoMetadata {
|
2020-05-31 21:51:09 +08:00
|
|
|
objectBase := strings.Replace(string(p), photoPrefix, photoMetadataPrefix, 1)
|
2020-06-14 17:31:37 +08:00
|
|
|
return PhotoMetadata(objectBase + "/" + name)
|
2020-05-31 21:51:09 +08:00
|
|
|
}
|
|
|
|
|
2020-06-14 17:31:37 +08:00
|
|
|
func (m PhotoMetadata) String() string {
|
|
|
|
return string(m)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m PhotoMetadata) Path() *url.URL {
|
|
|
|
return &url.URL{
|
|
|
|
Path: string(m),
|
|
|
|
}
|
2020-05-31 21:51:09 +08:00
|
|
|
}
|
2020-06-14 18:40:55 +08:00
|
|
|
|
|
|
|
func (p Photo) MetadataSize() PhotoMetadata {
|
|
|
|
return p.Metadata("size")
|
|
|
|
}
|
|
|
|
func (p Photo) MetadataDate() PhotoMetadata {
|
|
|
|
return p.Metadata("date")
|
|
|
|
}
|
|
|
|
func (p Photo) MetadataTitle() PhotoMetadata {
|
|
|
|
return p.Metadata("title")
|
|
|
|
}
|
|
|
|
func (p Photo) MetadataTags() PhotoMetadata {
|
|
|
|
return p.Metadata("tags")
|
|
|
|
}
|