Skip to content

Commit

Permalink
Prevent renaming of media files.
Browse files Browse the repository at this point in the history
  • Loading branch information
chinmoy12c committed Oct 7, 2023
1 parent a6ccd0f commit 1cb0f0e
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/modules/form/form.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
PROGRAM as ProgramMessages,
} from '../../common/messages';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const fs = require('fs');
import fs from 'fs';
import parser from 'xml2json';
import { FormMediaUploadStatus, FormUploadStatus } from './form.types';

Expand Down Expand Up @@ -79,6 +79,7 @@ export class FormService {
await this.login();
if (mediaFiles && mediaFiles.length > 0) {
const mediaUploadResult = await this.uploadFormMediaFiles(mediaFiles);
this.logger.log(`Uploaded media files data: ${JSON.stringify(mediaUploadResult.data)}`);
if (mediaUploadResult.error || !mediaUploadResult.data) {
this.logger.error(`FormService::uploadForm: Media Files upload failed!`);
throw new ServiceUnavailableException(`Media upload failed! Reason: ${mediaUploadResult.error}`);
Expand Down Expand Up @@ -164,8 +165,9 @@ export class FormService {
const promises: any = [];

mediaFiles.forEach((mediaFile: Express.Multer.File) => {
fs.copyFileSync(mediaFile.path, `${mediaFile.destination}/${mediaFile.originalname}`);
const formData = new FormData();
const fileToUpload = fs.createReadStream(mediaFile.path);
const fileToUpload = fs.createReadStream(`${mediaFile.destination}/${mediaFile.originalname}`);
formData.append('file', fileToUpload, mediaFile.originalname);

const requestOptions = {
Expand Down Expand Up @@ -222,7 +224,7 @@ export class FormService {
try {
let data = fs.readFileSync(formFile.path, 'utf-8');
uploadedMediaNames.forEach((value: string, key: string) => {
data = data.replaceAll(`{${key}}`, value);
data = data.replace(`{${key}}`, value);
});
fs.writeFileSync(formFile.path, data);
return '';
Expand Down

0 comments on commit 1cb0f0e

Please sign in to comment.