mirror of
https://github.com/stonith404/pingvin-share.git
synced 2025-02-19 01:55:48 -05:00
feat: add env variables for port, database url and data dir
This commit is contained in:
parent
5132d177b8
commit
98c0de78e8
11 changed files with 43 additions and 26 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -23,6 +23,7 @@ yarn-error.log*
|
||||||
|
|
||||||
# env file
|
# env file
|
||||||
.env
|
.env
|
||||||
|
!/backend/prisma/.env
|
||||||
|
|
||||||
# vercel
|
# vercel
|
||||||
.vercel
|
.vercel
|
||||||
|
|
2
backend/prisma/.env
Normal file
2
backend/prisma/.env
Normal file
|
@ -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"
|
|
@ -4,7 +4,7 @@ generator client {
|
||||||
|
|
||||||
datasource db {
|
datasource db {
|
||||||
provider = "sqlite"
|
provider = "sqlite"
|
||||||
url = "file:../data/pingvin-share.db"
|
url = env("DATABASE_URL")
|
||||||
}
|
}
|
||||||
|
|
||||||
model User {
|
model User {
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
import { Prisma, PrismaClient } from "@prisma/client";
|
import { Prisma, PrismaClient } from "@prisma/client";
|
||||||
import * as crypto from "crypto";
|
import * as crypto from "crypto";
|
||||||
|
|
||||||
const configVariables: ConfigVariables = {
|
const configVariables: ConfigVariables = {
|
||||||
internal: {
|
internal: {
|
||||||
jwtSecret: {
|
jwtSecret: {
|
||||||
|
@ -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() {
|
async function seedConfigVariables() {
|
||||||
for (const [category, configVariablesOfCategory] of Object.entries(
|
for (const [category, configVariablesOfCategory] of Object.entries(
|
||||||
|
|
|
@ -3,6 +3,7 @@ import * as NodeClam from "clamscan";
|
||||||
import * as fs from "fs";
|
import * as fs from "fs";
|
||||||
import { FileService } from "src/file/file.service";
|
import { FileService } from "src/file/file.service";
|
||||||
import { PrismaService } from "src/prisma/prisma.service";
|
import { PrismaService } from "src/prisma/prisma.service";
|
||||||
|
import { SHARE_DIRECTORY } from "../constants";
|
||||||
|
|
||||||
const clamscanConfig = {
|
const clamscanConfig = {
|
||||||
clamdscan: {
|
clamdscan: {
|
||||||
|
@ -39,12 +40,12 @@ export class ClamScanService {
|
||||||
const infectedFiles = [];
|
const infectedFiles = [];
|
||||||
|
|
||||||
const files = fs
|
const files = fs
|
||||||
.readdirSync(`./data/uploads/shares/${shareId}`)
|
.readdirSync(`${SHARE_DIRECTORY}/${shareId}`)
|
||||||
.filter((file) => file != "archive.zip");
|
.filter((file) => file != "archive.zip");
|
||||||
|
|
||||||
for (const fileId of files) {
|
for (const fileId of files) {
|
||||||
const { isInfected } = await clamScan
|
const { isInfected } = await clamScan
|
||||||
.isInfected(`./data/uploads/shares/${shareId}/${fileId}`)
|
.isInfected(`${SHARE_DIRECTORY}/${shareId}/${fileId}`)
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
console.log("ClamAV is not active");
|
console.log("ClamAV is not active");
|
||||||
return { isInfected: false };
|
return { isInfected: false };
|
||||||
|
|
3
backend/src/constants.ts
Normal file
3
backend/src/constants.ts
Normal file
|
@ -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";
|
|
@ -11,6 +11,7 @@ import * as fs from "fs";
|
||||||
import * as mime from "mime-types";
|
import * as mime from "mime-types";
|
||||||
import { ConfigService } from "src/config/config.service";
|
import { ConfigService } from "src/config/config.service";
|
||||||
import { PrismaService } from "src/prisma/prisma.service";
|
import { PrismaService } from "src/prisma/prisma.service";
|
||||||
|
import { SHARE_DIRECTORY } from "../constants";
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class FileService {
|
export class FileService {
|
||||||
|
@ -39,7 +40,7 @@ export class FileService {
|
||||||
let diskFileSize: number;
|
let diskFileSize: number;
|
||||||
try {
|
try {
|
||||||
diskFileSize = fs.statSync(
|
diskFileSize = fs.statSync(
|
||||||
`./data/uploads/shares/${shareId}/${file.id}.tmp-chunk`
|
`${SHARE_DIRECTORY}/${shareId}/${file.id}.tmp-chunk`
|
||||||
).size;
|
).size;
|
||||||
} catch {
|
} catch {
|
||||||
diskFileSize = 0;
|
diskFileSize = 0;
|
||||||
|
@ -78,18 +79,18 @@ export class FileService {
|
||||||
}
|
}
|
||||||
|
|
||||||
fs.appendFileSync(
|
fs.appendFileSync(
|
||||||
`./data/uploads/shares/${shareId}/${file.id}.tmp-chunk`,
|
`${SHARE_DIRECTORY}/${shareId}/${file.id}.tmp-chunk`,
|
||||||
buffer
|
buffer
|
||||||
);
|
);
|
||||||
|
|
||||||
const isLastChunk = chunk.index == chunk.total - 1;
|
const isLastChunk = chunk.index == chunk.total - 1;
|
||||||
if (isLastChunk) {
|
if (isLastChunk) {
|
||||||
fs.renameSync(
|
fs.renameSync(
|
||||||
`./data/uploads/shares/${shareId}/${file.id}.tmp-chunk`,
|
`${SHARE_DIRECTORY}/${shareId}/${file.id}.tmp-chunk`,
|
||||||
`./data/uploads/shares/${shareId}/${file.id}`
|
`${SHARE_DIRECTORY}/${shareId}/${file.id}`
|
||||||
);
|
);
|
||||||
const fileSize = fs.statSync(
|
const fileSize = fs.statSync(
|
||||||
`./data/uploads/shares/${shareId}/${file.id}`
|
`${SHARE_DIRECTORY}/${shareId}/${file.id}`
|
||||||
).size;
|
).size;
|
||||||
await this.prisma.file.create({
|
await this.prisma.file.create({
|
||||||
data: {
|
data: {
|
||||||
|
@ -111,9 +112,7 @@ export class FileService {
|
||||||
|
|
||||||
if (!fileMetaData) throw new NotFoundException("File not found");
|
if (!fileMetaData) throw new NotFoundException("File not found");
|
||||||
|
|
||||||
const file = fs.createReadStream(
|
const file = fs.createReadStream(`${SHARE_DIRECTORY}/${shareId}/${fileId}`);
|
||||||
`./data/uploads/shares/${shareId}/${fileId}`
|
|
||||||
);
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
metaData: {
|
metaData: {
|
||||||
|
@ -126,13 +125,13 @@ export class FileService {
|
||||||
}
|
}
|
||||||
|
|
||||||
async deleteAllFiles(shareId: string) {
|
async deleteAllFiles(shareId: string) {
|
||||||
await fs.promises.rm(`./data/uploads/shares/${shareId}`, {
|
await fs.promises.rm(`${SHARE_DIRECTORY}/${shareId}`, {
|
||||||
recursive: true,
|
recursive: true,
|
||||||
force: true,
|
force: true,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
getZip(shareId: string) {
|
getZip(shareId: string) {
|
||||||
return fs.createReadStream(`./data/uploads/shares/${shareId}/archive.zip`);
|
return fs.createReadStream(`${SHARE_DIRECTORY}/${shareId}/archive.zip`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import * as moment from "moment";
|
||||||
import { FileService } from "src/file/file.service";
|
import { FileService } from "src/file/file.service";
|
||||||
import { PrismaService } from "src/prisma/prisma.service";
|
import { PrismaService } from "src/prisma/prisma.service";
|
||||||
import { ReverseShareService } from "src/reverseShare/reverseShare.service";
|
import { ReverseShareService } from "src/reverseShare/reverseShare.service";
|
||||||
|
import { SHARE_DIRECTORY } from "../constants";
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class JobsService {
|
export class JobsService {
|
||||||
|
@ -61,25 +62,25 @@ export class JobsService {
|
||||||
let filesDeleted = 0;
|
let filesDeleted = 0;
|
||||||
|
|
||||||
const shareDirectories = fs
|
const shareDirectories = fs
|
||||||
.readdirSync("./data/uploads/shares", { withFileTypes: true })
|
.readdirSync(SHARE_DIRECTORY, { withFileTypes: true })
|
||||||
.filter((dirent) => dirent.isDirectory())
|
.filter((dirent) => dirent.isDirectory())
|
||||||
.map((dirent) => dirent.name);
|
.map((dirent) => dirent.name);
|
||||||
|
|
||||||
for (const shareDirectory of shareDirectories) {
|
for (const shareDirectory of shareDirectories) {
|
||||||
const temporaryFiles = fs
|
const temporaryFiles = fs
|
||||||
.readdirSync(`./data/uploads/shares/${shareDirectory}`)
|
.readdirSync(`${SHARE_DIRECTORY}/${shareDirectory}`)
|
||||||
.filter((file) => file.endsWith(".tmp-chunk"));
|
.filter((file) => file.endsWith(".tmp-chunk"));
|
||||||
|
|
||||||
for (const file of temporaryFiles) {
|
for (const file of temporaryFiles) {
|
||||||
const stats = fs.statSync(
|
const stats = fs.statSync(
|
||||||
`./data/uploads/shares/${shareDirectory}/${file}`
|
`${SHARE_DIRECTORY}/${shareDirectory}/${file}`
|
||||||
);
|
);
|
||||||
const isOlderThanOneDay = moment(stats.mtime)
|
const isOlderThanOneDay = moment(stats.mtime)
|
||||||
.add(1, "day")
|
.add(1, "day")
|
||||||
.isBefore(moment());
|
.isBefore(moment());
|
||||||
|
|
||||||
if (isOlderThanOneDay) {
|
if (isOlderThanOneDay) {
|
||||||
fs.rmSync(`./data/uploads/shares/${shareDirectory}/${file}`);
|
fs.rmSync(`${SHARE_DIRECTORY}/${shareDirectory}/${file}`);
|
||||||
filesDeleted++;
|
filesDeleted++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@ import * as bodyParser from "body-parser";
|
||||||
import * as cookieParser from "cookie-parser";
|
import * as cookieParser from "cookie-parser";
|
||||||
import * as fs from "fs";
|
import * as fs from "fs";
|
||||||
import { AppModule } from "./app.module";
|
import { AppModule } from "./app.module";
|
||||||
|
import { DATA_DIRECTORY } from "./constants";
|
||||||
|
|
||||||
async function bootstrap() {
|
async function bootstrap() {
|
||||||
const app = await NestFactory.create<NestExpressApplication>(AppModule);
|
const app = await NestFactory.create<NestExpressApplication>(AppModule);
|
||||||
|
@ -16,7 +17,9 @@ async function bootstrap() {
|
||||||
app.use(cookieParser());
|
app.use(cookieParser());
|
||||||
app.set("trust proxy", true);
|
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");
|
app.setGlobalPrefix("api");
|
||||||
|
|
||||||
|
@ -30,6 +33,6 @@ async function bootstrap() {
|
||||||
SwaggerModule.setup("api/swagger", app, document);
|
SwaggerModule.setup("api/swagger", app, document);
|
||||||
}
|
}
|
||||||
|
|
||||||
await app.listen(process.env.PORT || 8080);
|
await app.listen(parseInt(process.env.PORT) || 8080);
|
||||||
}
|
}
|
||||||
bootstrap();
|
bootstrap();
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import { Injectable } from "@nestjs/common";
|
import { Injectable } from "@nestjs/common";
|
||||||
import { PrismaClient } from "@prisma/client";
|
import { PrismaClient } from "@prisma/client";
|
||||||
|
import { DATABASE_URL } from "../constants";
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class PrismaService extends PrismaClient {
|
export class PrismaService extends PrismaClient {
|
||||||
|
@ -7,9 +8,7 @@ export class PrismaService extends PrismaClient {
|
||||||
super({
|
super({
|
||||||
datasources: {
|
datasources: {
|
||||||
db: {
|
db: {
|
||||||
url:
|
url: DATABASE_URL,
|
||||||
process.env.DATABASE_URL ||
|
|
||||||
"file:../data/pingvin-share.db?connection_limit=1",
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
|
@ -16,6 +16,7 @@ import { EmailService } from "src/email/email.service";
|
||||||
import { FileService } from "src/file/file.service";
|
import { FileService } from "src/file/file.service";
|
||||||
import { PrismaService } from "src/prisma/prisma.service";
|
import { PrismaService } from "src/prisma/prisma.service";
|
||||||
import { ReverseShareService } from "src/reverseShare/reverseShare.service";
|
import { ReverseShareService } from "src/reverseShare/reverseShare.service";
|
||||||
|
import { SHARE_DIRECTORY } from "../constants";
|
||||||
import { CreateShareDTO } from "./dto/createShare.dto";
|
import { CreateShareDTO } from "./dto/createShare.dto";
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
|
@ -65,7 +66,7 @@ export class ShareService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fs.mkdirSync(`./data/uploads/shares/${share.id}`, {
|
fs.mkdirSync(`${SHARE_DIRECTORY}/${share.id}`, {
|
||||||
recursive: true,
|
recursive: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -99,7 +100,7 @@ export class ShareService {
|
||||||
}
|
}
|
||||||
|
|
||||||
async createZip(shareId: string) {
|
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 files = await this.prisma.file.findMany({ where: { shareId } });
|
||||||
const archive = archiver("zip", {
|
const archive = archiver("zip", {
|
||||||
|
|
Loading…
Add table
Reference in a new issue