2020-01-02 00:03:07 +01:00
package config
import (
"strings"
"time"
"github.com/photoprism/photoprism/internal/entity"
2020-04-30 15:41:47 +02:00
"github.com/photoprism/photoprism/pkg/capture"
2020-01-13 11:07:09 +01:00
"github.com/photoprism/photoprism/pkg/colors"
2020-01-12 14:00:56 +01:00
"github.com/photoprism/photoprism/pkg/fs"
2020-04-26 14:31:33 +02:00
"github.com/photoprism/photoprism/pkg/txt"
2020-01-02 00:03:07 +01:00
)
2020-02-21 01:14:45 +01:00
// ClientConfig contains HTTP client / Web UI config values
2020-01-02 00:03:07 +01:00
type ClientConfig map [ string ] interface { }
2020-04-12 18:00:31 +02:00
// Flags returns config flags as string slice.
func ( c * Config ) Flags ( ) ( flags [ ] string ) {
2020-01-22 16:54:01 +01:00
if c . Public ( ) {
flags = append ( flags , "public" )
}
2020-04-12 18:00:31 +02:00
2020-01-22 16:54:01 +01:00
if c . Debug ( ) {
flags = append ( flags , "debug" )
}
2020-04-12 18:00:31 +02:00
2020-01-22 16:54:01 +01:00
if c . Experimental ( ) {
flags = append ( flags , "experimental" )
}
2020-04-12 18:00:31 +02:00
2020-01-22 16:54:01 +01:00
if c . ReadOnly ( ) {
flags = append ( flags , "readonly" )
}
2020-04-12 18:00:31 +02:00
if ! c . DisableSettings ( ) {
flags = append ( flags , "settings" )
}
return flags
}
// PublicClientConfig returns reduced config values for non-public sites.
func ( c * Config ) PublicClientConfig ( ) ClientConfig {
if c . Public ( ) {
return c . ClientConfig ( )
}
jsHash := fs . Checksum ( c . HttpStaticBuildPath ( ) + "/app.js" )
cssHash := fs . Checksum ( c . HttpStaticBuildPath ( ) + "/app.css" )
configFlags := c . Flags ( )
2020-01-22 16:54:01 +01:00
var noPos = struct {
PhotoUUID string ` json:"photo" `
LocationID string ` json:"location" `
TakenAt time . Time ` json:"utc" `
PhotoLat float64 ` json:"lat" `
PhotoLng float64 ` json:"lng" `
} { }
var count = struct {
Photos uint ` json:"photos" `
2020-05-14 19:03:12 +02:00
Videos uint ` json:"videos" `
2020-05-08 12:01:22 +02:00
Hidden uint ` json:"hidden" `
2020-01-22 16:54:01 +01:00
Favorites uint ` json:"favorites" `
Private uint ` json:"private" `
Stories uint ` json:"stories" `
Labels uint ` json:"labels" `
Albums uint ` json:"albums" `
Countries uint ` json:"countries" `
Places uint ` json:"places" `
} { }
result := ClientConfig {
2020-04-12 18:00:31 +02:00
"settings" : c . Settings ( ) ,
"flags" : strings . Join ( configFlags , " " ) ,
"name" : c . Name ( ) ,
"url" : c . Url ( ) ,
"title" : c . Title ( ) ,
"subtitle" : c . Subtitle ( ) ,
"description" : c . Description ( ) ,
"author" : c . Author ( ) ,
"version" : c . Version ( ) ,
"copyright" : c . Copyright ( ) ,
"debug" : c . Debug ( ) ,
"readonly" : c . ReadOnly ( ) ,
"uploadNSFW" : c . UploadNSFW ( ) ,
"public" : c . Public ( ) ,
"experimental" : c . Experimental ( ) ,
"disableSettings" : c . DisableSettings ( ) ,
"albums" : [ ] string { } ,
"cameras" : [ ] string { } ,
"lenses" : [ ] string { } ,
"countries" : [ ] string { } ,
"thumbnails" : Thumbnails ,
"jsHash" : jsHash ,
"cssHash" : cssHash ,
"count" : count ,
"pos" : noPos ,
"years" : [ ] int { } ,
"colors" : colors . All . List ( ) ,
"categories" : [ ] string { } ,
2020-04-26 14:31:33 +02:00
"clip" : txt . ClipDefault ,
2020-05-05 15:42:54 +02:00
"server" : RuntimeInfo { } ,
2020-01-22 16:54:01 +01:00
}
return result
}
2020-01-02 00:03:07 +01:00
// ClientConfig returns a loaded and set configuration entity.
func ( c * Config ) ClientConfig ( ) ClientConfig {
2020-04-30 15:41:47 +02:00
defer log . Debug ( capture . Time ( time . Now ( ) , "config: client config created" ) )
2020-01-02 00:03:07 +01:00
db := c . Db ( )
var cameras [ ] * entity . Camera
var lenses [ ] * entity . Lens
var albums [ ] * entity . Album
var position struct {
PhotoUUID string ` json:"photo" `
LocationID string ` json:"location" `
TakenAt time . Time ` json:"utc" `
PhotoLat float64 ` json:"lat" `
PhotoLng float64 ` json:"lng" `
}
db . Table ( "photos" ) .
Select ( "photo_uuid, location_id, photo_lat, photo_lng, taken_at" ) .
Where ( "deleted_at IS NULL AND photo_lat != 0 AND photo_lng != 0" ) .
Order ( "taken_at DESC" ) .
Limit ( 1 ) . Offset ( 0 ) .
Take ( & position )
var count = struct {
2020-05-10 19:43:49 +02:00
Photos uint ` json:"photos" `
2020-05-14 19:03:12 +02:00
Videos uint ` json:"videos" `
2020-05-10 19:43:49 +02:00
Hidden uint ` json:"hidden" `
Favorites uint ` json:"favorites" `
Private uint ` json:"private" `
Albums uint ` json:"albums" `
Countries uint ` json:"countries" `
Places uint ` json:"places" `
Labels uint ` json:"labels" `
LabelMaxPhotos uint ` json:"labelMaxPhotos" `
2020-01-02 00:03:07 +01:00
} { }
db . Table ( "photos" ) .
2020-05-14 19:03:12 +02:00
Select ( "SUM(photo_video = 1 AND photo_quality >= 0) AS videos, SUM(photo_quality = -1) AS hidden, SUM(photo_quality >= 0) AS photos, SUM(photo_favorite) AS favorites, SUM(photo_private) AS private" ) .
2020-05-18 15:45:55 +02:00
Where ( "photos.id NOT IN (SELECT photo_id FROM files WHERE file_primary = 1 AND (file_missing = 1 OR file_error <> ''))" ) .
2020-01-02 00:03:07 +01:00
Where ( "deleted_at IS NULL" ) .
Take ( & count )
db . Table ( "labels" ) .
2020-05-10 19:43:49 +02:00
Select ( "MAX(photo_count) as label_max_photos, COUNT(*) AS labels" ) .
Where ( "photo_count > 0" ) .
Where ( "deleted_at IS NULL" ) .
Where ( "(label_priority >= 0 || label_favorite = 1)" ) .
2020-01-02 00:03:07 +01:00
Take ( & count )
db . Table ( "albums" ) .
Select ( "COUNT(*) AS albums" ) .
Where ( "deleted_at IS NULL" ) .
Take ( & count )
db . Table ( "countries" ) .
Select ( "(COUNT(*) - 1) AS countries" ) .
Take ( & count )
db . Table ( "places" ) .
2020-05-10 16:12:15 +02:00
Select ( "SUM(photo_count > 0) AS places" ) .
Where ( "id != 'zz'" ) .
2020-01-02 00:03:07 +01:00
Take ( & count )
type country struct {
ID string ` json:"code" `
CountryName string ` json:"name" `
}
var countries [ ] country
db . Model ( & entity . Country { } ) .
Select ( "id, country_name" ) .
2020-04-25 14:22:47 +02:00
Order ( "country_slug" ) .
2020-01-02 00:03:07 +01:00
Scan ( & countries )
db . Where ( "deleted_at IS NULL" ) .
2020-04-25 14:22:47 +02:00
Limit ( 10000 ) . Order ( "camera_slug" ) .
2020-01-02 00:03:07 +01:00
Find ( & cameras )
db . Where ( "deleted_at IS NULL" ) .
2020-04-25 14:22:47 +02:00
Limit ( 10000 ) . Order ( "lens_slug" ) .
2020-01-02 00:03:07 +01:00
Find ( & lenses )
db . Where ( "deleted_at IS NULL AND album_favorite = 1" ) .
Limit ( 20 ) . Order ( "album_name" ) .
Find ( & albums )
var years [ ] int
db . Table ( "photos" ) .
2020-05-15 15:29:56 +02:00
Where ( "photo_year > 0" ) .
2020-01-02 00:03:07 +01:00
Order ( "photo_year DESC" ) .
Pluck ( "DISTINCT photo_year" , & years )
type CategoryLabel struct {
LabelName string
Title string
}
var categories [ ] CategoryLabel
db . Table ( "categories" ) .
Select ( "l.label_name" ) .
Joins ( "JOIN labels l ON categories.category_id = l.id" ) .
Group ( "l.label_name" ) .
Order ( "l.label_name" ) .
Limit ( 1000 ) . Offset ( 0 ) .
Scan ( & categories )
for i , l := range categories {
categories [ i ] . Title = strings . Title ( l . LabelName )
}
2020-02-01 20:52:28 +01:00
jsHash := fs . Checksum ( c . HttpStaticBuildPath ( ) + "/app.js" )
cssHash := fs . Checksum ( c . HttpStaticBuildPath ( ) + "/app.css" )
2020-04-12 18:00:31 +02:00
configFlags := c . Flags ( )
2020-01-02 00:03:07 +01:00
result := ClientConfig {
2020-04-12 18:00:31 +02:00
"flags" : strings . Join ( configFlags , " " ) ,
"name" : c . Name ( ) ,
"url" : c . Url ( ) ,
"title" : c . Title ( ) ,
"subtitle" : c . Subtitle ( ) ,
"description" : c . Description ( ) ,
"author" : c . Author ( ) ,
"version" : c . Version ( ) ,
"copyright" : c . Copyright ( ) ,
"debug" : c . Debug ( ) ,
"readonly" : c . ReadOnly ( ) ,
"uploadNSFW" : c . UploadNSFW ( ) ,
"public" : c . Public ( ) ,
"experimental" : c . Experimental ( ) ,
"disableSettings" : c . DisableSettings ( ) ,
"albums" : albums ,
"cameras" : cameras ,
"lenses" : lenses ,
"countries" : countries ,
"thumbnails" : Thumbnails ,
"jsHash" : jsHash ,
"cssHash" : cssHash ,
"settings" : c . Settings ( ) ,
"count" : count ,
"pos" : position ,
"years" : years ,
"colors" : colors . All . List ( ) ,
"categories" : categories ,
2020-04-26 14:31:33 +02:00
"clip" : txt . ClipDefault ,
2020-05-05 15:42:54 +02:00
"server" : NewRuntimeInfo ( ) ,
2020-01-02 00:03:07 +01:00
}
return result
}