2022-12-08 19:14:06 +01:00
import { Prisma , PrismaClient } from "@prisma/client" ;
2022-12-02 15:10:49 +01:00
import * as crypto from "crypto" ;
2023-03-04 23:29:00 +01:00
const configVariables : ConfigVariables = {
internal : {
jwtSecret : {
description : "Long random string used to sign JWT tokens" ,
type : "string" ,
2023-03-23 08:31:21 +01:00
defaultValue : crypto.randomBytes ( 256 ) . toString ( "base64" ) ,
2023-03-04 23:29:00 +01:00
locked : true ,
} ,
} ,
general : {
appName : {
description : "Name of the application" ,
type : "string" ,
2023-03-23 08:31:21 +01:00
defaultValue : "Pingvin Share" ,
2023-03-04 23:29:00 +01:00
secret : false ,
} ,
appUrl : {
description : "On which URL Pingvin Share is available" ,
type : "string" ,
2023-03-23 08:31:21 +01:00
defaultValue : "http://localhost:3000" ,
2023-01-26 13:44:04 +01:00
2023-03-04 23:29:00 +01:00
secret : false ,
} ,
showHomePage : {
description : "Whether to show the home page" ,
type : "boolean" ,
2023-03-23 08:31:21 +01:00
defaultValue : "true" ,
2023-03-04 23:29:00 +01:00
secret : false ,
} ,
} ,
share : {
allowRegistration : {
description : "Whether registration is allowed" ,
type : "boolean" ,
2023-03-23 08:31:21 +01:00
defaultValue : "true" ,
2023-01-26 14:06:25 +01:00
2023-03-04 23:29:00 +01:00
secret : false ,
} ,
allowUnauthenticatedShares : {
description : "Whether unauthorized users can create shares" ,
type : "boolean" ,
2023-03-23 08:31:21 +01:00
defaultValue : "false" ,
2022-11-28 15:04:32 +01:00
2023-03-04 23:29:00 +01:00
secret : false ,
} ,
maxSize : {
description : "Maximum share size in bytes" ,
type : "number" ,
2023-03-23 08:31:21 +01:00
defaultValue : "1073741824" ,
2023-03-04 23:29:00 +01:00
secret : false ,
} ,
} ,
email : {
enableShareEmailRecipients : {
description :
"Whether to allow emails to share recipients. Only enable this if you have enabled SMTP." ,
type : "boolean" ,
2023-03-23 08:31:21 +01:00
defaultValue : "false" ,
2022-11-28 15:04:32 +01:00
2023-03-04 23:29:00 +01:00
secret : false ,
} ,
shareRecipientsSubject : {
description :
"Subject of the email which gets sent to the share recipients." ,
type : "string" ,
2023-03-23 08:31:21 +01:00
defaultValue : "Files shared with you" ,
2023-03-04 23:29:00 +01:00
} ,
shareRecipientsMessage : {
description :
2023-03-23 08:31:21 +01:00
"Message which gets sent to the share recipients.\n\nAvailable variables:\n{creator} - The username of the creator of the share\n{shareUrl} - The URL of the share\n{desc} - The description of the share\n{expires} - The expiration date of the share\n\nVariables will be replaced with the actual values." ,
2023-03-04 23:29:00 +01:00
type : "text" ,
2023-03-23 08:31:21 +01:00
defaultValue :
"Hey!\n\n{creator} shared some files with you, view or download the files with this link: {shareUrl}\n\nThe share will expire {expires}.\n\nNote: {desc}\n\nShared securely with Pingvin Share 🐧" ,
2023-03-04 23:29:00 +01:00
} ,
reverseShareSubject : {
description :
"Subject of the email which gets sent when someone created a share with your reverse share link." ,
type : "string" ,
2023-03-23 08:31:21 +01:00
defaultValue : "Reverse share link used" ,
2023-03-04 23:29:00 +01:00
} ,
reverseShareMessage : {
description :
"Message which gets sent when someone created a share with your reverse share link. {shareUrl} will be replaced with the creator's name and the share URL." ,
type : "text" ,
2023-03-23 08:31:21 +01:00
defaultValue :
"Hey!\n\nA share was just created with your reverse share link: {shareUrl}\n\nShared securely with Pingvin Share 🐧" ,
2023-03-04 23:29:00 +01:00
} ,
resetPasswordSubject : {
description :
"Subject of the email which gets sent when a user requests a password reset." ,
type : "string" ,
2023-03-23 08:31:21 +01:00
defaultValue : "Pingvin Share password reset" ,
2023-03-04 23:29:00 +01:00
} ,
resetPasswordMessage : {
description :
"Message which gets sent when a user requests a password reset. {url} will be replaced with the reset password URL." ,
type : "text" ,
2023-03-23 08:31:21 +01:00
defaultValue :
"Hey!\n\nYou requested a password reset. Click this link to reset your password: {url}\nThe link expires in a hour.\n\nPingvin Share 🐧" ,
2023-03-04 23:29:00 +01:00
} ,
inviteSubject : {
description :
"Subject of the email which gets sent when an admin invites an user." ,
type : "string" ,
2023-03-23 08:31:21 +01:00
defaultValue : "Pingvin Share invite" ,
2023-03-04 23:29:00 +01:00
} ,
inviteMessage : {
description :
"Message which gets sent when an admin invites an user. {url} will be replaced with the invite URL and {password} with the password." ,
type : "text" ,
2023-03-23 08:31:21 +01:00
defaultValue :
"Hey!\n\nYou were invited to Pingvin Share. Click this link to accept the invite: {url}\n\nYour password is: {password}\n\nPingvin Share 🐧" ,
2023-03-04 23:29:00 +01:00
} ,
} ,
smtp : {
enabled : {
description :
"Whether SMTP is enabled. Only set this to true if you entered the host, port, email, user and password of your SMTP server." ,
type : "boolean" ,
2023-03-23 08:31:21 +01:00
defaultValue : "false" ,
2023-03-04 23:29:00 +01:00
secret : false ,
} ,
host : {
description : "Host of the SMTP server" ,
type : "string" ,
2023-03-23 08:31:21 +01:00
defaultValue : "" ,
2023-03-04 23:29:00 +01:00
} ,
port : {
description : "Port of the SMTP server" ,
type : "number" ,
2023-03-23 08:31:21 +01:00
defaultValue : "0" ,
2023-03-04 23:29:00 +01:00
} ,
email : {
description : "Email address which the emails get sent from" ,
type : "string" ,
2023-03-23 08:31:21 +01:00
defaultValue : "" ,
2023-03-04 23:29:00 +01:00
} ,
username : {
description : "Username of the SMTP server" ,
type : "string" ,
2023-03-23 08:31:21 +01:00
defaultValue : "" ,
2023-03-04 23:29:00 +01:00
} ,
password : {
description : "Password of the SMTP server" ,
type : "string" ,
2023-03-23 08:31:21 +01:00
defaultValue : "" ,
2023-03-04 23:29:00 +01:00
obscured : true ,
} ,
} ,
} ;
2022-11-28 15:04:32 +01:00
2023-03-04 23:29:00 +01:00
type ConfigVariables = {
[ category : string ] : {
[ variable : string ] : Omit <
Prisma . ConfigCreateInput ,
"name" | "category" | "order"
> ;
} ;
} ;
const prisma = new PrismaClient ( ) ;
async function seedConfigVariables() {
for ( const [ category , configVariablesOfCategory ] of Object . entries (
configVariables
) ) {
let order = 0 ;
for ( const [ name , properties ] of Object . entries (
configVariablesOfCategory
) ) {
const existingConfigVariable = await prisma . config . findUnique ( {
where : { name_category : { name , category } } ,
2022-11-28 15:04:32 +01:00
} ) ;
2023-03-04 23:29:00 +01:00
// Create a new config variable if it doesn't exist
if ( ! existingConfigVariable ) {
await prisma . config . create ( {
data : {
order ,
name ,
. . . properties ,
category ,
} ,
} ) ;
}
order ++ ;
2022-11-28 15:04:32 +01:00
}
}
2023-03-04 23:29:00 +01:00
}
2022-11-28 15:04:32 +01:00
2023-03-04 23:29:00 +01:00
async function migrateConfigVariables() {
const existingConfigVariables = await prisma . config . findMany ( ) ;
2022-11-28 15:04:32 +01:00
2023-03-04 23:29:00 +01:00
for ( const existingConfigVariable of existingConfigVariables ) {
const configVariable =
configVariables [ existingConfigVariable . category ] ? . [
existingConfigVariable . name
] ;
2022-12-08 20:00:04 +01:00
if ( ! configVariable ) {
2022-11-28 15:04:32 +01:00
await prisma . config . delete ( {
2023-03-04 23:29:00 +01:00
where : {
name_category : {
name : existingConfigVariable.name ,
category : existingConfigVariable.category ,
} ,
} ,
2022-11-28 15:04:32 +01:00
} ) ;
2022-12-08 20:00:04 +01:00
// Update the config variable if the metadata changed
} else if (
JSON . stringify ( {
2022-12-08 21:58:58 +01:00
. . . configVariable ,
2023-03-04 23:29:00 +01:00
name : existingConfigVariable.name ,
category : existingConfigVariable.category ,
value : existingConfigVariable.value ,
} ) != JSON . stringify ( existingConfigVariable )
2022-12-08 20:00:04 +01:00
) {
await prisma . config . update ( {
2023-03-04 23:29:00 +01:00
where : {
name_category : {
name : existingConfigVariable.name ,
category : existingConfigVariable.category ,
} ,
} ,
2022-12-08 21:58:58 +01:00
data : {
. . . configVariable ,
2023-03-04 23:29:00 +01:00
name : existingConfigVariable.name ,
category : existingConfigVariable.category ,
value : existingConfigVariable.value ,
2022-12-08 21:58:58 +01:00
} ,
2022-12-08 20:00:04 +01:00
} ) ;
2022-11-28 15:04:32 +01:00
}
}
}
2023-03-04 23:29:00 +01:00
seedConfigVariables ( )
. then ( ( ) = > migrateConfigVariables ( ) )
2022-11-28 15:04:32 +01:00
. then ( async ( ) = > {
await prisma . $disconnect ( ) ;
} )
. catch ( async ( e ) = > {
console . error ( e ) ;
await prisma . $disconnect ( ) ;
process . exit ( 1 ) ;
} ) ;