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-05-27 19:38:40 +02:00
type ClientConfig struct {
Name string ` json:"name" `
Version string ` json:"version" `
Copyright string ` json:"copyright" `
2020-05-31 02:09:52 +02:00
Flags string ` json:"flags" `
SiteUrl string ` json:"siteUrl" `
SiteTitle string ` json:"siteTitle" `
SiteCaption string ` json:"siteCaption" `
SiteDescription string ` json:"siteDescription" `
SiteAuthor string ` json:"siteAuthor" `
2020-05-27 19:38:40 +02:00
Debug bool ` json:"debug" `
ReadOnly bool ` json:"readonly" `
UploadNSFW bool ` json:"uploadNSFW" `
Public bool ` json:"public" `
Experimental bool ` json:"experimental" `
DisableSettings bool ` json:"disableSettings" `
2020-05-30 21:11:56 +02:00
AlbumCategories [ ] string ` json:"albumCategories" `
2020-05-27 19:38:40 +02:00
Albums [ ] entity . Album ` json:"albums" `
Cameras [ ] entity . Camera ` json:"cameras" `
Lenses [ ] entity . Lens ` json:"lenses" `
Countries [ ] entity . Country ` json:"countries" `
Thumbnails [ ] Thumbnail ` json:"thumbnails" `
DownloadToken string ` json:"downloadToken" `
2020-05-27 19:56:56 +02:00
PreviewToken string ` json:"previewToken" `
2020-05-27 19:38:40 +02:00
JSHash string ` json:"jsHash" `
CSSHash string ` json:"cssHash" `
Settings Settings ` json:"settings" `
Count ClientCounts ` json:"count" `
Pos ClientPosition ` json:"pos" `
Years [ ] int ` json:"years" `
Colors [ ] map [ string ] string ` json:"colors" `
Categories [ ] CategoryLabel ` json:"categories" `
Clip int ` json:"clip" `
Server RuntimeInfo ` json:"server" `
}
type ClientCounts struct {
2020-05-30 01:41:47 +02:00
Cameras int ` json:"cameras" `
Lenses int ` json:"lenses" `
Countries int ` json:"countries" `
Photos int ` json:"photos" `
Videos int ` json:"videos" `
Hidden int ` json:"hidden" `
Favorites int ` json:"favorites" `
Private int ` json:"private" `
Review int ` json:"review" `
Stories int ` json:"stories" `
Albums int ` json:"albums" `
Moments int ` json:"moments" `
Months int ` json:"months" `
Folders int ` json:"folders" `
Files int ` json:"files" `
Places int ` json:"places" `
2020-06-08 18:32:51 +02:00
States int ` json:"states" `
2020-05-30 01:41:47 +02:00
Labels int ` json:"labels" `
LabelMaxPhotos int ` json:"labelMaxPhotos" `
2020-05-27 19:38:40 +02:00
}
type CategoryLabel struct {
LabelUID string ` json:"UID" `
CustomSlug string ` json:"Slug" `
LabelName string ` json:"Name" `
}
type ClientPosition struct {
2020-05-29 12:56:24 +02:00
PhotoUID string ` json:"uid" `
LocationID string ` json:"loc" `
TakenAt time . Time ` json:"utc" `
PhotoLat float64 ` json:"lat" `
PhotoLng float64 ` json:"lng" `
2020-05-27 19:38:40 +02:00
}
2020-01-02 00:03:07 +01:00
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-05-31 02:09:52 +02:00
if ! c . SettingsHidden ( ) {
2020-04-12 18:00:31 +02:00
flags = append ( flags , "settings" )
}
return flags
}
2020-06-22 20:15:08 +02:00
// PublicConfig returns public client config values.
func ( c * Config ) PublicConfig ( ) ClientConfig {
2020-04-12 18:00:31 +02:00
if c . Public ( ) {
return c . ClientConfig ( )
}
2020-05-27 19:38:40 +02:00
settings := c . Settings ( )
2020-01-22 16:54:01 +01:00
result := ClientConfig {
2020-06-22 20:41:18 +02:00
Settings : Settings {
Language : settings . Language ,
Theme : settings . Theme ,
Maps : MapsSettings { Animate : settings . Maps . Animate , Style : settings . Maps . Style } ,
Features : FeatureSettings { Download : settings . Features . Download } ,
} ,
2020-05-31 02:09:52 +02:00
Flags : strings . Join ( c . Flags ( ) , " " ) ,
Name : c . Name ( ) ,
SiteUrl : c . SiteUrl ( ) ,
SiteTitle : c . SiteTitle ( ) ,
SiteCaption : c . SiteCaption ( ) ,
SiteDescription : c . SiteDescription ( ) ,
SiteAuthor : c . SiteAuthor ( ) ,
Version : c . Version ( ) ,
Copyright : c . Copyright ( ) ,
Debug : c . Debug ( ) ,
ReadOnly : c . ReadOnly ( ) ,
Public : c . Public ( ) ,
Experimental : c . Experimental ( ) ,
Thumbnails : Thumbnails ,
Colors : colors . All . List ( ) ,
JSHash : fs . Checksum ( c . StaticBuildPath ( ) + "/app.js" ) ,
CSSHash : fs . Checksum ( c . StaticBuildPath ( ) + "/app.css" ) ,
Clip : txt . ClipDefault ,
PreviewToken : "public" ,
DownloadToken : "public" ,
2020-01-22 16:54:01 +01:00
}
return result
}
2020-06-22 20:15:08 +02:00
// ShareConfig returns client config values for the sharing UI.
func ( c * Config ) ShareConfig ( ) ClientConfig {
defer log . Debug ( capture . Time ( time . Now ( ) , "config: share config created" ) )
settings := c . Settings ( )
result := ClientConfig {
2020-06-22 20:41:18 +02:00
Settings : Settings {
Language : settings . Language ,
Theme : settings . Theme ,
Maps : MapsSettings { Animate : settings . Maps . Animate , Style : settings . Maps . Style } ,
Features : FeatureSettings { Download : settings . Features . Download } ,
} ,
2020-06-22 20:15:08 +02:00
Flags : "readonly public shared" ,
Name : c . Name ( ) ,
SiteUrl : c . SiteUrl ( ) ,
SiteTitle : c . SiteTitle ( ) ,
SiteCaption : c . SiteCaption ( ) ,
SiteDescription : c . SiteDescription ( ) ,
SiteAuthor : c . SiteAuthor ( ) ,
Version : c . Version ( ) ,
Copyright : c . Copyright ( ) ,
Debug : c . Debug ( ) ,
ReadOnly : true ,
UploadNSFW : c . UploadNSFW ( ) ,
DisableSettings : true ,
Public : true ,
Experimental : false ,
Colors : colors . All . List ( ) ,
Thumbnails : Thumbnails ,
DownloadToken : c . DownloadToken ( ) ,
PreviewToken : c . PreviewToken ( ) ,
JSHash : fs . Checksum ( c . StaticBuildPath ( ) + "/share.js" ) ,
CSSHash : fs . Checksum ( c . StaticBuildPath ( ) + "/share.css" ) ,
Clip : txt . ClipDefault ,
}
return result
}
// ClientConfig returns client configuration values for the UI.
2020-01-02 00:03:07 +01:00
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-05-27 19:38:40 +02:00
result := ClientConfig {
Settings : * c . Settings ( ) ,
Flags : strings . Join ( c . Flags ( ) , " " ) ,
Name : c . Name ( ) ,
2020-05-31 02:09:52 +02:00
SiteUrl : c . SiteUrl ( ) ,
SiteTitle : c . SiteTitle ( ) ,
SiteCaption : c . SiteCaption ( ) ,
SiteDescription : c . SiteDescription ( ) ,
SiteAuthor : c . SiteAuthor ( ) ,
2020-05-27 19:38:40 +02:00
Version : c . Version ( ) ,
Copyright : c . Copyright ( ) ,
Debug : c . Debug ( ) ,
ReadOnly : c . ReadOnly ( ) ,
UploadNSFW : c . UploadNSFW ( ) ,
2020-05-31 02:09:52 +02:00
DisableSettings : c . SettingsHidden ( ) ,
2020-05-27 19:38:40 +02:00
Public : c . Public ( ) ,
Experimental : c . Experimental ( ) ,
Colors : colors . All . List ( ) ,
Thumbnails : Thumbnails ,
DownloadToken : c . DownloadToken ( ) ,
2020-05-27 19:56:56 +02:00
PreviewToken : c . PreviewToken ( ) ,
2020-05-31 02:09:52 +02:00
JSHash : fs . Checksum ( c . StaticBuildPath ( ) + "/app.js" ) ,
CSSHash : fs . Checksum ( c . StaticBuildPath ( ) + "/app.css" ) ,
2020-05-27 19:38:40 +02:00
Clip : txt . ClipDefault ,
Server : NewRuntimeInfo ( ) ,
2020-01-02 00:03:07 +01:00
}
2020-05-30 21:11:56 +02:00
c . Db ( ) . Table ( "photos" ) .
2020-05-29 12:56:24 +02:00
Select ( "photo_uid, location_id, photo_lat, photo_lng, taken_at" ) .
2020-01-02 00:03:07 +01:00
Where ( "deleted_at IS NULL AND photo_lat != 0 AND photo_lng != 0" ) .
Order ( "taken_at DESC" ) .
Limit ( 1 ) . Offset ( 0 ) .
2020-05-27 19:38:40 +02:00
Take ( & result . Pos )
2020-01-02 00:03:07 +01:00
2020-05-30 21:11:56 +02:00
c . Db ( ) . Table ( "cameras" ) .
2020-05-29 18:04:30 +02:00
Where ( "camera_slug <> 'zz' AND camera_slug <> ''" ) .
Select ( "COUNT(*) AS cameras" ) .
Take ( & result . Count )
2020-05-30 21:11:56 +02:00
c . Db ( ) . Table ( "lenses" ) .
2020-05-29 18:04:30 +02:00
Where ( "lens_slug <> 'zz' AND lens_slug <> ''" ) .
Select ( "COUNT(*) AS lenses" ) .
Take ( & result . Count )
2020-01-02 00:03:07 +01:00
2020-05-30 21:11:56 +02:00
c . Db ( ) . Table ( "photos" ) .
2020-06-04 17:33:00 +02:00
Select ( "SUM(photo_type = 'video' AND photo_quality >= 0 AND photo_private = 0) AS videos, SUM(photo_type IN ('image','raw','live') AND photo_quality < 3 AND photo_quality >= 0 AND photo_private = 0) AS review, SUM(photo_quality = -1) AS hidden, SUM(photo_type IN ('image','raw','live') AND photo_private = 0 AND photo_quality >= 0) AS photos, SUM(photo_favorite = 1 AND photo_private = 0 AND photo_quality >= 0) AS favorites, SUM(photo_private = 1 AND photo_quality >= 0) 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" ) .
2020-05-27 19:38:40 +02:00
Take ( & result . Count )
2020-01-02 00:03:07 +01:00
2020-05-30 21:11:56 +02:00
c . 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" ) .
2020-06-01 09:45:24 +02:00
Where ( "(label_priority >= 0 OR label_favorite = 1)" ) .
2020-05-27 19:38:40 +02:00
Take ( & result . Count )
2020-01-02 00:03:07 +01:00
2020-05-30 21:11:56 +02:00
c . Db ( ) . Table ( "albums" ) .
2020-06-08 18:32:51 +02:00
Select ( "SUM(album_type = ?) AS albums, SUM(album_type = ?) AS moments, SUM(album_type = ?) AS months, SUM(album_type = ?) AS states, SUM(album_type = ?) AS folders" , entity . AlbumDefault , entity . AlbumMoment , entity . AlbumMonth , entity . AlbumState , entity . AlbumFolder ) .
2020-01-02 00:03:07 +01:00
Where ( "deleted_at IS NULL" ) .
2020-05-27 19:38:40 +02:00
Take ( & result . Count )
2020-01-02 00:03:07 +01:00
2020-05-30 21:11:56 +02:00
c . Db ( ) . Table ( "files" ) .
2020-05-24 22:16:06 +02:00
Select ( "COUNT(*) AS files" ) .
Where ( "file_missing = 0" ) .
Where ( "deleted_at IS NULL" ) .
2020-05-27 19:38:40 +02:00
Take ( & result . Count )
2020-05-24 22:16:06 +02:00
2020-05-30 21:11:56 +02:00
c . Db ( ) . Table ( "countries" ) .
2020-01-02 00:03:07 +01:00
Select ( "(COUNT(*) - 1) AS countries" ) .
2020-05-27 19:38:40 +02:00
Take ( & result . Count )
2020-01-02 00:03:07 +01:00
2020-05-30 21:11:56 +02:00
c . Db ( ) . Table ( "places" ) .
2020-05-10 16:12:15 +02:00
Select ( "SUM(photo_count > 0) AS places" ) .
Where ( "id != 'zz'" ) .
2020-05-29 18:04:30 +02:00
Take ( & result . Count )
2020-01-02 00:03:07 +01:00
2020-05-30 21:11:56 +02:00
c . Db ( ) . Order ( "country_slug" ) .
2020-05-27 19:38:40 +02:00
Find ( & result . Countries )
2020-01-02 00:03:07 +01:00
2020-05-30 21:11:56 +02:00
c . Db ( ) . Where ( "deleted_at IS NULL" ) .
2020-04-25 14:22:47 +02:00
Limit ( 10000 ) . Order ( "camera_slug" ) .
2020-05-27 19:38:40 +02:00
Find ( & result . Cameras )
2020-01-02 00:03:07 +01:00
2020-05-30 21:11:56 +02:00
c . Db ( ) . Where ( "deleted_at IS NULL" ) .
2020-04-25 14:22:47 +02:00
Limit ( 10000 ) . Order ( "lens_slug" ) .
2020-05-27 19:38:40 +02:00
Find ( & result . Lenses )
2020-01-02 00:03:07 +01:00
2020-05-30 21:11:56 +02:00
c . Db ( ) . Where ( "deleted_at IS NULL AND album_favorite = 1" ) .
2020-05-26 09:02:19 +02:00
Limit ( 20 ) . Order ( "album_title" ) .
2020-05-27 19:38:40 +02:00
Find ( & result . Albums )
2020-01-02 00:03:07 +01:00
2020-05-30 21:11:56 +02:00
c . 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" ) .
2020-05-27 19:38:40 +02:00
Pluck ( "DISTINCT photo_year" , & result . Years )
2020-01-02 00:03:07 +01:00
2020-05-30 21:11:56 +02:00
c . Db ( ) . Table ( "categories" ) .
2020-05-23 20:58:58 +02:00
Select ( "l.label_uid, l.custom_slug, l.label_name" ) .
2020-01-02 00:03:07 +01:00
Joins ( "JOIN labels l ON categories.category_id = l.id" ) .
2020-05-23 20:58:58 +02:00
Where ( "l.deleted_at IS NULL" ) .
Group ( "l.custom_slug" ) .
Order ( "l.custom_slug" ) .
2020-01-02 00:03:07 +01:00
Limit ( 1000 ) . Offset ( 0 ) .
2020-05-27 19:38:40 +02:00
Scan ( & result . Categories )
2020-01-02 00:03:07 +01:00
2020-05-30 21:11:56 +02:00
c . Db ( ) . Table ( "albums" ) .
Select ( "album_category" ) .
Where ( "deleted_at IS NULL AND album_category <> ''" ) .
Group ( "album_category" ) .
Order ( "album_category" ) .
Limit ( 1000 ) . Offset ( 0 ) .
Pluck ( "album_category" , & result . AlbumCategories )
2020-01-02 00:03:07 +01:00
return result
}