2015-07-20 00:09:47 -07:00
'use strict' ;
exports = module . exports = {
2023-08-02 19:14:18 +05:30
get ,
set ,
2023-08-03 11:34:33 +05:30
getJson ,
setJson ,
2023-08-02 19:17:22 +05:30
getBlob ,
setBlob ,
2023-08-04 15:34:38 +05:30
APPSTORE _API _TOKEN _KEY : 'appstore_api_token' ,
API _SERVER _ORIGIN _KEY : 'api_server_origin' ,
AUTOUPDATE _PATTERN _KEY : 'autoupdate_pattern' ,
2023-08-15 08:14:35 +05:30
BACKUP _STORAGE _KEY : 'backup_storage' ,
BACKUP _LIMITS _KEY : 'backup_limits' ,
2023-07-12 10:01:53 +05:30
BACKUP _POLICY _KEY : 'backup_policy' ,
2023-08-04 15:34:38 +05:30
CLOUDRON _AVATAR _KEY : 'cloudron_avatar' ,
2024-04-03 17:27:22 +02:00
CLOUDRON _BACKGROUND _KEY : 'cloudron_background' ,
2023-08-04 15:34:38 +05:30
CLOUDRON _ID _KEY : 'cloudron_id' ,
CLOUDRON _NAME _KEY : 'cloudron_name' ,
CONSOLE _SERVER _ORIGIN _KEY : 'console_server_origin' ,
2023-08-16 19:28:04 +05:30
DASHBOARD _DOMAIN _KEY : 'dashboard_domain' ,
DASHBOARD _SUBDOMAIN _KEY : 'dashboard_subdomain' ,
2023-08-03 07:35:14 +05:30
DIRECTORY _SERVER _KEY : 'directory_server_config' ,
2023-08-04 15:34:38 +05:30
DYNAMIC _DNS _KEY : 'dynamic_dns' ,
EXTERNAL _LDAP _KEY : 'external_ldap_config' ,
FOOTER _KEY : 'footer' ,
FIREWALL _BLOCKLIST _KEY : 'firewall_blocklist' ,
GHOSTS _CONFIG _KEY : 'ghosts_config' ,
2023-08-03 06:05:29 +05:30
IPV4 _CONFIG _KEY : 'ipv4_config' ,
2023-08-04 15:34:38 +05:30
IPV6 _CONFIG _KEY : 'ipv6_config' ,
LANGUAGE _KEY : 'language' ,
MAIL _DOMAIN _KEY : 'mail_domain' ,
2023-08-16 19:28:04 +05:30
MAIL _SUBDOMAIN _KEY : 'mail_subdomain' ,
2023-08-04 15:34:38 +05:30
OIDC _COOKIE _SECRET _KEY : 'cookie_secret' ,
2022-01-13 14:34:02 -08:00
PROFILE _CONFIG _KEY : 'profile_config' ,
2021-09-22 09:13:16 -07:00
REVERSE _PROXY _CONFIG _KEY : 'reverseproxy_config' ,
2023-08-04 15:34:38 +05:30
SERVICES _CONFIG _KEY : 'services_config' ,
2017-07-18 13:31:43 -07:00
TIME _ZONE _KEY : 'time_zone' ,
2023-05-13 14:59:57 +02:00
TRUSTED _IPS _KEY : 'trusted_ips_key' ,
2019-07-25 11:16:52 -07:00
WEB _SERVER _ORIGIN _KEY : 'web_server_origin' ,
2019-07-26 10:49:29 -07:00
// testing
2021-08-19 13:24:38 -07:00
_clear : clear ,
_set : set
2015-07-20 00:09:47 -07:00
} ;
2021-01-20 11:10:17 -08:00
const assert = require ( 'assert' ) ,
2021-08-18 13:25:42 -07:00
database = require ( './database.js' ) ,
2023-08-04 11:24:28 +05:30
safe = require ( 'safetydance' ) ;
2015-07-20 00:09:47 -07:00
2021-08-18 13:25:42 -07:00
const SETTINGS _FIELDS = [ 'name' , 'value' ] . join ( ',' ) ;
2021-08-18 15:31:07 -07:00
const SETTINGS _BLOB _FIELDS = [ 'name' , 'valueBlob' ] . join ( ',' ) ;
2021-08-18 13:25:42 -07:00
2021-08-18 15:31:07 -07:00
async function get ( key ) {
assert . strictEqual ( typeof key , 'string' ) ;
const result = await database . query ( ` SELECT ${ SETTINGS _FIELDS } FROM settings WHERE name = ? ` , [ key ] ) ;
2021-08-18 15:40:28 -07:00
if ( result . length === 0 ) return null ; // can't return the default value here because we might need to massage/json parse the result
2021-08-18 15:31:07 -07:00
return result [ 0 ] . value ;
}
async function set ( key , value ) {
assert . strictEqual ( typeof key , 'string' ) ;
assert ( value === null || typeof value === 'string' ) ;
await database . query ( 'INSERT INTO settings (name, value) VALUES (?, ?) ON DUPLICATE KEY UPDATE value=VALUES(value)' , [ key , value ] ) ; // don't rely on affectedRows here since it gives 2
}
2023-08-03 11:34:33 +05:30
async function getJson ( key ) {
assert . strictEqual ( typeof key , 'string' ) ;
return safe . JSON . parse ( await get ( key ) ) ;
}
async function setJson ( key , value ) {
assert . strictEqual ( typeof key , 'string' ) ;
assert . strictEqual ( typeof value , 'object' ) ; // can be null
await set ( key , value ? JSON . stringify ( value ) : null ) ;
}
2021-08-18 15:31:07 -07:00
async function getBlob ( key ) {
assert . strictEqual ( typeof key , 'string' ) ;
const result = await database . query ( ` SELECT ${ SETTINGS _BLOB _FIELDS } FROM settings WHERE name = ? ` , [ key ] ) ;
if ( result . length === 0 ) return null ;
return result [ 0 ] . valueBlob ;
}
async function setBlob ( key , value ) {
assert . strictEqual ( typeof key , 'string' ) ;
assert ( value === null || Buffer . isBuffer ( value ) ) ;
await database . query ( 'INSERT INTO settings (name, valueBlob) VALUES (?, ?) ON DUPLICATE KEY UPDATE valueBlob=VALUES(valueBlob)' , [ key , value ] ) ; // don't rely on affectedRows here since it gives 2
}
async function clear ( ) {
await database . query ( 'DELETE FROM settings' ) ;
}