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 {
Flags string ` json:"flags" `
Name string ` json:"name" `
URL string ` json:"url" `
Title string ` json:"title" `
Subtitle string ` json:"subtitle" `
Description string ` json:"description" `
Author string ` json:"author" `
Version string ` json:"version" `
Copyright string ` json:"copyright" `
Debug bool ` json:"debug" `
ReadOnly bool ` json:"readonly" `
UploadNSFW bool ` json:"uploadNSFW" `
Public bool ` json:"public" `
Experimental bool ` json:"experimental" `
DisableSettings bool ` json:"disableSettings" `
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 {
Photos uint ` json:"photos" `
Videos uint ` json:"videos" `
Hidden uint ` json:"hidden" `
Favorites uint ` json:"favorites" `
Private uint ` json:"private" `
Review uint ` json:"review" `
Stories uint ` json:"stories" `
Albums uint ` json:"albums" `
Folders uint ` json:"folders" `
Files uint ` json:"files" `
Moments uint ` json:"moments" `
Countries uint ` json:"countries" `
Places uint ` json:"places" `
Labels uint ` json:"labels" `
LabelMaxPhotos uint ` json:"labelMaxPhotos" `
}
type CategoryLabel struct {
LabelUID string ` json:"UID" `
CustomSlug string ` json:"Slug" `
LabelName string ` json:"Name" `
}
type ClientPosition struct {
PhotoUID string ` json:"uid" `
LocUID string ` json:"loc" `
TakenAt time . Time ` json:"utc" `
PhotoLat float64 ` json:"lat" `
PhotoLng float64 ` json:"lng" `
}
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-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 ( )
}
2020-05-27 19:38:40 +02:00
settings := c . Settings ( )
2020-01-22 16:54:01 +01:00
result := ClientConfig {
2020-05-27 19:38:40 +02:00
Settings : Settings { Language : settings . Language , Theme : settings . Theme } ,
Flags : strings . Join ( c . Flags ( ) , " " ) ,
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 ( ) ,
Public : c . Public ( ) ,
Experimental : c . Experimental ( ) ,
Thumbnails : Thumbnails ,
Colors : colors . All . List ( ) ,
JSHash : fs . Checksum ( c . HttpStaticBuildPath ( ) + "/app.js" ) ,
CSSHash : fs . Checksum ( c . HttpStaticBuildPath ( ) + "/app.css" ) ,
Clip : txt . ClipDefault ,
2020-05-27 19:56:56 +02:00
PreviewToken : "public" ,
2020-05-27 19:38:40 +02:00
DownloadToken : "public" ,
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-05-27 19:38:40 +02:00
result := ClientConfig {
Settings : * c . Settings ( ) ,
Flags : strings . Join ( c . Flags ( ) , " " ) ,
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 ( ) ,
DisableSettings : c . DisableSettings ( ) ,
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-27 19:38:40 +02:00
JSHash : fs . Checksum ( c . HttpStaticBuildPath ( ) + "/app.js" ) ,
CSSHash : fs . Checksum ( c . HttpStaticBuildPath ( ) + "/app.css" ) ,
Clip : txt . ClipDefault ,
Server : NewRuntimeInfo ( ) ,
2020-01-02 00:03:07 +01:00
}
2020-05-27 19:38:40 +02:00
db := c . Db ( )
2020-01-02 00:03:07 +01:00
db . Table ( "photos" ) .
2020-05-25 19:10:44 +02:00
Select ( "photo_uid, loc_uid, 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
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" `
2020-05-23 20:58:58 +02:00
Review uint ` json:"review" `
2020-05-10 19:43:49 +02:00
Albums uint ` json:"albums" `
2020-05-23 20:58:58 +02:00
Folders uint ` json:"folders" `
2020-05-24 22:16:06 +02:00
Files uint ` json:"files" `
2020-05-23 20:58:58 +02:00
Moments uint ` json:"moments" `
2020-05-10 19:43:49 +02:00
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-23 20:58:58 +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_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
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-05-27 19:38:40 +02:00
Take ( & result . Count )
2020-01-02 00:03:07 +01:00
db . Table ( "albums" ) .
2020-05-23 20:58:58 +02:00
Select ( "SUM(album_type = '') AS albums, SUM(album_type = 'moment') AS moments, SUM(album_type = 'folder') AS folders" ) .
Where ( "deleted_at IS NULL" ) .
2020-05-27 19:38:40 +02:00
Take ( & result . Count )
2020-05-23 20:58:58 +02:00
db . Table ( "folders" ) .
Select ( "COUNT(*) AS folders" ) .
2020-05-26 09:02:19 +02:00
Where ( "folder_ignore = 0" ) .
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-24 22:16:06 +02:00
db . Table ( "files" ) .
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-01-02 00:03:07 +01:00
db . Table ( "countries" ) .
Select ( "(COUNT(*) - 1) AS countries" ) .
2020-05-27 19:38:40 +02:00
Take ( & result . Count )
2020-01-02 00:03:07 +01:00
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 )
2020-05-23 20:58:58 +02:00
db . Order ( "country_slug" ) .
2020-05-27 19:38:40 +02:00
Find ( & result . Countries )
2020-01-02 00:03:07 +01:00
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
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
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
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
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
return result
}