Fix site_info, replace ID type in graphql queries
This commit is contained in:
parent
31da5e1c07
commit
364521958b
|
@ -20,13 +20,20 @@ func GetSiteInfo(db *gorm.DB) (*SiteInfo, error) {
|
||||||
|
|
||||||
var siteInfo SiteInfo
|
var siteInfo SiteInfo
|
||||||
|
|
||||||
err := db.FirstOrCreate(&siteInfo, SiteInfo{
|
if err := db.First(&siteInfo).Error; err != nil {
|
||||||
InitialSetup: true,
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
PeriodicScanInterval: 0,
|
siteInfo = SiteInfo{
|
||||||
ConcurrentWorkers: 3,
|
InitialSetup: true,
|
||||||
}).Error
|
PeriodicScanInterval: 0,
|
||||||
if err != nil {
|
ConcurrentWorkers: 3,
|
||||||
return nil, errors.Wrap(err, "get site info from database")
|
}
|
||||||
|
|
||||||
|
if err := db.Create(&siteInfo).Error; err != nil {
|
||||||
|
return nil, errors.Wrap(err, "initialize site_info")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return nil, errors.Wrap(err, "get site info from database")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &siteInfo, nil
|
return &siteInfo, nil
|
||||||
|
|
|
@ -185,7 +185,7 @@ func VerifyTokenAndGetUser(db *gorm.DB, token string) (*User, error) {
|
||||||
// }
|
// }
|
||||||
|
|
||||||
var user User
|
var user User
|
||||||
result = db.First(&user, accessToken.ID)
|
result = db.First(&user, accessToken.UserID)
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
return nil, result.Error
|
return nil, result.Error
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/viktorstrate/photoview/api/graphql/models"
|
"github.com/viktorstrate/photoview/api/graphql/models"
|
||||||
"github.com/viktorstrate/photoview/api/scanner"
|
"github.com/viktorstrate/photoview/api/scanner"
|
||||||
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (r *mutationResolver) ScanAll(ctx context.Context) (*models.ScannerResult, error) {
|
func (r *mutationResolver) ScanAll(ctx context.Context) (*models.ScannerResult, error) {
|
||||||
|
@ -66,7 +67,7 @@ func (r *mutationResolver) SetScannerConcurrentWorkers(ctx context.Context, work
|
||||||
return 0, errors.New("concurrent workers must at least be 1")
|
return 0, errors.New("concurrent workers must at least be 1")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := r.Database.Model(&models.SiteInfo{}).Update("concurrent_workers", workers).Error; err != nil {
|
if err := r.Database.Session(&gorm.Session{AllowGlobalUpdate: true}).Model(&models.SiteInfo{}).Update("concurrent_workers", workers).Error; err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -33,9 +33,10 @@ func CleanupMedia(db *gorm.DB, albumId int, albumMedia []*models.Media) []error
|
||||||
|
|
||||||
deleteErrors := make([]error, 0)
|
deleteErrors := make([]error, 0)
|
||||||
|
|
||||||
|
mediaIDs := make([]int, 0)
|
||||||
for _, media := range mediaList {
|
for _, media := range mediaList {
|
||||||
|
|
||||||
// deletedMediaIDs = append(deletedMediaIDs, media.ID)
|
mediaIDs = append(mediaIDs, media.ID)
|
||||||
cachePath := path.Join(PhotoCache(), strconv.Itoa(int(albumId)), strconv.Itoa(int(media.ID)))
|
cachePath := path.Join(PhotoCache(), strconv.Itoa(int(albumId)), strconv.Itoa(int(media.ID)))
|
||||||
err := os.RemoveAll(cachePath)
|
err := os.RemoveAll(cachePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -44,7 +45,7 @@ func CleanupMedia(db *gorm.DB, albumId int, albumMedia []*models.Media) []error
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := db.Delete(&mediaList).Error; err != nil {
|
if err := db.Where("id IN ?", mediaIDs).Delete(models.Media{}).Error; err != nil {
|
||||||
deleteErrors = append(deleteErrors, errors.Wrap(err, "delete old media from database"))
|
deleteErrors = append(deleteErrors, errors.Wrap(err, "delete old media from database"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,23 +70,23 @@ func deleteOldUserAlbums(db *gorm.DB, scannedAlbums []*models.Album, user *model
|
||||||
var albums []models.Album
|
var albums []models.Album
|
||||||
|
|
||||||
albums_questions := strings.Repeat("MD5(?),", len(albumPaths))[:len(albumPaths)*7-1]
|
albums_questions := strings.Repeat("MD5(?),", len(albumPaths))[:len(albumPaths)*7-1]
|
||||||
if err := db.Where("album.owner_id = ? AND path_hash NOT IN ("+albums_questions+")", album_args...).Find(&albums).Error; err != nil {
|
if err := db.Where("owner_id = ? AND path_hash NOT IN ("+albums_questions+")", album_args...).Find(&albums).Error; err != nil {
|
||||||
return []error{errors.Wrap(err, "get albums to be deleted from database")}
|
return []error{errors.Wrap(err, "get albums to be deleted from database")}
|
||||||
}
|
}
|
||||||
|
|
||||||
deleteErrors := make([]error, 0)
|
deleteErrors := make([]error, 0)
|
||||||
|
|
||||||
deleted_album_ids := make([]interface{}, 0)
|
albumIDs := make([]int, 0)
|
||||||
for _, album := range albums {
|
for _, album := range albums {
|
||||||
deleted_album_ids = append(deleted_album_ids, album.ID)
|
albumIDs = append(albumIDs, album.ID)
|
||||||
cache_path := path.Join("./photo_cache", strconv.Itoa(int(album.ID)))
|
cachePath := path.Join("./photo_cache", strconv.Itoa(int(album.ID)))
|
||||||
err := os.RemoveAll(cache_path)
|
err := os.RemoveAll(cachePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
deleteErrors = append(deleteErrors, errors.Wrapf(err, "delete unused cache folder (%s)", cache_path))
|
deleteErrors = append(deleteErrors, errors.Wrapf(err, "delete unused cache folder (%s)", cachePath))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := db.Delete(&albums).Error; err != nil {
|
if err := db.Where("id IN ?", albumIDs).Delete(models.Album{}).Error; err != nil {
|
||||||
ScannerError("Could not delete old albums from database:\n%s\n", err)
|
ScannerError("Could not delete old albums from database:\n%s\n", err)
|
||||||
deleteErrors = append(deleteErrors, errors.Wrap(err, "delete old albums from database"))
|
deleteErrors = append(deleteErrors, errors.Wrap(err, "delete old albums from database"))
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,7 @@ import Layout from '../../Layout'
|
||||||
|
|
||||||
const albumQuery = gql`
|
const albumQuery = gql`
|
||||||
query albumQuery(
|
query albumQuery(
|
||||||
$id: Int!
|
$id: ID!
|
||||||
$onlyFavorites: Boolean
|
$onlyFavorites: Boolean
|
||||||
$mediaOrderBy: String
|
$mediaOrderBy: String
|
||||||
$mediaOrderDirection: OrderDirection
|
$mediaOrderDirection: OrderDirection
|
||||||
|
|
|
@ -5,7 +5,7 @@ import { useLazyQuery } from '@apollo/client'
|
||||||
import PresentView from '../../components/photoGallery/presentView/PresentView'
|
import PresentView from '../../components/photoGallery/presentView/PresentView'
|
||||||
|
|
||||||
const QUERY_MEDIA = gql`
|
const QUERY_MEDIA = gql`
|
||||||
query placePageQueryMedia($mediaIDs: [Int!]!) {
|
query placePageQueryMedia($mediaIDs: [ID!]!) {
|
||||||
mediaList(ids: $mediaIDs) {
|
mediaList(ids: $mediaIDs) {
|
||||||
id
|
id
|
||||||
title
|
title
|
||||||
|
|
|
@ -13,7 +13,7 @@ import {
|
||||||
|
|
||||||
const updateUserMutation = gql`
|
const updateUserMutation = gql`
|
||||||
mutation updateUser(
|
mutation updateUser(
|
||||||
$id: Int!
|
$id: ID!
|
||||||
$username: String
|
$username: String
|
||||||
$rootPath: String
|
$rootPath: String
|
||||||
$admin: Boolean
|
$admin: Boolean
|
||||||
|
@ -33,7 +33,7 @@ const updateUserMutation = gql`
|
||||||
`
|
`
|
||||||
|
|
||||||
const deleteUserMutation = gql`
|
const deleteUserMutation = gql`
|
||||||
mutation deleteUser($id: Int!) {
|
mutation deleteUser($id: ID!) {
|
||||||
deleteUser(id: $id) {
|
deleteUser(id: $id) {
|
||||||
id
|
id
|
||||||
username
|
username
|
||||||
|
@ -42,7 +42,7 @@ const deleteUserMutation = gql`
|
||||||
`
|
`
|
||||||
|
|
||||||
const changeUserPasswordMutation = gql`
|
const changeUserPasswordMutation = gql`
|
||||||
mutation changeUserPassword($userId: Int!, $password: String!) {
|
mutation changeUserPassword($userId: ID!, $password: String!) {
|
||||||
updateUser(id: $userId, password: $password) {
|
updateUser(id: $userId, password: $password) {
|
||||||
id
|
id
|
||||||
}
|
}
|
||||||
|
@ -50,7 +50,7 @@ const changeUserPasswordMutation = gql`
|
||||||
`
|
`
|
||||||
|
|
||||||
const scanUserMutation = gql`
|
const scanUserMutation = gql`
|
||||||
mutation scanUser($userId: Int!) {
|
mutation scanUser($userId: ID!) {
|
||||||
scanUser(userId: $userId) {
|
scanUser(userId: $userId) {
|
||||||
success
|
success
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,7 +37,7 @@ const SettingsIcon = props => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const ALBUM_PATH_QUERY = gql`
|
const ALBUM_PATH_QUERY = gql`
|
||||||
query albumPathQuery($id: Int!) {
|
query albumPathQuery($id: ID!) {
|
||||||
album(id: $id) {
|
album(id: $id) {
|
||||||
id
|
id
|
||||||
path {
|
path {
|
||||||
|
|
|
@ -7,7 +7,7 @@ import { Icon } from 'semantic-ui-react'
|
||||||
import { ProtectedImage } from './ProtectedMedia'
|
import { ProtectedImage } from './ProtectedMedia'
|
||||||
|
|
||||||
const markFavoriteMutation = gql`
|
const markFavoriteMutation = gql`
|
||||||
mutation markMediaFavorite($mediaId: Int!, $favorite: Boolean!) {
|
mutation markMediaFavorite($mediaId: ID!, $favorite: Boolean!) {
|
||||||
favoriteMedia(mediaId: $mediaId, favorite: $favorite) {
|
favoriteMedia(mediaId: $mediaId, favorite: $favorite) {
|
||||||
id
|
id
|
||||||
favorite
|
favorite
|
||||||
|
|
|
@ -4,7 +4,7 @@ import { useQuery, gql } from '@apollo/client'
|
||||||
import SidebarShare from './Sharing'
|
import SidebarShare from './Sharing'
|
||||||
|
|
||||||
const albumQuery = gql`
|
const albumQuery = gql`
|
||||||
query getAlbumSidebar($id: Int!) {
|
query getAlbumSidebar($id: ID!) {
|
||||||
album(id: $id) {
|
album(id: $id) {
|
||||||
id
|
id
|
||||||
title
|
title
|
||||||
|
|
|
@ -9,7 +9,7 @@ import SidebarDownload from './SidebarDownload'
|
||||||
import SidebarItem from './SidebarItem'
|
import SidebarItem from './SidebarItem'
|
||||||
|
|
||||||
const mediaQuery = gql`
|
const mediaQuery = gql`
|
||||||
query sidebarPhoto($id: Int!) {
|
query sidebarPhoto($id: ID!) {
|
||||||
media(id: $id) {
|
media(id: $id) {
|
||||||
id
|
id
|
||||||
title
|
title
|
||||||
|
|
|
@ -13,7 +13,7 @@ import copy from 'copy-to-clipboard'
|
||||||
import { authToken } from '../../authentication'
|
import { authToken } from '../../authentication'
|
||||||
|
|
||||||
const sharePhotoQuery = gql`
|
const sharePhotoQuery = gql`
|
||||||
query sidbarGetPhotoShares($id: Int!) {
|
query sidbarGetPhotoShares($id: ID!) {
|
||||||
media(id: $id) {
|
media(id: $id) {
|
||||||
id
|
id
|
||||||
shares {
|
shares {
|
||||||
|
@ -25,7 +25,7 @@ const sharePhotoQuery = gql`
|
||||||
`
|
`
|
||||||
|
|
||||||
const shareAlbumQuery = gql`
|
const shareAlbumQuery = gql`
|
||||||
query sidbarGetAlbumShares($id: Int!) {
|
query sidbarGetAlbumShares($id: ID!) {
|
||||||
album(id: $id) {
|
album(id: $id) {
|
||||||
id
|
id
|
||||||
shares {
|
shares {
|
||||||
|
@ -37,7 +37,7 @@ const shareAlbumQuery = gql`
|
||||||
`
|
`
|
||||||
|
|
||||||
const addPhotoShareMutation = gql`
|
const addPhotoShareMutation = gql`
|
||||||
mutation sidebarPhotoAddShare($id: Int!, $password: String, $expire: Time) {
|
mutation sidebarPhotoAddShare($id: ID!, $password: String, $expire: Time) {
|
||||||
shareMedia(mediaId: $id, password: $password, expire: $expire) {
|
shareMedia(mediaId: $id, password: $password, expire: $expire) {
|
||||||
token
|
token
|
||||||
}
|
}
|
||||||
|
@ -45,7 +45,7 @@ const addPhotoShareMutation = gql`
|
||||||
`
|
`
|
||||||
|
|
||||||
const addAlbumShareMutation = gql`
|
const addAlbumShareMutation = gql`
|
||||||
mutation sidebarAlbumAddShare($id: Int!, $password: String, $expire: Time) {
|
mutation sidebarAlbumAddShare($id: ID!, $password: String, $expire: Time) {
|
||||||
shareAlbum(albumId: $id, password: $password, expire: $expire) {
|
shareAlbum(albumId: $id, password: $password, expire: $expire) {
|
||||||
token
|
token
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ import { useLazyQuery, gql } from '@apollo/client'
|
||||||
import { authToken } from '../../authentication'
|
import { authToken } from '../../authentication'
|
||||||
|
|
||||||
export const SIDEBAR_DOWNLOAD_QUERY = gql`
|
export const SIDEBAR_DOWNLOAD_QUERY = gql`
|
||||||
query sidebarDownloadQuery($mediaId: Int!) {
|
query sidebarDownloadQuery($mediaId: ID!) {
|
||||||
media(id: $mediaId) {
|
media(id: $mediaId) {
|
||||||
id
|
id
|
||||||
downloads {
|
downloads {
|
||||||
|
|
Loading…
Reference in New Issue