stash/internal/dlna/cds.go

762 lines
19 KiB
Go
Raw Normal View History

2021-05-20 06:58:43 +00:00
package dlna
// from https://github.com/rclone/rclone
// Copyright (C) 2012 by Nick Craig-Wood http://www.craig-wood.com/nick/
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import (
"context"
"encoding/xml"
"fmt"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/anacrolix/dms/dlna"
"github.com/anacrolix/dms/upnp"
"github.com/anacrolix/dms/upnpav"
"github.com/stashapp/stash/pkg/logger"
"github.com/stashapp/stash/pkg/models"
"github.com/stashapp/stash/pkg/scene"
"github.com/stashapp/stash/pkg/sliceutil/stringslice"
"github.com/stashapp/stash/pkg/txn"
2021-05-20 06:58:43 +00:00
)
var pageSize = 100
type browse struct {
ObjectID string
BrowseFlag string
Filter string
StartingIndex int
RequestedCount int
}
type contentDirectoryService struct {
*Server
upnp.Eventing
}
func formatDurationSexagesimal(d time.Duration) string {
ns := d % time.Second
d /= time.Second
s := d % 60
d /= 60
m := d % 60
d /= 60
h := d
ret := fmt.Sprintf("%d:%02d:%02d.%09d", h, m, s, ns)
ret = strings.TrimRight(ret, "0")
ret = strings.TrimRight(ret, ".")
return ret
}
func (me *contentDirectoryService) updateIDString() string {
return fmt.Sprintf("%d", uint32(os.Getpid()))
}
func sceneToContainer(scene *models.Scene, parent string, host string) interface{} {
// make stash server URL
// TODO - fix this
iconURI := (&url.URL{
Scheme: "http",
Host: host,
Path: iconPath,
RawQuery: url.Values{
"scene": {strconv.Itoa(scene.ID)},
"c": {"jpeg"},
}.Encode(),
}).String()
// Object goes first
obj := upnpav.Object{
ID: strconv.Itoa(scene.ID),
Restricted: 1,
ParentID: parent,
Title: scene.GetTitle(),
Class: "object.item.videoItem",
Icon: iconURI,
AlbumArtURI: iconURI,
}
// Wrap up
item := upnpav.Item{
Object: obj,
Res: make([]upnpav.Resource, 0, 1),
}
mimeType := "video/mp4"
File storage rewrite (#2676) * Restructure data layer part 2 (#2599) * Refactor and separate image model * Refactor image query builder * Handle relationships in image query builder * Remove relationship management methods * Refactor gallery model/query builder * Add scenes to gallery model * Convert scene model * Refactor scene models * Remove unused methods * Add unit tests for gallery * Add image tests * Add scene tests * Convert unnecessary scene value pointers to values * Convert unnecessary pointer values to values * Refactor scene partial * Add scene partial tests * Refactor ImagePartial * Add image partial tests * Refactor gallery partial update * Add partial gallery update tests * Use zero/null package for null values * Add files and scan system * Add sqlite implementation for files/folders * Add unit tests for files/folders * Image refactors * Update image data layer * Refactor gallery model and creation * Refactor scene model * Refactor scenes * Don't set title from filename * Allow galleries to freely add/remove images * Add multiple scene file support to graphql and UI * Add multiple file support for images in graphql/UI * Add multiple file for galleries in graphql/UI * Remove use of some deprecated fields * Remove scene path usage * Remove gallery path usage * Remove path from image * Move funscript to video file * Refactor caption detection * Migrate existing data * Add post commit/rollback hook system * Lint. Comment out import/export tests * Add WithDatabase read only wrapper * Prepend tasks to list * Add 32 pre-migration * Add warnings in release and migration notes
2022-07-13 06:30:54 +00:00
var (
size int
bitrate uint
duration int64
)
f := scene.Files.Primary()
File storage rewrite (#2676) * Restructure data layer part 2 (#2599) * Refactor and separate image model * Refactor image query builder * Handle relationships in image query builder * Remove relationship management methods * Refactor gallery model/query builder * Add scenes to gallery model * Convert scene model * Refactor scene models * Remove unused methods * Add unit tests for gallery * Add image tests * Add scene tests * Convert unnecessary scene value pointers to values * Convert unnecessary pointer values to values * Refactor scene partial * Add scene partial tests * Refactor ImagePartial * Add image partial tests * Refactor gallery partial update * Add partial gallery update tests * Use zero/null package for null values * Add files and scan system * Add sqlite implementation for files/folders * Add unit tests for files/folders * Image refactors * Update image data layer * Refactor gallery model and creation * Refactor scene model * Refactor scenes * Don't set title from filename * Allow galleries to freely add/remove images * Add multiple scene file support to graphql and UI * Add multiple file support for images in graphql/UI * Add multiple file for galleries in graphql/UI * Remove use of some deprecated fields * Remove scene path usage * Remove gallery path usage * Remove path from image * Move funscript to video file * Refactor caption detection * Migrate existing data * Add post commit/rollback hook system * Lint. Comment out import/export tests * Add WithDatabase read only wrapper * Prepend tasks to list * Add 32 pre-migration * Add warnings in release and migration notes
2022-07-13 06:30:54 +00:00
if f != nil {
size = int(f.Size)
bitrate = uint(f.BitRate)
duration = int64(f.Duration)
}
2021-05-20 06:58:43 +00:00
item.Res = append(item.Res, upnpav.Resource{
URL: (&url.URL{
Scheme: "http",
Host: host,
Path: resPath,
RawQuery: url.Values{
"scene": {strconv.Itoa(scene.ID)},
}.Encode(),
}).String(),
ProtocolInfo: fmt.Sprintf("http-get:*:%s:%s", mimeType, dlna.ContentFeatures{
SupportRange: true,
}.String()),
File storage rewrite (#2676) * Restructure data layer part 2 (#2599) * Refactor and separate image model * Refactor image query builder * Handle relationships in image query builder * Remove relationship management methods * Refactor gallery model/query builder * Add scenes to gallery model * Convert scene model * Refactor scene models * Remove unused methods * Add unit tests for gallery * Add image tests * Add scene tests * Convert unnecessary scene value pointers to values * Convert unnecessary pointer values to values * Refactor scene partial * Add scene partial tests * Refactor ImagePartial * Add image partial tests * Refactor gallery partial update * Add partial gallery update tests * Use zero/null package for null values * Add files and scan system * Add sqlite implementation for files/folders * Add unit tests for files/folders * Image refactors * Update image data layer * Refactor gallery model and creation * Refactor scene model * Refactor scenes * Don't set title from filename * Allow galleries to freely add/remove images * Add multiple scene file support to graphql and UI * Add multiple file support for images in graphql/UI * Add multiple file for galleries in graphql/UI * Remove use of some deprecated fields * Remove scene path usage * Remove gallery path usage * Remove path from image * Move funscript to video file * Refactor caption detection * Migrate existing data * Add post commit/rollback hook system * Lint. Comment out import/export tests * Add WithDatabase read only wrapper * Prepend tasks to list * Add 32 pre-migration * Add warnings in release and migration notes
2022-07-13 06:30:54 +00:00
Bitrate: bitrate,
2021-05-20 06:58:43 +00:00
Duration: formatDurationSexagesimal(time.Duration(duration) * time.Second),
Size: uint64(size),
// Resolution: resolution,
})
item.Res = append(item.Res, upnpav.Resource{
URL: iconURI,
ProtocolInfo: "http-get:*:image/jpeg:DLNA.ORG_PN=JPEG_MED",
})
return item
}
// ContentDirectory object from ObjectID.
func (me *contentDirectoryService) objectFromID(id string) (o object, err error) {
o.Path, err = url.QueryUnescape(id)
if err != nil {
return
}
if o.Path == "0" {
o.Path = "/"
}
// o.Path = path.Clean(o.Path)
// if !path.IsAbs(o.Path) {
// err = fmt.Errorf("bad ObjectID %v", o.Path)
// return
// }
o.RootObjectPath = me.RootObjectPath
return
}
func childPath(paths []string) []string {
if len(paths) > 1 {
return paths[1:]
}
return nil
}
func (me *contentDirectoryService) Handle(action string, argsXML []byte, r *http.Request) (map[string]string, error) {
host := r.Host
// userAgent := r.UserAgent()
switch action {
case "GetSystemUpdateID":
return map[string]string{
"Id": me.updateIDString(),
}, nil
case "GetSortCapabilities":
return map[string]string{
"SortCaps": "dc:title",
}, nil
case "Browse":
var browse browse
if err := xml.Unmarshal([]byte(argsXML), &browse); err != nil {
return nil, upnp.Errorf(upnp.ArgumentValueInvalidErrorCode, "cannot unmarshal browse argument: %s", err.Error())
2021-05-20 06:58:43 +00:00
}
obj, err := me.objectFromID(browse.ObjectID)
if err != nil {
return nil, upnp.Errorf(upnpav.NoSuchObjectErrorCode, err.Error())
}
switch browse.BrowseFlag {
case "BrowseDirectChildren":
return me.handleBrowseDirectChildren(obj, host)
2021-05-20 06:58:43 +00:00
case "BrowseMetadata":
return me.handleBrowseMetadata(obj, host)
2021-05-20 06:58:43 +00:00
default:
return nil, upnp.Errorf(upnp.ArgumentValueInvalidErrorCode, "unhandled browse flag: %v", browse.BrowseFlag)
}
case "GetSearchCapabilities":
return map[string]string{
"SearchCaps": "",
}, nil
// from https://github.com/rclone/rclone/blob/master/cmd/serve/dlna/cds.go
// Samsung Extensions
case "X_GetFeatureList":
return map[string]string{
"FeatureList": `<Features xmlns="urn:schemas-upnp-org:av:avs" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:schemas-upnp-org:av:avs http://www.upnp.org/schemas/av/avs.xsd">
<Feature name="samsung.com_BASICVIEW" version="1">
<container id="0" type="object.item.imageItem"/>
<container id="0" type="object.item.audioItem"/>
<container id="0" type="object.item.videoItem"/>
</Feature>
</Features>`}, nil
case "X_SetBookmark":
// just ignore
return map[string]string{}, nil
default:
return nil, upnp.InvalidActionError
}
}
func (me *contentDirectoryService) handleBrowseDirectChildren(obj object, host string) (map[string]string, error) {
// Read folder and return children
// TODO: check if obj == 0 and return root objects
// TODO: check if special path and return files
var objs []interface{}
if obj.IsRoot() {
objs = getRootObjects()
}
paths := strings.Split(obj.Path, "/")
// All videos
if obj.Path == "all" {
objs = me.getAllScenes(host)
}
if strings.HasPrefix(obj.Path, "all/") {
page := getPageFromID(paths)
if page != nil {
objs = me.getPageVideos(&models.SceneFilterType{}, "all", *page, host)
}
}
// Saved searches
// if obj.Path == "saved-searches" {
// var savedPlaylists []models.Playlist
// db, _ := models.GetDB()
// db.Where("is_deo_enabled = ?", true).Order("ordering asc").Find(&savedPlaylists)
// db.Close()
// for _, playlist := range savedPlaylists {
// objs = append(objs, upnpav.Container{Object: upnpav.Object{
// ID: "saved-searches/" + strconv.Itoa(int(playlist.ID)),
// Restricted: 1,
// ParentID: "saved-searches",
// Class: "object.container.storageFolder",
// Title: playlist.Name,
// }})
// }
// }
// if strings.HasPrefix(obj.Path, "saved-searches/") {
// id := strings.Split(obj.Path, "/")
// var savedPlaylist models.Playlist
// db, _ := models.GetDB()
// db.Where("id = ?", id[1]).First(&savedPlaylist)
// db.Close()
// var r models.RequestSceneList
// if err := json.Unmarshal([]byte(savedPlaylist.SearchParams), &r); err == nil {
// r.IsAccessible = optional.NewBool(true)
// r.IsAvailable = optional.NewBool(true)
// data := models.QueryScenesFull(r)
// for i := range data.Scenes {
// objs = append(objs, me.sceneToContainer(data.Scenes[i], "sites/"+id[1], host))
// }
// }
// }
// Studios
if obj.Path == "studios" {
objs = me.getStudios()
}
if strings.HasPrefix(obj.Path, "studios/") {
objs = me.getStudioScenes(childPath(paths), host)
}
// Tags
if obj.Path == "tags" {
objs = me.getTags()
}
if strings.HasPrefix(obj.Path, "tags/") {
objs = me.getTagScenes(childPath(paths), host)
}
// Performers
if obj.Path == "performers" {
objs = me.getPerformers()
}
if strings.HasPrefix(obj.Path, "performers/") {
objs = me.getPerformerScenes(childPath(paths), host)
}
// Movies
if obj.Path == "movies" {
objs = me.getMovies()
}
if strings.HasPrefix(obj.Path, "movies/") {
objs = me.getMovieScenes(childPath(paths), host)
}
// Rating
if obj.Path == "rating" {
objs = me.getRating()
}
if strings.HasPrefix(obj.Path, "rating/") {
objs = me.getRatingScenes(childPath(paths), host)
}
return makeBrowseResult(objs, me.updateIDString())
}
func (me *contentDirectoryService) handleBrowseMetadata(obj object, host string) (map[string]string, error) {
var objs []interface{}
var updateID string
// if numeric, then must be scene, otherwise handle as if path
sceneID, err := strconv.Atoi(obj.Path)
if err != nil {
// #1465 - handle root object
if obj.IsRoot() {
objs = getRootObject()
} else {
// HACK: just create a fake storage folder to return. The name won't
// be correct, but hopefully the names returned from handleBrowseDirectChildren
// will be used instead.
objs = []interface{}{makeStorageFolder(obj.ID(), obj.ID(), obj.ParentID())}
}
updateID = me.updateIDString()
} else {
var scene *models.Scene
if err := txn.WithReadTxn(context.TODO(), me.txnManager, func(ctx context.Context) error {
scene, err = me.repository.SceneFinder.Find(ctx, sceneID)
if scene != nil {
err = scene.LoadPrimaryFile(ctx, me.repository.FileGetter)
}
if err != nil {
return err
}
return nil
}); err != nil {
logger.Error(err.Error())
}
if scene != nil {
upnpObject := sceneToContainer(scene, "-1", host)
objs = []interface{}{upnpObject}
// http://upnp.org/specs/av/UPnP-av-ContentDirectory-v1-Service.pdf
// maximum update ID is 2**32, then rolls back to 0
const maxUpdateID int64 = 1 << 32
File storage rewrite (#2676) * Restructure data layer part 2 (#2599) * Refactor and separate image model * Refactor image query builder * Handle relationships in image query builder * Remove relationship management methods * Refactor gallery model/query builder * Add scenes to gallery model * Convert scene model * Refactor scene models * Remove unused methods * Add unit tests for gallery * Add image tests * Add scene tests * Convert unnecessary scene value pointers to values * Convert unnecessary pointer values to values * Refactor scene partial * Add scene partial tests * Refactor ImagePartial * Add image partial tests * Refactor gallery partial update * Add partial gallery update tests * Use zero/null package for null values * Add files and scan system * Add sqlite implementation for files/folders * Add unit tests for files/folders * Image refactors * Update image data layer * Refactor gallery model and creation * Refactor scene model * Refactor scenes * Don't set title from filename * Allow galleries to freely add/remove images * Add multiple scene file support to graphql and UI * Add multiple file support for images in graphql/UI * Add multiple file for galleries in graphql/UI * Remove use of some deprecated fields * Remove scene path usage * Remove gallery path usage * Remove path from image * Move funscript to video file * Refactor caption detection * Migrate existing data * Add post commit/rollback hook system * Lint. Comment out import/export tests * Add WithDatabase read only wrapper * Prepend tasks to list * Add 32 pre-migration * Add warnings in release and migration notes
2022-07-13 06:30:54 +00:00
updateID = fmt.Sprint(scene.UpdatedAt.Unix() % maxUpdateID)
} else {
return nil, upnp.Errorf(upnpav.NoSuchObjectErrorCode, "scene not found")
}
}
return makeBrowseResult(objs, updateID)
}
func makeBrowseResult(objs []interface{}, updateID string) (map[string]string, error) {
result, err := xml.Marshal(objs)
if err != nil {
return nil, upnp.Errorf(upnp.ActionFailedErrorCode, "could not marshal objects: %s", err.Error())
}
return map[string]string{
"TotalMatches": fmt.Sprint(len(objs)),
"NumberReturned": fmt.Sprint(len(objs)),
"Result": didl_lite(string(result)),
"UpdateID": updateID,
}, nil
}
2021-05-20 06:58:43 +00:00
func makeStorageFolder(id, title, parentID string) upnpav.Container {
defaultChildCount := 1
return upnpav.Container{
Object: upnpav.Object{
ID: id,
Restricted: 1,
ParentID: parentID,
Class: "object.container.storageFolder",
Title: title,
},
ChildCount: defaultChildCount,
}
}
func getRootObject() []interface{} {
const rootID = "0"
return []interface{}{makeStorageFolder(rootID, "stash", "-1")}
}
2021-05-20 06:58:43 +00:00
func getRootObjects() []interface{} {
const rootID = "0"
var objs []interface{}
objs = append(objs, makeStorageFolder("all", "all", rootID))
objs = append(objs, makeStorageFolder("performers", "performers", rootID))
objs = append(objs, makeStorageFolder("tags", "tags", rootID))
objs = append(objs, makeStorageFolder("studios", "studios", rootID))
objs = append(objs, makeStorageFolder("movies", "movies", rootID))
objs = append(objs, makeStorageFolder("rating", "rating", rootID))
return objs
}
func getSortDirection(sceneFilter *models.SceneFilterType, sort string) models.SortDirectionEnum {
direction := models.SortDirectionEnumDesc
if sort == "title" {
direction = models.SortDirectionEnumAsc
}
return direction
}
2021-05-20 06:58:43 +00:00
func (me *contentDirectoryService) getVideos(sceneFilter *models.SceneFilterType, parentID string, host string) []interface{} {
var objs []interface{}
if err := txn.WithReadTxn(context.TODO(), me.txnManager, func(ctx context.Context) error {
sort := me.VideoSortOrder
direction := getSortDirection(sceneFilter, sort)
2021-05-20 06:58:43 +00:00
findFilter := &models.FindFilterType{
PerPage: &pageSize,
Sort: &sort,
Direction: &direction,
2021-05-20 06:58:43 +00:00
}
scenes, total, err := scene.QueryWithCount(ctx, me.repository.SceneFinder, sceneFilter, findFilter)
2021-05-20 06:58:43 +00:00
if err != nil {
return err
}
if total > pageSize {
pager := scenePager{
sceneFilter: sceneFilter,
parentID: parentID,
}
objs, err = pager.getPages(ctx, me.repository.SceneFinder, total)
2021-05-20 06:58:43 +00:00
if err != nil {
return err
}
} else {
for _, s := range scenes {
if err := s.LoadPrimaryFile(ctx, me.repository.FileGetter); err != nil {
return err
}
2021-05-20 06:58:43 +00:00
objs = append(objs, sceneToContainer(s, parentID, host))
}
}
return nil
}); err != nil {
logger.Error(err.Error())
}
return objs
}
func (me *contentDirectoryService) getPageVideos(sceneFilter *models.SceneFilterType, parentID string, page int, host string) []interface{} {
var objs []interface{}
if err := txn.WithReadTxn(context.TODO(), me.txnManager, func(ctx context.Context) error {
2021-05-20 06:58:43 +00:00
pager := scenePager{
sceneFilter: sceneFilter,
parentID: parentID,
}
sort := me.VideoSortOrder
direction := getSortDirection(sceneFilter, sort)
2021-05-20 06:58:43 +00:00
var err error
objs, err = pager.getPageVideos(ctx, me.repository.SceneFinder, me.repository.FileGetter, page, host, sort, direction)
2021-05-20 06:58:43 +00:00
if err != nil {
return err
}
return nil
}); err != nil {
logger.Error(err.Error())
}
return objs
}
func getPageFromID(paths []string) *int {
i := stringslice.StrIndex(paths, "page")
2021-05-20 06:58:43 +00:00
if i == -1 || i+1 >= len(paths) {
return nil
}
ret, err := strconv.Atoi(paths[i+1])
if err != nil {
return nil
}
return &ret
}
func (me *contentDirectoryService) getAllScenes(host string) []interface{} {
return me.getVideos(&models.SceneFilterType{}, "all", host)
}
func (me *contentDirectoryService) getStudios() []interface{} {
var objs []interface{}
if err := txn.WithReadTxn(context.TODO(), me.txnManager, func(ctx context.Context) error {
studios, err := me.repository.StudioFinder.All(ctx)
2021-05-20 06:58:43 +00:00
if err != nil {
return err
}
for _, s := range studios {
objs = append(objs, makeStorageFolder("studios/"+strconv.Itoa(s.ID), s.Name, "studios"))
2021-05-20 06:58:43 +00:00
}
return nil
}); err != nil {
logger.Errorf(err.Error())
}
return objs
}
func (me *contentDirectoryService) getStudioScenes(paths []string, host string) []interface{} {
sceneFilter := &models.SceneFilterType{
Studios: &models.HierarchicalMultiCriterionInput{
2021-05-20 06:58:43 +00:00
Modifier: models.CriterionModifierIncludes,
Value: []string{paths[0]},
},
}
parentID := "studios/" + strings.Join(paths, "/")
page := getPageFromID(paths)
if page != nil {
return me.getPageVideos(sceneFilter, parentID, *page, host)
}
return me.getVideos(sceneFilter, parentID, host)
}
func (me *contentDirectoryService) getTags() []interface{} {
var objs []interface{}
if err := txn.WithReadTxn(context.TODO(), me.txnManager, func(ctx context.Context) error {
tags, err := me.repository.TagFinder.All(ctx)
2021-05-20 06:58:43 +00:00
if err != nil {
return err
}
for _, s := range tags {
objs = append(objs, makeStorageFolder("tags/"+strconv.Itoa(s.ID), s.Name, "tags"))
}
return nil
}); err != nil {
logger.Errorf(err.Error())
}
return objs
}
func (me *contentDirectoryService) getTagScenes(paths []string, host string) []interface{} {
sceneFilter := &models.SceneFilterType{
Tag hierarchy (#1519) * Add migration script for tag relations table * Expand hierarchical filter features Expand the features of the hierarchical multi input filter with support for using a relations table, which only has parent_id and child_id columns, and support adding an additional intermediate table to join on, for example for scenes and tags which are linked by the scenes_tags table as well. * Add hierarchical filtering for tags * Add hierarchical tags support to scene markers Refactor filtering of scene markers to filterBuilder and in the process add support for hierarchical tags as well. * List parent and child tags on tag details page * Support setting parent and child tags Add support for setting parent and child tags during tag creation and tag updates. * Validate no loops are created in tags hierarchy * Update tag merging to support tag hierarcy * Add unit tests for tags.EnsureUniqueHierarchy * Fix applying recursive to with clause The SQL `RECURSIVE` of a `WITH` clause only needs to be applied once, imediately after the `WITH`. So this fixes the query building to do just that, automatically applying the `RECURSIVE` keyword when any added with clause is added as recursive. * Rename hierarchical root id column * Rewrite hierarchical filtering for performance Completely rewrite the hierarchical filtering to optimize for performance. Doing the recursive query in combination with a complex query seems to break SQLite optimizing some things which means that the recursive part might be 2,5 second slower than adding a static `VALUES()` list. This is mostly noticable in case of the tag hierarchy where setting an exclusion with any depth (or depth: all) being applied has this performance impact of 2,5 second. "Include" also suffered this issue, but some rewritten query by joining in the *_tags table in one pass and applying a `WHERE x IS NOT NULL` filter did seem to optimize that case. But that optimization isn't applied to the `IS NULL` filter of "exclude". Running a simple query beforehand to get all (recursive) items and then applying them to the query doesn't have this performance penalty. * Remove UI references to child studios and tags * Add parents to tag export * Support importing of parent relationship for tags * Assign stable ids to parent / child badges * Silence Apollo warning on parents/children fields on tags Silence warning triggered by Apollo GraphQL by explicitly instructing it to use the incoming parents/children values. By default it already does this, but it triggers a warning as it might be unintended that it uses the incoming values (instead of for example merging both arrays). Setting merge to false still applies the same behaviour (use only incoming values) but silences the warning as it's explicitly configured to work like this. * Rework detecting unique tag hierarchy Completely rework the unique tag hierarchy to detect invalid hierarchies for which a tag is "added in the middle". So when there are tags A <- B and A <- C, you could previously edit tag B and add tag C as a sub tag without it being noticed as parent A being applied twice (to tag C). While afterwards saving tag C would fail as tag A was applied as parent twice. The updated code correctly detects this scenario as well. Furthermore the error messaging has been reworked a bit and the message now mentions both the direct parent / sub tag as well as the tag which would results in the error. So in aboves example it would now show the message that tag C can't be applied because tag A already is a parent. * Update relations on cached tags when needed Update the relations on cached tags when a tag is created / updated / deleted so these always reflect the correct state. Otherwise (re)opening a tag might still show the old relations untill the page is fully reloaded or the list is navigated. But this obviously is strange when you for example have tag A, create or update tag B to have a relation to tag A, and from tags B page click through to tag A and it doesn't show that it is linked to tag B.
2021-09-09 04:58:43 +00:00
Tags: &models.HierarchicalMultiCriterionInput{
2021-05-20 06:58:43 +00:00
Modifier: models.CriterionModifierIncludes,
Value: []string{paths[0]},
},
}
parentID := "tags/" + strings.Join(paths, "/")
page := getPageFromID(paths)
if page != nil {
return me.getPageVideos(sceneFilter, parentID, *page, host)
}
return me.getVideos(sceneFilter, parentID, host)
}
func (me *contentDirectoryService) getPerformers() []interface{} {
var objs []interface{}
if err := txn.WithReadTxn(context.TODO(), me.txnManager, func(ctx context.Context) error {
performers, err := me.repository.PerformerFinder.All(ctx)
2021-05-20 06:58:43 +00:00
if err != nil {
return err
}
for _, s := range performers {
objs = append(objs, makeStorageFolder("performers/"+strconv.Itoa(s.ID), s.Name, "performers"))
2021-05-20 06:58:43 +00:00
}
return nil
}); err != nil {
logger.Errorf(err.Error())
}
return objs
}
func (me *contentDirectoryService) getPerformerScenes(paths []string, host string) []interface{} {
sceneFilter := &models.SceneFilterType{
Performers: &models.MultiCriterionInput{
Modifier: models.CriterionModifierIncludes,
Value: []string{paths[0]},
},
}
parentID := "performers/" + strings.Join(paths, "/")
page := getPageFromID(paths)
if page != nil {
return me.getPageVideos(sceneFilter, parentID, *page, host)
}
return me.getVideos(sceneFilter, parentID, host)
}
func (me *contentDirectoryService) getMovies() []interface{} {
var objs []interface{}
if err := txn.WithReadTxn(context.TODO(), me.txnManager, func(ctx context.Context) error {
movies, err := me.repository.MovieFinder.All(ctx)
2021-05-20 06:58:43 +00:00
if err != nil {
return err
}
for _, s := range movies {
objs = append(objs, makeStorageFolder("movies/"+strconv.Itoa(s.ID), s.Name, "movies"))
2021-05-20 06:58:43 +00:00
}
return nil
}); err != nil {
logger.Errorf(err.Error())
}
return objs
}
func (me *contentDirectoryService) getMovieScenes(paths []string, host string) []interface{} {
sceneFilter := &models.SceneFilterType{
Movies: &models.MultiCriterionInput{
Modifier: models.CriterionModifierIncludes,
Value: []string{paths[0]},
},
}
parentID := "movies/" + strings.Join(paths, "/")
page := getPageFromID(paths)
if page != nil {
return me.getPageVideos(sceneFilter, parentID, *page, host)
}
return me.getVideos(sceneFilter, parentID, host)
}
func (me *contentDirectoryService) getRating() []interface{} {
var objs []interface{}
for r := 1; r <= 5; r++ {
rStr := strconv.Itoa(r)
objs = append(objs, makeStorageFolder("rating/"+rStr, rStr, "rating"))
}
return objs
}
func (me *contentDirectoryService) getRatingScenes(paths []string, host string) []interface{} {
r, err := strconv.Atoi(paths[0])
if err != nil {
return nil
}
sceneFilter := &models.SceneFilterType{
Rating: &models.IntCriterionInput{
Modifier: models.CriterionModifierEquals,
Value: r,
},
}
parentID := "rating/" + strings.Join(paths, "/")
page := getPageFromID(paths)
if page != nil {
return me.getPageVideos(sceneFilter, parentID, *page, host)
}
return me.getVideos(sceneFilter, parentID, host)
}
// Represents a ContentDirectory object.
type object struct {
Path string // The cleaned, absolute path for the object relative to the server.
RootObjectPath string
}
// Returns the actual local filesystem path for the object.
func (o *object) FilePath() string {
return filepath.Join(o.RootObjectPath, filepath.FromSlash(o.Path))
}
// Returns the ObjectID for the object. This is used in various ContentDirectory actions.
func (o object) ID() string {
if len(o.Path) == 1 {
return "0"
}
return url.QueryEscape(o.Path)
}
func (o *object) IsRoot() bool {
return o.Path == "/"
}
// Returns the object's parent ObjectID. Fortunately it can be deduced from the
// ObjectID (for now).
func (o object) ParentID() string {
if o.IsRoot() {
return "-1"
}
o.Path = path.Dir(o.Path)
return o.ID()
}