Skip to content
This repository was archived by the owner on Jun 29, 2025. It is now read-only.

Commit 98c0de7

Browse files
committed
feat: add env variables for port, database url and data dir
1 parent 5132d17 commit 98c0de7

File tree

11 files changed

+43
-26
lines changed

11 files changed

+43
-26
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ yarn-error.log*
2323

2424
# env file
2525
.env
26+
!/backend/prisma/.env
2627

2728
# vercel
2829
.vercel

backend/prisma/.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#This file is only used to set a default value for the database url
2+
DATABASE_URL="file:../data/pingvin-share.db"

backend/prisma/schema.prisma

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ generator client {
44

55
datasource db {
66
provider = "sqlite"
7-
url = "file:../data/pingvin-share.db"
7+
url = env("DATABASE_URL")
88
}
99

1010
model User {

backend/prisma/seed/config.seed.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Prisma, PrismaClient } from "@prisma/client";
22
import * as crypto from "crypto";
3-
43
const configVariables: ConfigVariables = {
54
internal: {
65
jwtSecret: {
@@ -162,7 +161,15 @@ type ConfigVariables = {
162161
};
163162
};
164163

165-
const prisma = new PrismaClient();
164+
const prisma = new PrismaClient({
165+
datasources: {
166+
db: {
167+
url:
168+
process.env.DATABASE_URL ||
169+
"file:../data/pingvin-share.db?connection_limit=1",
170+
},
171+
},
172+
});
166173

167174
async function seedConfigVariables() {
168175
for (const [category, configVariablesOfCategory] of Object.entries(

backend/src/clamscan/clamscan.service.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as NodeClam from "clamscan";
33
import * as fs from "fs";
44
import { FileService } from "src/file/file.service";
55
import { PrismaService } from "src/prisma/prisma.service";
6+
import { SHARE_DIRECTORY } from "../constants";
67

78
const clamscanConfig = {
89
clamdscan: {
@@ -39,12 +40,12 @@ export class ClamScanService {
3940
const infectedFiles = [];
4041

4142
const files = fs
42-
.readdirSync(`./data/uploads/shares/${shareId}`)
43+
.readdirSync(`${SHARE_DIRECTORY}/${shareId}`)
4344
.filter((file) => file != "archive.zip");
4445

4546
for (const fileId of files) {
4647
const { isInfected } = await clamScan
47-
.isInfected(`./data/uploads/shares/${shareId}/${fileId}`)
48+
.isInfected(`${SHARE_DIRECTORY}/${shareId}/${fileId}`)
4849
.catch(() => {
4950
console.log("ClamAV is not active");
5051
return { isInfected: false };

backend/src/constants.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const DATA_DIRECTORY = process.env.DATA_DIRECTORY || "./data";
2+
export const SHARE_DIRECTORY = `${DATA_DIRECTORY}/uploads/shares`
3+
export const DATABASE_URL = process.env.DATABASE_URL || "file:../data/pingvin-share.db?connection_limit=1";

backend/src/file/file.service.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import * as fs from "fs";
1111
import * as mime from "mime-types";
1212
import { ConfigService } from "src/config/config.service";
1313
import { PrismaService } from "src/prisma/prisma.service";
14+
import { SHARE_DIRECTORY } from "../constants";
1415

1516
@Injectable()
1617
export class FileService {
@@ -39,7 +40,7 @@ export class FileService {
3940
let diskFileSize: number;
4041
try {
4142
diskFileSize = fs.statSync(
42-
`./data/uploads/shares/${shareId}/${file.id}.tmp-chunk`
43+
`${SHARE_DIRECTORY}/${shareId}/${file.id}.tmp-chunk`
4344
).size;
4445
} catch {
4546
diskFileSize = 0;
@@ -78,18 +79,18 @@ export class FileService {
7879
}
7980

8081
fs.appendFileSync(
81-
`./data/uploads/shares/${shareId}/${file.id}.tmp-chunk`,
82+
`${SHARE_DIRECTORY}/${shareId}/${file.id}.tmp-chunk`,
8283
buffer
8384
);
8485

8586
const isLastChunk = chunk.index == chunk.total - 1;
8687
if (isLastChunk) {
8788
fs.renameSync(
88-
`./data/uploads/shares/${shareId}/${file.id}.tmp-chunk`,
89-
`./data/uploads/shares/${shareId}/${file.id}`
89+
`${SHARE_DIRECTORY}/${shareId}/${file.id}.tmp-chunk`,
90+
`${SHARE_DIRECTORY}/${shareId}/${file.id}`
9091
);
9192
const fileSize = fs.statSync(
92-
`./data/uploads/shares/${shareId}/${file.id}`
93+
`${SHARE_DIRECTORY}/${shareId}/${file.id}`
9394
).size;
9495
await this.prisma.file.create({
9596
data: {
@@ -111,9 +112,7 @@ export class FileService {
111112

112113
if (!fileMetaData) throw new NotFoundException("File not found");
113114

114-
const file = fs.createReadStream(
115-
`./data/uploads/shares/${shareId}/${fileId}`
116-
);
115+
const file = fs.createReadStream(`${SHARE_DIRECTORY}/${shareId}/${fileId}`);
117116

118117
return {
119118
metaData: {
@@ -126,13 +125,13 @@ export class FileService {
126125
}
127126

128127
async deleteAllFiles(shareId: string) {
129-
await fs.promises.rm(`./data/uploads/shares/${shareId}`, {
128+
await fs.promises.rm(`${SHARE_DIRECTORY}/${shareId}`, {
130129
recursive: true,
131130
force: true,
132131
});
133132
}
134133

135134
getZip(shareId: string) {
136-
return fs.createReadStream(`./data/uploads/shares/${shareId}/archive.zip`);
135+
return fs.createReadStream(`${SHARE_DIRECTORY}/${shareId}/archive.zip`);
137136
}
138137
}

backend/src/jobs/jobs.service.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as moment from "moment";
55
import { FileService } from "src/file/file.service";
66
import { PrismaService } from "src/prisma/prisma.service";
77
import { ReverseShareService } from "src/reverseShare/reverseShare.service";
8+
import { SHARE_DIRECTORY } from "../constants";
89

910
@Injectable()
1011
export class JobsService {
@@ -61,25 +62,25 @@ export class JobsService {
6162
let filesDeleted = 0;
6263

6364
const shareDirectories = fs
64-
.readdirSync("./data/uploads/shares", { withFileTypes: true })
65+
.readdirSync(SHARE_DIRECTORY, { withFileTypes: true })
6566
.filter((dirent) => dirent.isDirectory())
6667
.map((dirent) => dirent.name);
6768

6869
for (const shareDirectory of shareDirectories) {
6970
const temporaryFiles = fs
70-
.readdirSync(`./data/uploads/shares/${shareDirectory}`)
71+
.readdirSync(`${SHARE_DIRECTORY}/${shareDirectory}`)
7172
.filter((file) => file.endsWith(".tmp-chunk"));
7273

7374
for (const file of temporaryFiles) {
7475
const stats = fs.statSync(
75-
`./data/uploads/shares/${shareDirectory}/${file}`
76+
`${SHARE_DIRECTORY}/${shareDirectory}/${file}`
7677
);
7778
const isOlderThanOneDay = moment(stats.mtime)
7879
.add(1, "day")
7980
.isBefore(moment());
8081

8182
if (isOlderThanOneDay) {
82-
fs.rmSync(`./data/uploads/shares/${shareDirectory}/${file}`);
83+
fs.rmSync(`${SHARE_DIRECTORY}/${shareDirectory}/${file}`);
8384
filesDeleted++;
8485
}
8586
}

backend/src/main.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as bodyParser from "body-parser";
66
import * as cookieParser from "cookie-parser";
77
import * as fs from "fs";
88
import { AppModule } from "./app.module";
9+
import { DATA_DIRECTORY } from "./constants";
910

1011
async function bootstrap() {
1112
const app = await NestFactory.create<NestExpressApplication>(AppModule);
@@ -16,7 +17,9 @@ async function bootstrap() {
1617
app.use(cookieParser());
1718
app.set("trust proxy", true);
1819

19-
await fs.promises.mkdir("./data/uploads/_temp", { recursive: true });
20+
await fs.promises.mkdir(`${DATA_DIRECTORY}/uploads/_temp`, {
21+
recursive: true,
22+
});
2023

2124
app.setGlobalPrefix("api");
2225

@@ -30,6 +33,6 @@ async function bootstrap() {
3033
SwaggerModule.setup("api/swagger", app, document);
3134
}
3235

33-
await app.listen(process.env.PORT || 8080);
36+
await app.listen(parseInt(process.env.PORT) || 8080);
3437
}
3538
bootstrap();

backend/src/prisma/prisma.service.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import { Injectable } from "@nestjs/common";
22
import { PrismaClient } from "@prisma/client";
3+
import { DATABASE_URL } from "../constants";
34

45
@Injectable()
56
export class PrismaService extends PrismaClient {
67
constructor() {
78
super({
89
datasources: {
910
db: {
10-
url:
11-
process.env.DATABASE_URL ||
12-
"file:../data/pingvin-share.db?connection_limit=1",
11+
url: DATABASE_URL,
1312
},
1413
},
1514
});

0 commit comments

Comments
 (0)