Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Audio Metadata #7490

Merged
merged 10 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Enhancement: Store and index metadata

Audio metadata is now extracted and stored by the search service.
It is available for driveItems in a folder listing using the Graph API.

https://github.com/owncloud/ocis/pull/7490
364 changes: 301 additions & 63 deletions protogen/gen/ocis/messages/search/v0/search.pb.go

Large diffs are not rendered by default.

36 changes: 36 additions & 0 deletions protogen/gen/ocis/messages/search/v0/search.pb.web.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

98 changes: 49 additions & 49 deletions protogen/gen/ocis/messages/settings/v0/settings.pb.go

Large diffs are not rendered by default.

63 changes: 63 additions & 0 deletions protogen/gen/ocis/services/search/v0/search.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,66 @@
}
}
},
"v0Audio": {
"type": "object",
"properties": {
"album": {
"type": "string"
},
"albumArtist": {
"type": "string"
},
"artist": {
"type": "string"
},
"bitrate": {
"type": "string",
"format": "int64"
},
"composers": {
"type": "string"
},
"copyright": {
"type": "string"
},
"disc": {
"type": "integer",
"format": "int32"
},
"discCount": {
"type": "integer",
"format": "int32"
},
"duration": {
"type": "string",
"format": "int64"
},
"genre": {
"type": "string"
},
"hasDrm": {
"type": "boolean"
},
"isVariableBitrate": {
"type": "boolean"
},
"title": {
"type": "string"
},
"track": {
"type": "integer",
"format": "int32"
},
"trackCount": {
"type": "integer",
"format": "int32"
},
"year": {
"type": "integer",
"format": "int32"
}
}
},
"v0Entity": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -207,6 +267,9 @@
},
"highlights": {
"type": "string"
},
"audio": {
"$ref": "#/definitions/v0Audio"
}
}
},
Expand Down
20 changes: 20 additions & 0 deletions protogen/proto/ocis/messages/search/v0/search.proto
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,25 @@ message Reference {
string path = 2;
}

message Audio {
optional string album = 1;
optional string albumArtist = 2;
optional string artist = 3;
optional int64 bitrate = 4;
optional string composers = 5;
optional string copyright = 6;
optional int32 disc = 7;
optional int32 discCount = 8;
optional int64 duration = 9;
optional string genre = 10;
optional bool hasDrm = 11;
optional bool isVariableBitrate = 12;
optional string title = 13;
optional int32 track = 14;
optional int32 trackCount = 15;
optional int32 year = 16;
}

message Entity {
Reference ref = 1;
ResourceID id = 2;
Expand All @@ -32,6 +51,7 @@ message Entity {
ResourceID parent_id = 12;
repeated string tags = 13;
string highlights = 14;
Audio audio = 15;
}

message Match {
Expand Down
88 changes: 81 additions & 7 deletions services/graph/pkg/service/v0/driveitems.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
"net/http"
"net/url"
"path"
"reflect"
"strconv"
"strings"
"time"

cs3rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
Expand All @@ -17,6 +20,7 @@ import (
"github.com/cs3org/reva/v2/pkg/utils"
"github.com/go-chi/render"
libregraph "github.com/owncloud/libre-graph-api-go"
"github.com/owncloud/ocis/v2/ocis-pkg/log"
"github.com/owncloud/ocis/v2/services/graph/pkg/service/v0/errorcode"
"golang.org/x/crypto/sha3"
)
Expand Down Expand Up @@ -87,7 +91,7 @@ func (g Graph) GetRootDriveChildren(w http.ResponseWriter, r *http.Request) {
return
}

files, err := formatDriveItems(lRes.Infos)
files, err := formatDriveItems(g.logger, lRes.Infos)
if err != nil {
g.logger.Error().Err(err).Msg("error encoding response as json")
errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, err.Error())
Expand Down Expand Up @@ -152,7 +156,7 @@ func (g Graph) GetDriveItem(w http.ResponseWriter, r *http.Request) {
errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, res.Status.Message)
return
}
driveItem, err := cs3ResourceToDriveItem(res.Info)
driveItem, err := cs3ResourceToDriveItem(g.logger, res.Info)
if err != nil {
errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, err.Error())
return
Expand Down Expand Up @@ -220,7 +224,7 @@ func (g Graph) GetDriveItemChildren(w http.ResponseWriter, r *http.Request) {
return
}

files, err := formatDriveItems(res.Infos)
files, err := formatDriveItems(g.logger, res.Infos)
if err != nil {
errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, err.Error())
return
Expand All @@ -244,7 +248,7 @@ func (g Graph) getDriveItem(ctx context.Context, ref storageprovider.Reference)
refStr, _ := storagespace.FormatReference(&ref)
return nil, fmt.Errorf("could not stat %s: %s", refStr, res.Status.Message)
}
return cs3ResourceToDriveItem(res.Info)
return cs3ResourceToDriveItem(g.logger, res.Info)
}

func (g Graph) getRemoteItem(ctx context.Context, root *storageprovider.ResourceId, baseURL *url.URL) (*libregraph.RemoteItem, error) {
Expand Down Expand Up @@ -285,10 +289,10 @@ func (g Graph) getRemoteItem(ctx context.Context, root *storageprovider.Resource
return item, nil
}

func formatDriveItems(mds []*storageprovider.ResourceInfo) ([]*libregraph.DriveItem, error) {
func formatDriveItems(logger *log.Logger, mds []*storageprovider.ResourceInfo) ([]*libregraph.DriveItem, error) {
responses := make([]*libregraph.DriveItem, 0, len(mds))
for i := range mds {
res, err := cs3ResourceToDriveItem(mds[i])
res, err := cs3ResourceToDriveItem(logger, mds[i])
if err != nil {
return nil, err
}
Expand All @@ -302,7 +306,7 @@ func cs3TimestampToTime(t *types.Timestamp) time.Time {
return time.Unix(int64(t.Seconds), int64(t.Nanos))
}

func cs3ResourceToDriveItem(res *storageprovider.ResourceInfo) (*libregraph.DriveItem, error) {
func cs3ResourceToDriveItem(logger *log.Logger, res *storageprovider.ResourceInfo) (*libregraph.DriveItem, error) {
size := new(int64)
*size = int64(res.Size) // TODO lurking overflow: make size of libregraph drive item use uint64

Expand Down Expand Up @@ -330,9 +334,79 @@ func cs3ResourceToDriveItem(res *storageprovider.ResourceInfo) (*libregraph.Driv
if res.Type == storageprovider.ResourceType_RESOURCE_TYPE_CONTAINER {
driveItem.Folder = &libregraph.Folder{}
}

driveItem.Audio = cs3ResourceToDriveItemAudioFacet(logger, res)

return driveItem, nil
}

func cs3ResourceToDriveItemAudioFacet(logger *log.Logger, res *storageprovider.ResourceInfo) *libregraph.Audio {
if !strings.HasPrefix(res.MimeType, "audio/") {
return nil
}

k := res.ArbitraryMetadata.Metadata
if k == nil {
return nil
}

var audio = &libregraph.Audio{}
if ok := unmarshalStringMap(logger, audio, k, "libre.graph.audio."); ok {
return audio
}

return nil
}

func getFieldName(structField reflect.StructField) string {
tag := structField.Tag.Get("json")
if tag == "" {
return structField.Name
}

return strings.Split(tag, ",")[0]
}

func unmarshalStringMap(logger *log.Logger, out any, flatMap map[string]string, prefix string) bool {
nonEmpty := false
obj := reflect.ValueOf(out).Elem()
for i := 0; i < obj.NumField(); i++ {
field := obj.Field(i)
structField := obj.Type().Field(i)
mapKey := prefix + getFieldName(structField)

if value, ok := flatMap[mapKey]; ok {
if field.Kind() == reflect.Ptr {
newValue := reflect.New(field.Type().Elem())
var tmp any
var err error
switch t := newValue.Type().Elem().Kind(); t {
case reflect.String:
tmp = value
case reflect.Int32:
tmp, err = strconv.ParseInt(value, 10, 32)
case reflect.Int64:
tmp, err = strconv.ParseInt(value, 10, 64)
case reflect.Bool:
tmp, err = strconv.ParseBool(value)
default:
err = errors.New("unsupported type")
dschmidt marked this conversation as resolved.
Show resolved Hide resolved
logger.Error().Err(err).Str("type", t.String()).Str("mapKey", mapKey).Msg("target field type for value of mapKey is not supported")
}
if err != nil {
dschmidt marked this conversation as resolved.
Show resolved Hide resolved
logger.Error().Err(err).Str("mapKey", mapKey).Msg("unmarshalling failed")
continue
}
newValue.Elem().Set(reflect.ValueOf(tmp).Convert(field.Type().Elem()))
field.Set(newValue)
nonEmpty = true
}
}
}

return nonEmpty
}

func cs3ResourceToRemoteItem(res *storageprovider.ResourceInfo) (*libregraph.RemoteItem, error) {
size := new(int64)
*size = int64(res.Size) // TODO lurking overflow: make size of libregraph drive item use uint64
Expand Down
Loading