Skip to content

Commit c7c0551

Browse files
committed
feat(search): populate audio and gps-altitude facets from tika, drop the audio-only gate
1 parent 69a6d8a commit c7c0551

5 files changed

Lines changed: 88 additions & 25 deletions

File tree

services/search/pkg/content/tika.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,7 @@ func (t Tika) Extract(ctx context.Context, ri *provider.ResourceInfo) (Document,
9090
doc.Location = t.getLocation(meta)
9191
doc.Image = t.getImage(meta)
9292
doc.Photo = t.getPhoto(meta)
93-
94-
if contentType, err := getFirstValue(meta, "Content-Type"); err == nil && strings.HasPrefix(contentType, "audio/") {
95-
doc.Audio = t.getAudio(meta)
96-
}
93+
doc.Audio = t.getAudio(meta)
9794
}
9895

9996
if langCode, _ := t.tika.LanguageString(ctx, doc.Content); langCode != "" && t.CleanStopWords {

services/search/pkg/content/tika_audio.go

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,23 @@ func (t Tika) getAudio(meta map[string][]string) *libregraph.Audio {
3030
audio.SetArtist(v)
3131
}
3232

33-
// TODO: audio.Bitrate: not provided by tika
34-
// TODO: audio.Composers: not provided by tika
35-
// TODO: audio.Copyright: not provided by tika for audio files?
33+
if v, err := getFirstValue(meta, "audio:bitrate"); err == nil {
34+
// tika emits bits per second, graph wants kbps
35+
if bps, err := strconv.ParseInt(v, 10, 64); err == nil {
36+
initAudio()
37+
audio.SetBitrate(bps / 1000)
38+
}
39+
}
40+
41+
if v, err := getFirstValue(meta, "xmpDM:composer"); err == nil {
42+
initAudio()
43+
audio.SetComposers(v)
44+
}
45+
46+
if v, err := getFirstValue(meta, "xmpDM:copyright"); err == nil {
47+
initAudio()
48+
audio.SetCopyright(v)
49+
}
3650

3751
if v, err := getFirstValue(meta, "xmpDM:discNumber"); err == nil {
3852
if i, err := strconv.ParseInt(v, 10, 32); err == nil {
@@ -42,7 +56,12 @@ func (t Tika) getAudio(meta map[string][]string) *libregraph.Audio {
4256

4357
}
4458

45-
// TODO: audio.DiscCount: not provided by tika
59+
if v, err := getFirstValue(meta, "audio:disc-count"); err == nil {
60+
if i, err := strconv.ParseInt(v, 10, 32); err == nil {
61+
initAudio()
62+
audio.SetDiscCount(int32(i))
63+
}
64+
}
4665

4766
if v, err := getFirstValue(meta, "xmpDM:duration"); err == nil {
4867
// Tika emits fractional seconds.
@@ -57,8 +76,19 @@ func (t Tika) getAudio(meta map[string][]string) *libregraph.Audio {
5776
audio.SetGenre(v)
5877
}
5978

60-
// TODO: audio.HasDrm: not provided by tika
61-
// TODO: audio.IsVariableBitrate: not provided by tika
79+
if v, err := getFirstValue(meta, "audio:has-drm"); err == nil {
80+
if b, err := strconv.ParseBool(v); err == nil {
81+
initAudio()
82+
audio.SetHasDrm(b)
83+
}
84+
}
85+
86+
if v, err := getFirstValue(meta, "audio:is-variable-bitrate"); err == nil {
87+
if b, err := strconv.ParseBool(v); err == nil {
88+
initAudio()
89+
audio.SetIsVariableBitrate(b)
90+
}
91+
}
6292

6393
if v, err := getFirstValue(meta, "dc:title"); err == nil {
6494
initAudio()
@@ -72,7 +102,12 @@ func (t Tika) getAudio(meta map[string][]string) *libregraph.Audio {
72102
}
73103
}
74104

75-
// TODO: audio.TrackCount: not provided by tika
105+
if v, err := getFirstValue(meta, "audio:track-count"); err == nil {
106+
if i, err := strconv.ParseInt(v, 10, 32); err == nil {
107+
initAudio()
108+
audio.SetTrackCount(int32(i))
109+
}
110+
}
76111

77112
if v, err := getFirstValue(meta, "xmpDM:releaseDate"); err == nil {
78113
if i, err := strconv.ParseInt(v, 10, 32); err == nil {

services/search/pkg/content/tika_audio_test.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,22 @@ import (
99
var _ = Describe("getAudio", func() {
1010
It("maps the audio metadata to the audio facet", func() {
1111
meta := map[string][]string{
12-
"xmpDM:genre": {"Some Genre"},
13-
"xmpDM:album": {"Some Album"},
14-
"xmpDM:trackNumber": {"7"},
15-
"xmpDM:discNumber": {"4"},
16-
"xmpDM:releaseDate": {"2004"},
17-
"xmpDM:artist": {"Some Artist"},
18-
"xmpDM:albumArtist": {"Some AlbumArtist"},
19-
"dc:title": {"Some Title"},
20-
"xmpDM:duration": {"225.5"},
12+
"xmpDM:genre": {"Some Genre"},
13+
"xmpDM:album": {"Some Album"},
14+
"xmpDM:trackNumber": {"7"},
15+
"xmpDM:discNumber": {"4"},
16+
"xmpDM:releaseDate": {"2004"},
17+
"xmpDM:artist": {"Some Artist"},
18+
"xmpDM:albumArtist": {"Some AlbumArtist"},
19+
"dc:title": {"Some Title"},
20+
"xmpDM:duration": {"225.5"},
21+
"xmpDM:composer": {"Some Composers"},
22+
"xmpDM:copyright": {"Some Copyright"},
23+
"audio:bitrate": {"192000"},
24+
"audio:is-variable-bitrate": {"true"},
25+
"audio:has-drm": {"false"},
26+
"audio:track-count": {"9"},
27+
"audio:disc-count": {"5"},
2128
}
2229

2330
audio := Tika{}.getAudio(meta)
@@ -26,11 +33,18 @@ var _ = Describe("getAudio", func() {
2633
Expect(audio.Album).To(Equal(libregraph.PtrString("Some Album")))
2734
Expect(audio.AlbumArtist).To(Equal(libregraph.PtrString("Some AlbumArtist")))
2835
Expect(audio.Artist).To(Equal(libregraph.PtrString("Some Artist")))
36+
Expect(audio.Bitrate).To(Equal(libregraph.PtrInt64(192)))
37+
Expect(audio.Composers).To(Equal(libregraph.PtrString("Some Composers")))
38+
Expect(audio.Copyright).To(Equal(libregraph.PtrString("Some Copyright")))
2939
Expect(audio.Disc).To(Equal(libregraph.PtrInt32(4)))
40+
Expect(audio.DiscCount).To(Equal(libregraph.PtrInt32(5)))
3041
Expect(audio.Duration).To(Equal(libregraph.PtrInt64(225500)))
3142
Expect(audio.Genre).To(Equal(libregraph.PtrString("Some Genre")))
43+
Expect(audio.HasDrm).To(Equal(libregraph.PtrBool(false)))
44+
Expect(audio.IsVariableBitrate).To(Equal(libregraph.PtrBool(true)))
3245
Expect(audio.Title).To(Equal(libregraph.PtrString("Some Title")))
3346
Expect(audio.Track).To(Equal(libregraph.PtrInt32(7)))
47+
Expect(audio.TrackCount).To(Equal(libregraph.PtrInt32(9)))
3448
Expect(audio.Year).To(Equal(libregraph.PtrInt32(2004)))
3549
})
3650

services/search/pkg/content/tika_location.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import (
66
libregraph "github.com/opencloud-eu/libre-graph-api-go"
77
)
88

9+
// graph geoCoordinates.altitude is in feet, exif GPS altitude (geo:alt) in metres.
10+
const metresToFeet = 3.280839895
11+
912
func (t Tika) getLocation(meta map[string][]string) *libregraph.GeoCoordinates {
1013
var location *libregraph.GeoCoordinates
1114
initLocation := func() {
@@ -14,10 +17,6 @@ func (t Tika) getLocation(meta map[string][]string) *libregraph.GeoCoordinates {
1417
}
1518
}
1619

17-
// TODO: location.Altitute: transform the following data to … feet above sea level.
18-
// "GPS:GPS Altitude": []string{"227.4 metres"},
19-
// "GPS:GPS Altitude Ref": []string{"Sea level"},
20-
2120
if v, err := getFirstValue(meta, "geo:lat"); err == nil {
2221
if i, err := strconv.ParseFloat(v, 64); err == nil {
2322
initLocation()
@@ -32,5 +31,13 @@ func (t Tika) getLocation(meta map[string][]string) *libregraph.GeoCoordinates {
3231
}
3332
}
3433

34+
// tika emits metres (negative below sea level), graph wants feet
35+
if v, err := getFirstValue(meta, "geo:alt"); err == nil {
36+
if metres, err := strconv.ParseFloat(v, 64); err == nil {
37+
initLocation()
38+
location.SetAltitude(metres * metresToFeet)
39+
}
40+
}
41+
3542
return location
3643
}

services/search/pkg/content/tika_location_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,24 @@ import (
77
)
88

99
var _ = Describe("getLocation", func() {
10-
It("maps latitude and longitude to the location facet", func() {
10+
It("maps lat/long and converts altitude from metres to feet", func() {
11+
metres := 227.4
1112
location := Tika{}.getLocation(map[string][]string{
1213
"geo:lat": {"49.48675890884328"},
1314
"geo:long": {"11.103870357204285"},
15+
"geo:alt": {"227.4"},
1416
})
1517
Expect(location).ToNot(BeNil())
1618
Expect(location.Latitude).To(Equal(libregraph.PtrFloat64(49.48675890884328)))
1719
Expect(location.Longitude).To(Equal(libregraph.PtrFloat64(11.103870357204285)))
20+
Expect(location.Altitude).To(Equal(libregraph.PtrFloat64(metres * metresToFeet)))
21+
})
22+
23+
It("keeps below-sea-level altitude negative", func() {
24+
metres := -227.4
25+
location := Tika{}.getLocation(map[string][]string{"geo:alt": {"-227.4"}})
26+
Expect(location).ToNot(BeNil())
27+
Expect(location.Altitude).To(Equal(libregraph.PtrFloat64(metres * metresToFeet)))
1828
})
1929

2030
It("returns nil when no location metadata is present", func() {

0 commit comments

Comments
 (0)