Skip to content

Commit

Permalink
feat: add env variables for port, database url and data dir
Browse files Browse the repository at this point in the history
  • Loading branch information
stonith404 committed May 5, 2023
1 parent 5132d17 commit 98c0de7
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ yarn-error.log*

# env file
.env
!/backend/prisma/.env

# vercel
.vercel
Expand Down
2 changes: 2 additions & 0 deletions backend/prisma/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#This file is only used to set a default value for the database url
DATABASE_URL="file:../data/pingvin-share.db"
2 changes: 1 addition & 1 deletion backend/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ generator client {

datasource db {
provider = "sqlite"
url = "file:../data/pingvin-share.db"
url = env("DATABASE_URL")
}

model User {
Expand Down
11 changes: 9 additions & 2 deletions backend/prisma/seed/config.seed.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Prisma, PrismaClient } from "@prisma/client";
import * as crypto from "crypto";

const configVariables: ConfigVariables = {
internal: {
jwtSecret: {
Expand Down Expand Up @@ -162,7 +161,15 @@ type ConfigVariables = {
};
};

const prisma = new PrismaClient();
const prisma = new PrismaClient({
datasources: {
db: {
url:
process.env.DATABASE_URL ||
"file:../data/pingvin-share.db?connection_limit=1",
},
},
});

async function seedConfigVariables() {
for (const [category, configVariablesOfCategory] of Object.entries(
Expand Down
5 changes: 3 additions & 2 deletions backend/src/clamscan/clamscan.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as NodeClam from "clamscan";
import * as fs from "fs";
import { FileService } from "src/file/file.service";
import { PrismaService } from "src/prisma/prisma.service";
import { SHARE_DIRECTORY } from "../constants";

const clamscanConfig = {
clamdscan: {
Expand Down Expand Up @@ -39,12 +40,12 @@ export class ClamScanService {
const infectedFiles = [];

const files = fs
.readdirSync(`./data/uploads/shares/${shareId}`)
.readdirSync(`${SHARE_DIRECTORY}/${shareId}`)
.filter((file) => file != "archive.zip");

for (const fileId of files) {
const { isInfected } = await clamScan
.isInfected(`./data/uploads/shares/${shareId}/${fileId}`)
.isInfected(`${SHARE_DIRECTORY}/${shareId}/${fileId}`)
.catch(() => {
console.log("ClamAV is not active");
return { isInfected: false };
Expand Down
3 changes: 3 additions & 0 deletions backend/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const DATA_DIRECTORY = process.env.DATA_DIRECTORY || "./data";
export const SHARE_DIRECTORY = `${DATA_DIRECTORY}/uploads/shares`
export const DATABASE_URL = process.env.DATABASE_URL || "file:../data/pingvin-share.db?connection_limit=1";
19 changes: 9 additions & 10 deletions backend/src/file/file.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as fs from "fs";
import * as mime from "mime-types";
import { ConfigService } from "src/config/config.service";
import { PrismaService } from "src/prisma/prisma.service";
import { SHARE_DIRECTORY } from "../constants";

@Injectable()
export class FileService {
Expand Down Expand Up @@ -39,7 +40,7 @@ export class FileService {
let diskFileSize: number;
try {
diskFileSize = fs.statSync(
`./data/uploads/shares/${shareId}/${file.id}.tmp-chunk`
`${SHARE_DIRECTORY}/${shareId}/${file.id}.tmp-chunk`
).size;
} catch {
diskFileSize = 0;
Expand Down Expand Up @@ -78,18 +79,18 @@ export class FileService {
}

fs.appendFileSync(
`./data/uploads/shares/${shareId}/${file.id}.tmp-chunk`,
`${SHARE_DIRECTORY}/${shareId}/${file.id}.tmp-chunk`,
buffer
);

const isLastChunk = chunk.index == chunk.total - 1;
if (isLastChunk) {
fs.renameSync(
`./data/uploads/shares/${shareId}/${file.id}.tmp-chunk`,
`./data/uploads/shares/${shareId}/${file.id}`
`${SHARE_DIRECTORY}/${shareId}/${file.id}.tmp-chunk`,
`${SHARE_DIRECTORY}/${shareId}/${file.id}`
);
const fileSize = fs.statSync(
`./data/uploads/shares/${shareId}/${file.id}`
`${SHARE_DIRECTORY}/${shareId}/${file.id}`
).size;
await this.prisma.file.create({
data: {
Expand All @@ -111,9 +112,7 @@ export class FileService {

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

const file = fs.createReadStream(
`./data/uploads/shares/${shareId}/${fileId}`
);
const file = fs.createReadStream(`${SHARE_DIRECTORY}/${shareId}/${fileId}`);

return {
metaData: {
Expand All @@ -126,13 +125,13 @@ export class FileService {
}

async deleteAllFiles(shareId: string) {
await fs.promises.rm(`./data/uploads/shares/${shareId}`, {
await fs.promises.rm(`${SHARE_DIRECTORY}/${shareId}`, {
recursive: true,
force: true,
});
}

getZip(shareId: string) {
return fs.createReadStream(`./data/uploads/shares/${shareId}/archive.zip`);
return fs.createReadStream(`${SHARE_DIRECTORY}/${shareId}/archive.zip`);
}
}
9 changes: 5 additions & 4 deletions backend/src/jobs/jobs.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as moment from "moment";
import { FileService } from "src/file/file.service";
import { PrismaService } from "src/prisma/prisma.service";
import { ReverseShareService } from "src/reverseShare/reverseShare.service";
import { SHARE_DIRECTORY } from "../constants";

@Injectable()
export class JobsService {
Expand Down Expand Up @@ -61,25 +62,25 @@ export class JobsService {
let filesDeleted = 0;

const shareDirectories = fs
.readdirSync("./data/uploads/shares", { withFileTypes: true })
.readdirSync(SHARE_DIRECTORY, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name);

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

for (const file of temporaryFiles) {
const stats = fs.statSync(
`./data/uploads/shares/${shareDirectory}/${file}`
`${SHARE_DIRECTORY}/${shareDirectory}/${file}`
);
const isOlderThanOneDay = moment(stats.mtime)
.add(1, "day")
.isBefore(moment());

if (isOlderThanOneDay) {
fs.rmSync(`./data/uploads/shares/${shareDirectory}/${file}`);
fs.rmSync(`${SHARE_DIRECTORY}/${shareDirectory}/${file}`);
filesDeleted++;
}
}
Expand Down
7 changes: 5 additions & 2 deletions backend/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as bodyParser from "body-parser";
import * as cookieParser from "cookie-parser";
import * as fs from "fs";
import { AppModule } from "./app.module";
import { DATA_DIRECTORY } from "./constants";

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

await fs.promises.mkdir("./data/uploads/_temp", { recursive: true });
await fs.promises.mkdir(`${DATA_DIRECTORY}/uploads/_temp`, {
recursive: true,
});

app.setGlobalPrefix("api");

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

await app.listen(process.env.PORT || 8080);
await app.listen(parseInt(process.env.PORT) || 8080);
}
bootstrap();
5 changes: 2 additions & 3 deletions backend/src/prisma/prisma.service.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { Injectable } from "@nestjs/common";
import { PrismaClient } from "@prisma/client";
import { DATABASE_URL } from "../constants";

@Injectable()
export class PrismaService extends PrismaClient {
constructor() {
super({
datasources: {
db: {
url:
process.env.DATABASE_URL ||
"file:../data/pingvin-share.db?connection_limit=1",
url: DATABASE_URL,
},
},
});
Expand Down
5 changes: 3 additions & 2 deletions backend/src/share/share.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { EmailService } from "src/email/email.service";
import { FileService } from "src/file/file.service";
import { PrismaService } from "src/prisma/prisma.service";
import { ReverseShareService } from "src/reverseShare/reverseShare.service";
import { SHARE_DIRECTORY } from "../constants";
import { CreateShareDTO } from "./dto/createShare.dto";

@Injectable()
Expand Down Expand Up @@ -65,7 +66,7 @@ export class ShareService {
}
}

fs.mkdirSync(`./data/uploads/shares/${share.id}`, {
fs.mkdirSync(`${SHARE_DIRECTORY}/${share.id}`, {
recursive: true,
});

Expand Down Expand Up @@ -99,7 +100,7 @@ export class ShareService {
}

async createZip(shareId: string) {
const path = `./data/uploads/shares/${shareId}`;
const path = `${SHARE_DIRECTORY}/${shareId}`;

const files = await this.prisma.file.findMany({ where: { shareId } });
const archive = archiver("zip", {
Expand Down

0 comments on commit 98c0de7

Please sign in to comment.