func (t *ScanTask) associateGallery(ctx context.Context, wg *sizedwaitgroup.SizedWaitGroup) {
	path := t.file.Path()
	if err := t.TxnManager.WithTxn(ctx, func(r models.Repository) error {
		qb := r.Gallery()
		sqb := r.Scene()
		g, err := qb.FindByPath(path)
		if err != nil {
			return err
		}

		if g == nil {
			// associate is run after scan is finished
			// should only happen if gallery is a directory or an io error occurs during hashing
			logger.Warnf("associate: gallery %s not found in DB", path)
			return nil
		}

		basename := strings.TrimSuffix(path, filepath.Ext(path))
		var relatedFiles []string
		vExt := config.GetInstance().GetVideoExtensions()
		// make a list of media files that can be related to the gallery
		for _, ext := range vExt {
			related := basename + "." + ext
			// exclude gallery extensions from the related files
			if !isGallery(related) {
				relatedFiles = append(relatedFiles, related)
			}
		}
		for _, scenePath := range relatedFiles {
			scene, _ := sqb.FindByPath(scenePath)
			// found related Scene
			if scene != nil {
				sceneGalleries, _ := sqb.FindByGalleryID(g.ID) // check if gallery is already associated to the scene
				isAssoc := false
				for _, sg := range sceneGalleries {
					if scene.ID == sg.ID {
						isAssoc = true
						break
					}
				}
				if !isAssoc {
					logger.Infof("associate: Gallery %s is related to scene: %d", path, scene.ID)
					if err := sqb.UpdateGalleries(scene.ID, []int{g.ID}); err != nil {
						return err
					}
				}
			}
		}
		return nil
	}); err != nil {
		logger.Error(err.Error())
	}
	wg.Done()
}
// associates a gallery to a scene with the same basename
associates a gallery to a scene with the same basename
// associates a gallery to a scene with the same basename
func (t *ScanTask) associateGallery(ctx context.Context, wg *sizedwaitgroup.SizedWaitGroup) {
	path := t.file.Path()
	if err := t.TxnManager.WithTxn(ctx, func(r models.Repository) error {
		qb := r.Gallery()
		sqb := r.Scene()
		g, err := qb.FindByPath(path)
		if err != nil {
			return err
		}
		if g == nil {
			logger.Warnf("associate: gallery %s not found in DB", path)
			return nil
		}
		basename := strings.TrimSuffix(path, filepath.Ext(path))
		var relatedFiles []string
		vExt := config.GetInstance().GetVideoExtensions()
		for _, ext := range vExt {
			related := basename + "." + ext
			if !isGallery(related) {
				relatedFiles = append(relatedFiles, related)
			}
		}
		for _, scenePath := range relatedFiles {
			scene, _ := sqb.FindByPath(scenePath)
			if scene != nil {
				sceneGalleries, _ := sqb.FindByGalleryID(g.ID) 
				isAssoc := false
				for _, sg := range sceneGalleries {
					if scene.ID == sg.ID {
						isAssoc = true
						break
					}
				}
				if !isAssoc {
					logger.Infof("associate: Gallery %s is related to scene: %d", path, scene.ID)
					if err := sqb.UpdateGalleries(scene.ID, []int{g.ID}); err != nil {
						return err
					}
				}
			}
		}
		return nil
	}); err != nil {
		logger.Error(err.Error())
	}
	wg.Done()
}
func ( t * ScanTask ) associateGallery ( ctx context . Context , wg * sizedwaitgroup . SizedWaitGroup ) { path := t . file . Path ( ) if err := t . TxnManager . WithTxn ( ctx , func ( r models . Repository ) error { qb := r . Gallery ( ) sqb := r . Scene ( ) g , err := qb . FindByPath ( path ) if err != nil { return err } if g == nil { logger . Warnf ( " " , path ) return nil } basename := strings . TrimSuffix ( path , filepath . Ext ( path ) ) var relatedFiles [ ] string vExt := config . GetInstance ( ) . GetVideoExtensions ( ) for _ , ext := range vExt { related := basename + " " + ext if ! isGallery ( related ) { relatedFiles = append ( relatedFiles , related ) } } for _ , scenePath := range relatedFiles { scene , _ := sqb . FindByPath ( scenePath ) if scene != nil { sceneGalleries , _ := sqb . FindByGalleryID ( g . ID ) isAssoc := false for _ , sg := range sceneGalleries { if scene . ID == sg . ID { isAssoc = true break } } if ! isAssoc { logger . Infof ( " " , path , scene . ID ) if err := sqb . UpdateGalleries ( scene . ID , [ ] int { g . ID } ) ; err != nil { return err } } } } return nil } ) ; err != nil { logger . Error ( err . Error ( ) ) } wg . Done ( ) }
associates a gallery to a scene with the same basename
associates a gallery to a scene with the same basename
// associate is run after scan is finished // should only happen if gallery is a directory or an io error occurs during hashing // make a list of media files that can be related to the gallery // exclude gallery extensions from the related files // found related Scene // check if gallery is already associated to the scene