stash/internal/manager/import.go
WithoutPants f69bd8a94f
Restructure go project (#2356)
* Move main to cmd
* Move api to internal
* Move logger and manager to internal
* Move shell hiding code to separate package
* Decouple job from desktop and utils
* Decouple session from config
* Move static into internal
* Decouple config from dlna
* Move desktop to internal
* Move dlna to internal
* Decouple remaining packages from config
* Move config into internal
* Move jsonschema and paths to models
* Make ffmpeg functions private
* Move file utility methods into fsutil package
* Move symwalk into fsutil
* Move single-use util functions into client package
* Move slice functions to separate packages
* Add env var to suppress windowsgui arg
* Move hash functions into separate package
* Move identify to internal
* Move autotag to internal
* Touch UI when generating backend
2022-03-17 11:33:59 +11:00

61 lines
1.3 KiB
Go

package manager
import (
"fmt"
"github.com/stashapp/stash/pkg/logger"
"github.com/stashapp/stash/pkg/models"
)
type importer interface {
PreImport() error
PostImport(id int) error
Name() string
FindExistingID() (*int, error)
Create() (*int, error)
Update(id int) error
}
func performImport(i importer, duplicateBehaviour models.ImportDuplicateEnum) error {
if err := i.PreImport(); err != nil {
return err
}
// try to find an existing object with the same name
name := i.Name()
existing, err := i.FindExistingID()
if err != nil {
return fmt.Errorf("error finding existing objects: %v", err)
}
var id int
if existing != nil {
if duplicateBehaviour == models.ImportDuplicateEnumFail {
return fmt.Errorf("existing object with name '%s'", name)
} else if duplicateBehaviour == models.ImportDuplicateEnumIgnore {
logger.Info("Skipping existing object")
return nil
}
// must be overwriting
id = *existing
if err := i.Update(id); err != nil {
return fmt.Errorf("error updating existing object: %v", err)
}
} else {
// creating
createdID, err := i.Create()
if err != nil {
return fmt.Errorf("error creating object: %v", err)
}
id = *createdID
}
if err := i.PostImport(id); err != nil {
return err
}
return nil
}