Skip to content

Commit

Permalink
fix: base64 img regex (#1018)
Browse files Browse the repository at this point in the history
* fix: base64 img regex

* refactor: minor refactor for file type validation
  • Loading branch information
rahultrivedi1800 authored Jul 1, 2022
1 parent 81590c8 commit d06e87f
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 15 deletions.
7 changes: 2 additions & 5 deletions src/backend/photos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ import axios from 'axios'
import ipfs from './utilities/ipfs'
import { nodeUrl, sigValidity } from './utilities/config'
import libsodium from './utilities/keys'
import { uint8ArrayToHexString } from './utilities/helpers'
import { uint8ArrayToHexString, validFileTypes } from './utilities/helpers'
import { getCompressedImage } from './utilities/imageCompression'
import { encryptData } from './crypto'

const validFileTypes = [`image/jpeg`, `image/jpg`, `image/png`, `image/avif`, `image/webp`]
const regExpString = `^data:${validFileTypes.join(`|`)}:base64,([a-fA-F0-9]+)$`

interface IUploadPhotoResult {
cid: string
url: string | ArrayBuffer
Expand Down Expand Up @@ -45,7 +42,7 @@ export function addPhotoToIPFS(content: string | ArrayBuffer) {
}

export function isValidPhoto(content: string) {
const regExp = new RegExp(regExpString)
const regExp = new RegExp(`^data:image/(?:${validFileTypes.join(`|`)});base64,([a-zA-Z0-9+/]+=*)$`)
if (!regExp.test(content)) {
return false
}
Expand Down
11 changes: 3 additions & 8 deletions src/backend/utilities/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ export interface ISignedIPFSObject<T> {
public_key: string
}

export const validFileTypes = [`png`, `jpeg`, `jpg`, `avif`, `webp`]
export const validMimeTypes = validFileTypes.map((t) => `image/${t}`)

export function uint8ArrayToHexString(uint8Array: Uint8Array): string {
return Buffer.from(uint8Array).toString(`hex`)
}
Expand Down Expand Up @@ -41,14 +44,6 @@ export function getBlobExtension(blob: Blob): string | null {
}
}

export function isValidFileType(fileType: string) {
const validFileTypes = [`image/png`, `image/jpeg`, `image/jpg`, `image/avif`, `image/webp`]
if (!validFileTypes.includes(fileType)) {
return false
}
return true
}

export function calculateReadingTime(wordCount?: number, postImagesLength: number = 0) {
if (!wordCount) {
return null
Expand Down
4 changes: 2 additions & 2 deletions src/components/post/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ import {
Tag,
} from '@/backend/post'
import { preUploadPhoto, uploadPhoto } from '@/backend/photos'
import { isValidFileType } from '@/backend/utilities/helpers'
import { validMimeTypes } from '@/backend/utilities/helpers'
import textLimits from '@/backend/utilities/text_limits'
interface IData {
Expand Down Expand Up @@ -484,7 +484,7 @@ export default Vue.extend({
return pastedContent
},
async handleFile(file: File) {
if (!isValidFileType(file.type)) {
if (!validMimeTypes.includes(file.type)) {
this.$toastError(`image of type ${file.type} is invalid`)
return
}
Expand Down

0 comments on commit d06e87f

Please sign in to comment.