Skip to content

Commit

Permalink
Add regexp to featuresExtension, fix same prefix dir issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxi-Mega committed Aug 23, 2022
1 parent bf85644 commit 6369d4a
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# S3ImageServer [V2.4.4]
# S3ImageServer [V2.5.0]

### Browse images from S3 bucket

Expand Down
38 changes: 25 additions & 13 deletions src/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"path"
"reflect"
"regexp"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -40,19 +41,20 @@ type ImageGroup struct {
type Config struct {
S3 S3Config `yaml:"s3"`

BasePath string `yaml:"basePath"`
WindowTitle string `yaml:"windowTitle"`
ScaleInitialPercentage uint8 `yaml:"scaleInitialPercentage"`
PreviewFilename string `yaml:"previewFilename"`
GeonamesFilename string `yaml:"geonamesFilename"`
FeaturesExtension string `yaml:"featuresExtension"`
FeaturesPropertyName string `yaml:"featuresPropertyName"`
FullProductExtension string `yaml:"fullProductExtension"`
FullProductProtocol string `yaml:"fullProductProtocol"`
FullProductRootUrl string `yaml:"fullProductRootUrl"`
FullProductSignedUrl bool `yaml:"fullProductSignedUrl"`
ImageGroups []ImageGroup `yaml:"imageGroups"`
imageTypes []ImageType
BasePath string `yaml:"basePath"`
WindowTitle string `yaml:"windowTitle"`
ScaleInitialPercentage uint8 `yaml:"scaleInitialPercentage"`
PreviewFilename string `yaml:"previewFilename"`
GeonamesFilename string `yaml:"geonamesFilename"`
FeaturesExtensionRegexp string `yaml:"featuresExtensionRegexp"`
featuresExtensionRegexp *regexp.Regexp
FeaturesPropertyName string `yaml:"featuresPropertyName"`
FullProductExtension string `yaml:"fullProductExtension"`
FullProductProtocol string `yaml:"fullProductProtocol"`
FullProductRootUrl string `yaml:"fullProductRootUrl"`
FullProductSignedUrl bool `yaml:"fullProductSignedUrl"`
ImageGroups []ImageGroup `yaml:"imageGroups"`
imageTypes []ImageType

LogLevel string `yaml:"logLevel"`
ColorLogs bool `yaml:"colorLogs"`
Expand Down Expand Up @@ -217,6 +219,14 @@ func loadConfigFromFile(filePath string) (Config, error) {
if !valid {
return Config{}, errors.New(strings.Join(errs, "\n- "))
}

if cfg.FeaturesExtensionRegexp != "" {
cfg.featuresExtensionRegexp, err = regexp.Compile(cfg.FeaturesExtensionRegexp)
if err != nil {
return Config{}, fmt.Errorf("invalid features extension regexp: %w", err)
}
}

for _, group := range cfg.ImageGroups {
cfg.imageTypes = append(cfg.imageTypes, group.Types...)
}
Expand All @@ -232,6 +242,8 @@ func (config Config) String() string {
result += "scaleInitialPercentage: " + strconv.FormatUint(uint64(config.ScaleInitialPercentage), 10) + "\n"
result += "previewFilename: " + config.PreviewFilename + "\n"
result += "geonamesFilename: " + config.GeonamesFilename + "\n"
result += "featuresExtensionRegexp: " + config.FeaturesExtensionRegexp + "\n"
result += "featuresPropertyName: " + config.FeaturesPropertyName + "\n"
result += "fullProductExtension: " + config.FullProductExtension + "\n"
result += "fullProductProtocol: " + config.FullProductProtocol + "\n"
result += "fullProductRootUrl: " + config.FullProductRootUrl + "\n"
Expand Down
1 change: 1 addition & 0 deletions src/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func parseFeatures(filePath string) (Features, error) {
features := make(Features)
for _, rawFeature := range rawFeatures.Features {
detection := strings.Title(rawFeature.Properties[config.FeaturesPropertyName].(string))
// TODO: inflection ?
if !strings.HasSuffix(detection, "s") {
detection += "s"
}
Expand Down
4 changes: 4 additions & 0 deletions src/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ func (image S3Image) getAssociatedGeonamesPath() string {
return image.FormattedKey[:strings.LastIndex(image.FormattedKey, "@")+1] + config.GeonamesFilename
}

func (image S3Image) String() string {
return image.S3Key
}

type S3Images []S3Image

func (images S3Images) findImageByKey(key string) (image *S3Image, found bool) {
Expand Down
2 changes: 1 addition & 1 deletion src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"time"
)

const version = "2.4.4"
const version = "2.5.0"

const defaultTempDirName = "s3_image_server"

Expand Down
2 changes: 1 addition & 1 deletion src/resources/example_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ windowTitle: "S3 Image Viewer"
scaleInitialPercentage: 50
previewFilename: "preview.jpg"
geonamesFilename: "geonames.json"
featuresExtension: ".features.json"
featuresExtensionRegexp: "\\.features\\.json$"
featuresPropertyName: "detection"
fullProductExtension: "tif"
fullProductProtocol: "protocol://"
Expand Down
4 changes: 2 additions & 2 deletions src/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func listMetaFiles(minioClient *minio.Client, dirs map[string]string, eventChan
ctx, cancel := context.WithTimeout(context.Background(), config.PollingPeriod)
defer cancel()
printDebug("Looking for meta files in ", dir, " | config.geonamesFilename: ", config.GeonamesFilename)
for obj := range minioClient.ListObjects(ctx, config.S3.BucketName, minio.ListObjectsOptions{Prefix: dir, Recursive: true}) {
for obj := range minioClient.ListObjects(ctx, config.S3.BucketName, minio.ListObjectsOptions{Prefix: dir + "/", Recursive: true}) {
if obj.Err != nil {
continue
}
Expand All @@ -201,7 +201,7 @@ func listMetaFiles(minioClient *minio.Client, dirs map[string]string, eventChan
}

// features
if len(config.FeaturesExtension) > 0 && strings.HasSuffix(obj.Key, config.FeaturesExtension) {
if config.featuresExtensionRegexp != nil && config.featuresExtensionRegexp.MatchString(obj.Key) {
parts := strings.Split(obj.Key, "/")
filename := parts[len(parts)-1]
formattedFilename := formatFileName(dir + "/" + filename)
Expand Down

0 comments on commit 6369d4a

Please sign in to comment.