Skip to content

Commit

Permalink
feat: put db and uploads in same folder
Browse files Browse the repository at this point in the history
  • Loading branch information
stonith404 committed Oct 11, 2022
1 parent 759db40 commit 80cdcda
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ yarn-error.log*
/frontend/public/sw.*

# project specific
/backend/uploads/
/backend/data/
/data/
/backend/prisma/pingvin-share.db*
4 changes: 0 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ COPY ./backend .
RUN npx prisma generate
RUN npm run build





FROM node:18 AS runner
WORKDIR /opt/app/frontend
ENV NODE_ENV=production
Expand Down
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:./pingvin-share.db"
url = "file:../data/pingvin-share.db"
}

model User {
Expand Down
2 changes: 1 addition & 1 deletion backend/src/file/file.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class FileController {
@UseGuards(JwtGuard, ShareOwnerGuard)
@UseInterceptors(
FileInterceptor("file", {
dest: "./uploads/_temp/",
dest: "./data/uploads/_temp/",
})
)
async create(
Expand Down
15 changes: 8 additions & 7 deletions backend/src/file/file.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { JwtService } from "@nestjs/jwt";
import { randomUUID } from "crypto";
import * as fs from "fs";
import * as mime from "mime-types";
import { join } from "path";
import { PrismaService } from "src/prisma/prisma.service";

@Injectable()
Expand All @@ -29,10 +28,12 @@ export class FileService {

const fileId = randomUUID();

await fs.promises.mkdir(`./uploads/shares/${shareId}`, { recursive: true });
await fs.promises.mkdir(`./data/uploads/shares/${shareId}`, {
recursive: true,
});
fs.promises.rename(
`./uploads/_temp/${file.filename}`,
`./uploads/shares/${shareId}/${fileId}`
`./data/uploads/_temp/${file.filename}`,
`./data/uploads/shares/${shareId}/${fileId}`
);

return await this.prisma.file.create({
Expand All @@ -53,7 +54,7 @@ export class FileService {
if (!fileMetaData) throw new NotFoundException("File not found");

const file = fs.createReadStream(
join(process.cwd(), `uploads/shares/${shareId}/${fileId}`)
`./data/uploads/shares/${shareId}/${fileId}`
);

return {
Expand All @@ -67,14 +68,14 @@ export class FileService {
}

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

getZip(shareId: string) {
return fs.createReadStream(`./uploads/shares/${shareId}/archive.zip`);
return fs.createReadStream(`./data/uploads/shares/${shareId}/archive.zip`);
}

getFileDownloadUrl(shareId: string, fileId: string) {
Expand Down
2 changes: 1 addition & 1 deletion backend/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ async function bootstrap() {
app.useGlobalPipes(new ValidationPipe());
app.useGlobalInterceptors(new ClassSerializerInterceptor(app.get(Reflector)));

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

app.setGlobalPrefix("api");
await app.listen(8080);
Expand Down
2 changes: 1 addition & 1 deletion backend/src/prisma/prisma.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class PrismaService extends PrismaClient {
super({
datasources: {
db: {
url: "file:./pingvin-share.db",
url: "file:../data/pingvin-share.db",
},
},
});
Expand Down
2 changes: 1 addition & 1 deletion backend/src/share/share.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class ShareService {
}

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

const files = await this.prisma.file.findMany({ where: { shareId } });
const archive = archiver("zip", {
Expand Down
3 changes: 1 addition & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ services:
- MAX_FILE_SIZE=${MAX_FILE_SIZE}
- JWT_SECRET=${JWT_SECRET}
volumes:
- "${PWD}/data/uploads:/opt/app/backend/uploads"
- "${PWD}/data/pingvin-share.db:/opt/app/backend/prisma/pingvin-share.db"
- "${PWD}/data:/opt/app/backend/data"

0 comments on commit 80cdcda

Please sign in to comment.