2020-03-11 21:34:04 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-01-21 11:02:09 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
2020-09-15 07:28:53 +00:00
|
|
|
"time"
|
2020-03-11 21:34:04 +00:00
|
|
|
|
2021-01-21 11:02:09 +00:00
|
|
|
"github.com/stashapp/stash/pkg/database"
|
|
|
|
"github.com/stashapp/stash/pkg/logger"
|
2020-03-11 21:34:04 +00:00
|
|
|
"github.com/stashapp/stash/pkg/manager"
|
2020-09-15 07:28:53 +00:00
|
|
|
"github.com/stashapp/stash/pkg/manager/config"
|
2020-03-11 21:34:04 +00:00
|
|
|
"github.com/stashapp/stash/pkg/models"
|
2021-01-21 11:02:09 +00:00
|
|
|
"github.com/stashapp/stash/pkg/utils"
|
2020-03-11 21:34:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (r *mutationResolver) MetadataScan(ctx context.Context, input models.ScanMetadataInput) (string, error) {
|
2020-11-15 22:20:04 +00:00
|
|
|
manager.GetInstance().Scan(input)
|
2020-03-11 21:34:04 +00:00
|
|
|
return "todo", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *mutationResolver) MetadataImport(ctx context.Context) (string, error) {
|
|
|
|
manager.GetInstance().Import()
|
|
|
|
return "todo", nil
|
|
|
|
}
|
|
|
|
|
2020-09-20 08:36:02 +00:00
|
|
|
func (r *mutationResolver) ImportObjects(ctx context.Context, input models.ImportObjectsInput) (string, error) {
|
2021-01-31 21:15:10 +00:00
|
|
|
t, err := manager.CreateImportTask(config.GetVideoFileNamingAlgorithm(), input)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = manager.GetInstance().RunSingleTask(t)
|
2020-09-20 08:36:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return "todo", nil
|
|
|
|
}
|
|
|
|
|
2020-03-11 21:34:04 +00:00
|
|
|
func (r *mutationResolver) MetadataExport(ctx context.Context) (string, error) {
|
|
|
|
manager.GetInstance().Export()
|
|
|
|
return "todo", nil
|
|
|
|
}
|
|
|
|
|
2020-09-15 07:28:53 +00:00
|
|
|
func (r *mutationResolver) ExportObjects(ctx context.Context, input models.ExportObjectsInput) (*string, error) {
|
|
|
|
t := manager.CreateExportTask(config.GetVideoFileNamingAlgorithm(), input)
|
|
|
|
wg, err := manager.GetInstance().RunSingleTask(t)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
if t.DownloadHash != "" {
|
|
|
|
baseURL, _ := ctx.Value(BaseURLCtxKey).(string)
|
|
|
|
|
|
|
|
// generate timestamp
|
|
|
|
suffix := time.Now().Format("20060102-150405")
|
|
|
|
ret := baseURL + "/downloads/" + t.DownloadHash + "/export" + suffix + ".zip"
|
|
|
|
return &ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-03-11 21:34:04 +00:00
|
|
|
func (r *mutationResolver) MetadataGenerate(ctx context.Context, input models.GenerateMetadataInput) (string, error) {
|
2020-07-19 01:59:18 +00:00
|
|
|
manager.GetInstance().Generate(input)
|
2020-03-11 21:34:04 +00:00
|
|
|
return "todo", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *mutationResolver) MetadataAutoTag(ctx context.Context, input models.AutoTagMetadataInput) (string, error) {
|
2021-02-01 20:57:56 +00:00
|
|
|
manager.GetInstance().AutoTag(input)
|
2020-03-11 21:34:04 +00:00
|
|
|
return "todo", nil
|
|
|
|
}
|
|
|
|
|
2021-01-29 04:03:34 +00:00
|
|
|
func (r *mutationResolver) MetadataClean(ctx context.Context, input models.CleanMetadataInput) (string, error) {
|
|
|
|
manager.GetInstance().Clean(input)
|
2020-03-11 21:34:04 +00:00
|
|
|
return "todo", nil
|
|
|
|
}
|
|
|
|
|
2020-08-06 01:21:14 +00:00
|
|
|
func (r *mutationResolver) MigrateHashNaming(ctx context.Context) (string, error) {
|
|
|
|
manager.GetInstance().MigrateHash()
|
|
|
|
return "todo", nil
|
|
|
|
}
|
|
|
|
|
2020-03-11 21:34:04 +00:00
|
|
|
func (r *mutationResolver) JobStatus(ctx context.Context) (*models.MetadataUpdateStatus, error) {
|
|
|
|
status := manager.GetInstance().Status
|
|
|
|
ret := models.MetadataUpdateStatus{
|
|
|
|
Progress: status.Progress,
|
|
|
|
Status: status.Status.String(),
|
|
|
|
Message: "",
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *mutationResolver) StopJob(ctx context.Context) (bool, error) {
|
|
|
|
return manager.GetInstance().Status.Stop(), nil
|
|
|
|
}
|
2021-01-21 11:02:09 +00:00
|
|
|
|
|
|
|
func (r *mutationResolver) BackupDatabase(ctx context.Context, input models.BackupDatabaseInput) (*string, error) {
|
|
|
|
// if download is true, then backup to temporary file and return a link
|
|
|
|
download := input.Download != nil && *input.Download
|
|
|
|
mgr := manager.GetInstance()
|
|
|
|
var backupPath string
|
|
|
|
if download {
|
|
|
|
utils.EnsureDir(mgr.Paths.Generated.Downloads)
|
|
|
|
f, err := ioutil.TempFile(mgr.Paths.Generated.Downloads, "backup*.sqlite")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
backupPath = f.Name()
|
|
|
|
f.Close()
|
|
|
|
} else {
|
|
|
|
backupPath = database.DatabaseBackupPath()
|
|
|
|
}
|
|
|
|
|
|
|
|
err := database.Backup(database.DB, backupPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if download {
|
|
|
|
downloadHash := mgr.DownloadStore.RegisterFile(backupPath, "", false)
|
|
|
|
logger.Debugf("Generated backup file %s with hash %s", backupPath, downloadHash)
|
|
|
|
|
|
|
|
baseURL, _ := ctx.Value(BaseURLCtxKey).(string)
|
|
|
|
|
|
|
|
fn := filepath.Base(database.DatabaseBackupPath())
|
|
|
|
ret := baseURL + "/downloads/" + downloadHash + "/" + fn
|
|
|
|
return &ret, nil
|
|
|
|
} else {
|
|
|
|
logger.Infof("Successfully backed up database to: %s", backupPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|