Skip to content

Commit

Permalink
Merge pull request #236 from aXenDeveloper/fix/upload_files
Browse files Browse the repository at this point in the history
fix(backend): Upload to public folder
  • Loading branch information
aXenDeveloper authored Feb 29, 2024
2 parents f09077d + 403d58c commit 22b36d1
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 11 deletions.
2 changes: 1 addition & 1 deletion backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { DatabaseModule } from "@/database/database.module";
JwtModule.register({ global: true }),
ModulesModule,
ServeStaticModule.forRoot({
rootPath: join(__dirname, "..", "../public"),
rootPath: join(process.cwd(), "public"),
serveRoot: "/public"
}),
ScheduleModule.forRoot(),
Expand Down
18 changes: 13 additions & 5 deletions backend/src/apps/core/files/upload/upload.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createWriteStream, existsSync, mkdirSync, statSync } from "fs";
import { join } from "path";

import { Injectable } from "@nestjs/common";

Expand Down Expand Up @@ -79,22 +80,29 @@ export class UploadCoreFilesService {

// Create folders
const date = new Date();
const dirFolder = `public/monthly_${date.getMonth() + 1}_${date.getFullYear()}/${module_name}`;
const dir = join(
"public",
`monthly_${date.getMonth() + 1}_${date.getFullYear()}`,
module_name
);
const dirFolder = join(process.cwd(), dir);
if (!existsSync(dirFolder)) {
mkdirSync(dirFolder, { recursive: true });
}

const saveFiles = await Promise.all(
files.map(async file => {
const { createReadStream, filename, mimetype } = await file;
const extension = filename.split(".").pop();
const name = filename.split(".").shift();

const stream = createReadStream();

// Generate file name
const currentFileName = `${date.getTime()}_${generateRandomString(
10
)}_${removeSpecialCharacters(filename)}`;
const url = `${dirFolder}/${currentFileName}`;
)}_${removeSpecialCharacters(name)}.${extension}`;
const url = join(dirFolder, currentFileName);

// Save file to file system
await new Promise((resolve, reject) =>
Expand All @@ -111,8 +119,8 @@ export class UploadCoreFilesService {
module_name,
mimetype,
name: currentFileName,
dir_folder: dirFolder,
extension: filename.split(".").pop(),
dir_folder: dir,
extension,
size: stat.size
};
})
Expand Down
4 changes: 2 additions & 2 deletions backend/src/utils/graphql-upload/processRequest.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { IncomingMessage, ServerResponse } from "http";

import * as busboy from "busboy";
import busboy from "busboy";
import * as createError from "http-errors";
import * as objectPath from "object-path";
import objectPath from "object-path";

import Upload, { FileUpload } from "./Upload";
import ignoreStream from "./ignoreStream";
Expand Down
4 changes: 2 additions & 2 deletions frontend/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ const config = () => {
{
hostname: "localhost",
port: "8080",
protocol: "http",
pathname: "/public/**"
protocol: "http"
// pathname: "/public/**"
},
{
hostname: backend.hostname,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useTranslations } from "next-intl";

import { Card, CardContent, CardHeader } from "@/components/ui/card";
import { useTextLang } from "@/hooks/core/use-text-lang";
import { ActionsForumsForum } from "./actions/action";
import { ActionsForumsForum } from "./actions/actions";
import type { Forum_Forums__Show_ItemQuery } from "@/graphql/hooks";
import { TopicsListForum } from "./topics-list/topics-list";
import { ReadOnlyEditor } from "@/components/editor/read-only/read-only-editor";
Expand Down

0 comments on commit 22b36d1

Please sign in to comment.