diff --git a/go.mod b/go.mod index f51638ba1a..e06f141e93 100644 --- a/go.mod +++ b/go.mod @@ -46,6 +46,7 @@ require ( github.com/gookit/config/v2 v2.2.9 github.com/gorilla/mux v1.8.1 github.com/grpc-ecosystem/grpc-gateway/v2 v2.31.0 + github.com/iancoleman/strcase v0.3.0 github.com/invopop/validation v0.8.0 github.com/jellydator/ttlcache/v2 v2.11.1 github.com/jellydator/ttlcache/v3 v3.4.1 @@ -246,7 +247,6 @@ require ( github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect github.com/hashicorp/yamux v0.1.2 // indirect github.com/huandu/xstrings v1.5.0 // indirect - github.com/iancoleman/strcase v0.3.0 // indirect github.com/imdario/mergo v0.3.16 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect diff --git a/services/webdav/pkg/service/v0/search.go b/services/webdav/pkg/service/v0/search.go index e38c4d1e4f..f125bfdec7 100644 --- a/services/webdav/pkg/service/v0/search.go +++ b/services/webdav/pkg/service/v0/search.go @@ -16,8 +16,12 @@ import ( userv1beta1 "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" + "github.com/iancoleman/strcase" merrors "go-micro.dev/v4/errors" "go-micro.dev/v4/metadata" + "google.golang.org/protobuf/proto" + "google.golang.org/protobuf/reflect/protoreflect" + "google.golang.org/protobuf/types/known/timestamppb" revactx "github.com/opencloud-eu/reva/v3/pkg/ctx" "github.com/opencloud-eu/reva/v3/pkg/storagespace" @@ -263,8 +267,13 @@ func matchToPropResponse(ctx context.Context, davPrefix, publicURL string, match propstatOK.Prop = append(propstatOK.Prop, prop.Escaped("oc:favorite", "1")) } - propstatOK.Prop = appendPhotoProps(propstatOK.Prop, match.GetEntity().GetPhoto()) - propstatOK.Prop = appendLocationProps(propstatOK.Prop, match.GetEntity().GetLocation()) + propstatOK.Prop = appendFacet(propstatOK.Prop, "audio", match.Entity.Audio) + propstatOK.Prop = appendFacet(propstatOK.Prop, "location", match.Entity.Location) + propstatOK.Prop = appendFacet(propstatOK.Prop, "image", match.Entity.Image) + propstatOK.Prop = appendFacet(propstatOK.Prop, "photo", match.Entity.Photo) + propstatOK.Prop = appendFacet(propstatOK.Prop, "video", match.Entity.Video) + propstatOK.Prop = appendFacet(propstatOK.Prop, "motion-photo", match.Entity.MotionPhoto) + propstatOK.Prop = appendFacet(propstatOK.Prop, "live-photo", match.Entity.LivePhoto) if len(propstatOK.Prop) > 0 { response.Propstat = append(response.Propstat, propstatOK) @@ -273,69 +282,43 @@ func matchToPropResponse(ctx context.Context, davPrefix, publicURL string, match return &response, nil } -// decimal places of the photo props; six for a coordinate resolve about 10 cm -const ( - lensPrecision = 2 - exposurePrecision = 0 - coordinatePrecision = 6 - altitudePrecision = 1 -) - -func appendPhotoProps(props []prop.PropertyXML, photo *searchmsg.Photo) []prop.PropertyXML { - if photo == nil { +// appendFacet renders a facet in the xml shape PROPFIND uses for it. +func appendFacet(props []prop.PropertyXML, name string, facet proto.Message) []prop.PropertyXML { + m := facet.ProtoReflect() + if !m.IsValid() { return props } - if photo.GetTakenDateTime() != nil { - props = append(props, prop.Escaped("oc:photo-taken-date-time", photo.GetTakenDateTime().AsTime().Format(time.RFC3339))) + + content := strings.Builder{} + fields := m.Descriptor().Fields() + for i := 0; i < fields.Len(); i++ { + fd := fields.Get(i) + if !m.Has(fd) { + continue + } + key := "oc:" + strcase.ToKebab(string(fd.Name())) + content.WriteString("<" + key + ">") + content.WriteString(prop.Escape(facetValue(fd, m.Get(fd)))) + content.WriteString("") } - props = appendStringProp(props, "oc:photo-camera-make", photo.CameraMake) - props = appendStringProp(props, "oc:photo-camera-model", photo.CameraModel) - props = appendFloat32Prop(props, "oc:photo-fnumber", photo.FNumber, lensPrecision) - props = appendFloat32Prop(props, "oc:photo-focal-length", photo.FocalLength, lensPrecision) - props = appendInt32Prop(props, "oc:photo-iso", photo.Iso) - props = appendInt32Prop(props, "oc:photo-orientation", photo.Orientation) - props = appendFloat32Prop(props, "oc:photo-exposure-numerator", photo.ExposureNumerator, exposurePrecision) - props = appendFloat32Prop(props, "oc:photo-exposure-denominator", photo.ExposureDenominator, exposurePrecision) - return props + if content.Len() == 0 { + return props + } + return append(props, prop.Raw("oc:"+name, content.String())) } -func appendLocationProps(props []prop.PropertyXML, location *searchmsg.GeoCoordinates) []prop.PropertyXML { - if location == nil { - return props +func facetValue(fd protoreflect.FieldDescriptor, v protoreflect.Value) string { + switch fd.Kind() { + case protoreflect.FloatKind: + return strconv.FormatFloat(v.Float(), 'f', -1, 32) + case protoreflect.DoubleKind: + return strconv.FormatFloat(v.Float(), 'f', -1, 64) + case protoreflect.MessageKind: + if ts, ok := v.Message().Interface().(*timestamppb.Timestamp); ok { + return ts.AsTime().Format(time.RFC3339) + } } - // no zero check here: 0 is a valid coordinate (equator, prime meridian, sea level) - props = appendFloat64Prop(props, "oc:photo-location-latitude", location.Latitude, coordinatePrecision) - props = appendFloat64Prop(props, "oc:photo-location-longitude", location.Longitude, coordinatePrecision) - props = appendFloat64Prop(props, "oc:photo-location-altitude", location.Altitude, altitudePrecision) - return props -} - -func appendStringProp(props []prop.PropertyXML, name string, value *string) []prop.PropertyXML { - if value == nil || *value == "" { - return props - } - return append(props, prop.Escaped(name, *value)) -} - -func appendFloat32Prop(props []prop.PropertyXML, name string, value *float32, precision int) []prop.PropertyXML { - if value == nil || *value == 0 { - return props - } - return append(props, prop.Escaped(name, strconv.FormatFloat(float64(*value), 'f', precision, 32))) -} - -func appendInt32Prop(props []prop.PropertyXML, name string, value *int32) []prop.PropertyXML { - if value == nil || *value == 0 { - return props - } - return append(props, prop.Escaped(name, strconv.FormatInt(int64(*value), 10))) -} - -func appendFloat64Prop(props []prop.PropertyXML, name string, value *float64, precision int) []prop.PropertyXML { - if value == nil { - return props - } - return append(props, prop.Escaped(name, strconv.FormatFloat(*value, 'f', precision, 64))) + return v.String() } type report struct { diff --git a/services/webdav/pkg/service/v0/search_test.go b/services/webdav/pkg/service/v0/search_test.go index 274cf8cf86..76ea8812db 100644 --- a/services/webdav/pkg/service/v0/search_test.go +++ b/services/webdav/pkg/service/v0/search_test.go @@ -42,65 +42,22 @@ var _ = Describe("SpacesSearchRegex", func() { ) }) -var _ = Describe("matchToPropResponse", func() { - propsOf := func(entity *searchmsg.Entity) map[string]string { - entity.Ref = &searchmsg.Reference{ResourceId: &searchmsg.ResourceID{StorageId: "storage", SpaceId: "space"}, Path: "./photo.jpg"} - entity.Id = &searchmsg.ResourceID{StorageId: "storage", SpaceId: "space", OpaqueId: "photo"} - entity.LastModifiedTime = timestamppb.Now() - res, err := matchToPropResponse(context.Background(), "", "https://cloud.example.com", &searchmsg.Match{Entity: entity}, &userv1beta1.User{Id: &userv1beta1.UserId{OpaqueId: "alice"}}, thumbnail.SourceTypes{}) +var _ = Describe("multistatusResponse", func() { + It("carries the facets of a hit in the PROPFIND shape", func() { + id := &searchmsg.ResourceID{StorageId: "storage", SpaceId: "space", OpaqueId: "file"} + res, err := multistatusResponse(context.Background(), "", "", []*searchmsg.Match{{Entity: &searchmsg.Entity{ + Ref: &searchmsg.Reference{ResourceId: id}, + Id: id, + Audio: &searchmsg.Audio{}, + Photo: &searchmsg.Photo{FNumber: proto.Float32(1.8), TakenDateTime: timestamppb.New(time.Date(2018, 1, 1, 12, 34, 56, 0, time.UTC))}, + Location: &searchmsg.GeoCoordinates{Latitude: proto.Float64(0), Longitude: proto.Float64(11.576124)}, + LivePhoto: &searchmsg.LivePhoto{ContentId: proto.String("a&b"), StillImageTimeUs: proto.Int64(1233333)}, + }}}, &userv1beta1.User{Id: &userv1beta1.UserId{}}, thumbnail.NewSourceTypes(nil)) Expect(err).ToNot(HaveOccurred()) - Expect(res.Propstat).To(HaveLen(1)) - props := map[string]string{} - for _, p := range res.Propstat[0].Prop { - props[p.XMLName.Local] = string(p.InnerXML) - } - return props - } - It("returns the photo and location metadata", func() { - props := propsOf(&searchmsg.Entity{ - Photo: &searchmsg.Photo{ - TakenDateTime: timestamppb.New(time.Date(2024, 5, 1, 12, 30, 0, 0, time.UTC)), - CameraMake: proto.String("Acme"), - CameraModel: proto.String("X100"), - FNumber: proto.Float32(1.8), - FocalLength: proto.Float32(26), - Iso: proto.Int32(400), - Orientation: proto.Int32(6), - ExposureNumerator: proto.Float32(1), - ExposureDenominator: proto.Float32(250), - }, - Location: &searchmsg.GeoCoordinates{Latitude: proto.Float64(48.137154), Longitude: proto.Float64(11.576124), Altitude: proto.Float64(519.5)}, - }) - Expect(props).To(HaveKeyWithValue("oc:photo-taken-date-time", "2024-05-01T12:30:00Z")) - Expect(props).To(HaveKeyWithValue("oc:photo-camera-make", "Acme")) - Expect(props).To(HaveKeyWithValue("oc:photo-camera-model", "X100")) - Expect(props).To(HaveKeyWithValue("oc:photo-fnumber", "1.80")) - Expect(props).To(HaveKeyWithValue("oc:photo-focal-length", "26.00")) - Expect(props).To(HaveKeyWithValue("oc:photo-iso", "400")) - Expect(props).To(HaveKeyWithValue("oc:photo-orientation", "6")) - Expect(props).To(HaveKeyWithValue("oc:photo-exposure-numerator", "1")) - Expect(props).To(HaveKeyWithValue("oc:photo-exposure-denominator", "250")) - Expect(props).To(HaveKeyWithValue("oc:photo-location-latitude", "48.137154")) - Expect(props).To(HaveKeyWithValue("oc:photo-location-longitude", "11.576124")) - Expect(props).To(HaveKeyWithValue("oc:photo-location-altitude", "519.5")) - }) - - It("keeps a zero coordinate but skips empty photo values", func() { - props := propsOf(&searchmsg.Entity{ - Photo: &searchmsg.Photo{CameraMake: proto.String(""), Iso: proto.Int32(0)}, - Location: &searchmsg.GeoCoordinates{Latitude: proto.Float64(0), Longitude: proto.Float64(0)}, - }) - Expect(props).To(HaveKeyWithValue("oc:photo-location-latitude", "0.000000")) - Expect(props).To(HaveKeyWithValue("oc:photo-location-longitude", "0.000000")) - Expect(props).ToNot(HaveKey("oc:photo-location-altitude")) - Expect(props).ToNot(HaveKey("oc:photo-camera-make")) - Expect(props).ToNot(HaveKey("oc:photo-iso")) - }) - - It("adds no photo props to a match without them", func() { - for name := range propsOf(&searchmsg.Entity{}) { - Expect(name).ToNot(HavePrefix("oc:photo-")) - } + Expect(string(res)).To(ContainSubstring("1.82018-01-01T12:34:56Z")) + Expect(string(res)).To(ContainSubstring("011.576124")) + Expect(string(res)).To(ContainSubstring("a&b1233333")) + Expect(string(res)).ToNot(ContainSubstring("oc:audio")) }) })