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-07-20 15:32:07 +02:00
2023-03-04 23:29:00 +01:00
const configVariables : ConfigVariables = {
internal : {
jwtSecret : {
type : "string" ,
2023-11-03 13:06:59 +01:00
value : crypto.randomBytes ( 256 ) . toString ( "base64" ) ,
2023-03-04 23:29:00 +01:00
locked : true ,
} ,
} ,
general : {
appName : {
type : "string" ,
2023-03-23 08:31:21 +01:00
defaultValue : "Pingvin Share" ,
2023-03-04 23:29:00 +01:00
secret : false ,
} ,
appUrl : {
type : "string" ,
2023-03-23 08:31:21 +01:00
defaultValue : "http://localhost:3000" ,
2023-03-04 23:29:00 +01:00
secret : false ,
} ,
showHomePage : {
type : "boolean" ,
2023-03-23 08:31:21 +01:00
defaultValue : "true" ,
2023-03-04 23:29:00 +01:00
secret : false ,
} ,
} ,
share : {
allowRegistration : {
type : "boolean" ,
2023-03-23 08:31:21 +01:00
defaultValue : "true" ,
2023-03-04 23:29:00 +01:00
secret : false ,
} ,
allowUnauthenticatedShares : {
type : "boolean" ,
2023-03-23 08:31:21 +01:00
defaultValue : "false" ,
2023-03-04 23:29:00 +01:00
secret : false ,
} ,
2023-10-23 15:17:47 +02:00
maxExpiration : {
type : "number" ,
defaultValue : "0" ,
secret : false ,
} ,
2023-03-04 23:29:00 +01:00
maxSize : {
type : "number" ,
2023-07-22 15:33:45 +02:00
defaultValue : "1000000000" ,
2023-03-04 23:29:00 +01:00
secret : false ,
} ,
2023-07-22 15:44:45 +02:00
zipCompressionLevel : {
type : "number" ,
defaultValue : "9" ,
} ,
2023-03-04 23:29:00 +01:00
} ,
email : {
enableShareEmailRecipients : {
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 : {
type : "string" ,
2023-03-23 08:31:21 +01:00
defaultValue : "Files shared with you" ,
2023-03-04 23:29:00 +01:00
} ,
shareRecipientsMessage : {
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 : {
type : "string" ,
2023-03-23 08:31:21 +01:00
defaultValue : "Reverse share link used" ,
2023-03-04 23:29:00 +01:00
} ,
reverseShareMessage : {
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 : {
type : "string" ,
2023-03-23 08:31:21 +01:00
defaultValue : "Pingvin Share password reset" ,
2023-03-04 23:29:00 +01:00
} ,
resetPasswordMessage : {
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 : {
type : "string" ,
2023-03-23 08:31:21 +01:00
defaultValue : "Pingvin Share invite" ,
2023-03-04 23:29:00 +01:00
} ,
inviteMessage : {
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 : {
type : "boolean" ,
2023-03-23 08:31:21 +01:00
defaultValue : "false" ,
2023-03-04 23:29:00 +01:00
secret : false ,
} ,
host : {
type : "string" ,
2023-03-23 08:31:21 +01:00
defaultValue : "" ,
2023-03-04 23:29:00 +01:00
} ,
port : {
type : "number" ,
2023-03-23 08:31:21 +01:00
defaultValue : "0" ,
2023-03-04 23:29:00 +01:00
} ,
email : {
type : "string" ,
2023-03-23 08:31:21 +01:00
defaultValue : "" ,
2023-03-04 23:29:00 +01:00
} ,
username : {
type : "string" ,
2023-03-23 08:31:21 +01:00
defaultValue : "" ,
2023-03-04 23:29:00 +01:00
} ,
password : {
type : "string" ,
2023-03-23 08:31:21 +01:00
defaultValue : "" ,
2023-03-04 23:29:00 +01:00
obscured : true ,
} ,
} ,
2023-10-22 16:09:53 +02:00
oauth : {
"allowRegistration" : {
type : "boolean" ,
defaultValue : "true" ,
} ,
"ignoreTotp" : {
type : "boolean" ,
defaultValue : "true" ,
} ,
"github-enabled" : {
type : "boolean" ,
defaultValue : "false" ,
} ,
"github-clientId" : {
type : "string" ,
defaultValue : "" ,
} ,
"github-clientSecret" : {
type : "string" ,
defaultValue : "" ,
obscured : true ,
} ,
"google-enabled" : {
type : "boolean" ,
defaultValue : "false" ,
} ,
"google-clientId" : {
type : "string" ,
defaultValue : "" ,
} ,
"google-clientSecret" : {
type : "string" ,
defaultValue : "" ,
obscured : true ,
} ,
"microsoft-enabled" : {
type : "boolean" ,
defaultValue : "false" ,
} ,
"microsoft-tenant" : {
type : "string" ,
defaultValue : "common" ,
} ,
"microsoft-clientId" : {
type : "string" ,
defaultValue : "" ,
} ,
"microsoft-clientSecret" : {
type : "string" ,
defaultValue : "" ,
obscured : true ,
} ,
"discord-enabled" : {
type : "boolean" ,
defaultValue : "false" ,
} ,
2023-11-30 22:41:06 +01:00
"discord-limitedGuild" : {
type : "string" ,
defaultValue : "" ,
} ,
2023-10-22 16:09:53 +02:00
"discord-clientId" : {
type : "string" ,
defaultValue : "" ,
} ,
"discord-clientSecret" : {
type : "string" ,
defaultValue : "" ,
obscured : true ,
} ,
"oidc-enabled" : {
type : "boolean" ,
defaultValue : "false" ,
} ,
"oidc-discoveryUri" : {
type : "string" ,
defaultValue : "" ,
} ,
2023-12-20 12:32:42 +01:00
"oidc-usernameClaim" : {
type : "string" ,
defaultValue : "" ,
} ,
2023-10-22 16:09:53 +02:00
"oidc-clientId" : {
type : "string" ,
defaultValue : "" ,
} ,
"oidc-clientSecret" : {
type : "string" ,
defaultValue : "" ,
obscured : true ,
} ,
}
2023-03-04 23:29:00 +01:00
} ;
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"
> ;
} ;
} ;
2023-05-05 11:37:02 +02:00
const prisma = new PrismaClient ( {
datasources : {
db : {
url :
process . env . DATABASE_URL ||
"file:../data/pingvin-share.db?connection_limit=1" ,
} ,
} ,
} ) ;
2023-03-04 23:29:00 +01:00
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 ] ? . [
2023-11-30 22:41:06 +01:00
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 ) ;
} ) ;