Skip to content

Commit

Permalink
fix(mediafileservice): revert api; fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
seaerchin committed Feb 24, 2023
1 parent ec1d5a9 commit 6785e84
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
7 changes: 2 additions & 5 deletions src/services/fileServices/MdPageServices/MediaFileService.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import logger from "@logger/logger"
const logger = require("@logger/logger")

const { BadRequestError } = require("@errors/BadRequestError")
const { MediaTypeError } = require("@errors/MediaTypeError")
Expand Down Expand Up @@ -38,10 +38,7 @@ class MediaFileService {
}

// Sanitize and validate file
const sanitizedContent = await validateAndSanitizeFileUpload(
fileContent,
fileBuffer
)
const sanitizedContent = await validateAndSanitizeFileUpload(content)
if (!sanitizedContent) {
throw new MediaTypeError(`File extension is not within the approved list`)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe("Media File Service", () => {
const imageName = "test image.png"
const fileName = "test file.pdf"
const directoryName = "images/subfolder"
const mockContent = "test"
const mockContent = "schema, test"
const mockSanitizedContent = "sanitized-test"
const sha = "12345"
const mockGithubSessionData = "githubData"
Expand All @@ -32,6 +32,7 @@ describe("Media File Service", () => {
validateAndSanitizeFileUpload: jest
.fn()
.mockReturnValue(mockSanitizedContent),
scanFileForVirus: jest.fn().mockReturnValue({ CleanResult: true }),
}))
const {
MediaFileService,
Expand Down
6 changes: 4 additions & 2 deletions src/utils/file-upload-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const scanFileForVirus = (fileBuffer) =>
throw new BaseIsomerError(500, "Internal Server Error")
}

apiInstance.scanFile(fileBuffer, (error, data, response) => {
apiInstance.scanFile(fileBuffer, (error, data) => {
if (error) {
logger.error(
`Error when calling Cloudmersive Virus Scan API: ${error.message}`
Expand All @@ -46,7 +46,9 @@ const scanFileForVirus = (fileBuffer) =>
})
})

const validateAndSanitizeFileUpload = async (content, fileBuffer) => {
const validateAndSanitizeFileUpload = async (data) => {
const [, content] = data.split(",")
const fileBuffer = Buffer.from(content, "base64")
const detectedFileType = await FileType.fromBuffer(fileBuffer)

if (isSvg(fileBuffer)) {
Expand Down

0 comments on commit 6785e84

Please sign in to comment.