2020-01-02 00:03:07 +01:00
package config
import (
"strings"
"time"
"github.com/photoprism/photoprism/internal/entity"
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-09-06 14:18:40 +02:00
// ClientConfig represents 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" `
2020-06-26 14:26:36 +02:00
SitePreview string ` json:"sitePreview" `
2020-05-31 02:09:52 +02:00
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" `
2020-07-13 17:25:27 +02:00
Thumbs [ ] Thumb ` json:"thumbs" `
2020-10-04 22:22:53 +02:00
Status string ` json:"status" `
2020-10-04 04:47:54 +02:00
MapKey string ` json:"mapKey" `
2020-05-27 19:38:40 +02:00
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-07-11 23:43:29 +02:00
PhotoUID string ` json:"uid" `
2020-07-12 08:27:05 +02:00
CellID string ` json:"cid" `
2020-07-11 23:43:29 +02:00
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-25 01:20:58 +02:00
// PublicConfig returns public client config values with as little information as possible.
2020-06-22 20:15:08 +02:00
func ( c * Config ) PublicConfig ( ) ClientConfig {
2020-04-12 18:00:31 +02:00
if c . Public ( ) {
2020-06-25 01:20:58 +02:00
return c . UserConfig ( )
2020-04-12 18:00:31 +02:00
}
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 ,
2020-06-23 12:37:37 +02:00
Maps : settings . Maps ,
Features : settings . Features ,
2020-06-22 20:41:18 +02:00
} ,
2020-05-31 02:09:52 +02:00
Flags : strings . Join ( c . Flags ( ) , " " ) ,
Name : c . Name ( ) ,
SiteUrl : c . SiteUrl ( ) ,
2020-06-26 14:26:36 +02:00
SitePreview : c . SitePreview ( ) ,
2020-05-31 02:09:52 +02:00
SiteTitle : c . SiteTitle ( ) ,
SiteCaption : c . SiteCaption ( ) ,
SiteDescription : c . SiteDescription ( ) ,
SiteAuthor : c . SiteAuthor ( ) ,
Version : c . Version ( ) ,
Copyright : c . Copyright ( ) ,
Debug : c . Debug ( ) ,
ReadOnly : c . ReadOnly ( ) ,
2020-08-19 12:45:26 +02:00
DisableSettings : c . SettingsHidden ( ) ,
2020-05-31 02:09:52 +02:00
Public : c . Public ( ) ,
Experimental : c . Experimental ( ) ,
2020-10-04 22:22:53 +02:00
Status : "" ,
2020-10-04 04:47:54 +02:00
MapKey : "" ,
2020-07-13 17:25:27 +02:00
Thumbs : Thumbs ,
2020-05-31 02:09:52 +02:00
Colors : colors . All . List ( ) ,
2020-06-26 16:11:56 +02:00
JSHash : fs . Checksum ( c . BuildPath ( ) + "/app.js" ) ,
CSSHash : fs . Checksum ( c . BuildPath ( ) + "/app.css" ) ,
2020-05-31 02:09:52 +02:00
Clip : txt . ClipDefault ,
PreviewToken : "public" ,
DownloadToken : "public" ,
2020-01-22 16:54:01 +01:00
}
return result
}
2020-06-25 01:20:58 +02:00
// GuestConfig returns client config values for the sharing with guests.
func ( c * Config ) GuestConfig ( ) ClientConfig {
2020-06-22 20:15:08 +02:00
settings := c . Settings ( )
result := ClientConfig {
2020-06-22 20:41:18 +02:00
Settings : Settings {
Language : settings . Language ,
Theme : settings . Theme ,
2020-06-23 12:37:37 +02:00
Maps : settings . Maps ,
Features : settings . Features ,
2020-06-22 20:41:18 +02:00
} ,
2020-06-22 20:15:08 +02:00
Flags : "readonly public shared" ,
Name : c . Name ( ) ,
SiteUrl : c . SiteUrl ( ) ,
2020-06-26 14:26:36 +02:00
SitePreview : c . SitePreview ( ) ,
2020-06-22 20:15:08 +02:00
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 ( ) ,
2020-07-13 17:25:27 +02:00
Thumbs : Thumbs ,
2020-12-04 13:10:32 +01:00
Status : c . Hub ( ) . Status ,
MapKey : c . Hub ( ) . MapKey ( ) ,
2020-06-22 20:15:08 +02:00
DownloadToken : c . DownloadToken ( ) ,
PreviewToken : c . PreviewToken ( ) ,
2020-06-26 16:11:56 +02:00
JSHash : fs . Checksum ( c . BuildPath ( ) + "/share.js" ) ,
CSSHash : fs . Checksum ( c . BuildPath ( ) + "/share.css" ) ,
2020-06-22 20:15:08 +02:00
Clip : txt . ClipDefault ,
}
return result
}
2020-06-25 01:20:58 +02:00
// UserConfig returns client configuration values for registered users.
func ( c * Config ) UserConfig ( ) ClientConfig {
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 ( ) ,
2020-06-26 14:26:36 +02:00
SitePreview : c . SitePreview ( ) ,
2020-05-31 02:09:52 +02:00
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 ( ) ,
2020-07-13 17:25:27 +02:00
Thumbs : Thumbs ,
2020-12-04 13:10:32 +01:00
Status : c . Hub ( ) . Status ,
MapKey : c . Hub ( ) . MapKey ( ) ,
2020-05-27 19:38:40 +02:00
DownloadToken : c . DownloadToken ( ) ,
2020-05-27 19:56:56 +02:00
PreviewToken : c . PreviewToken ( ) ,
2020-06-26 16:11:56 +02:00
JSHash : fs . Checksum ( c . BuildPath ( ) + "/app.js" ) ,
CSSHash : fs . Checksum ( c . BuildPath ( ) + "/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-07-12 08:27:05 +02:00
Select ( "photo_uid, cell_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
}