Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: ensure consistent filesize const #2079

Merged
merged 2 commits into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/app/constants/filesize.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/app/models/form.server.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import mongoose, {
} from 'mongoose'
import validator from 'validator'

import { MB } from '../../shared/constants'
import { reorder } from '../../shared/util/immutable-array-fns'
import {
AuthType,
Expand Down Expand Up @@ -41,7 +42,6 @@ import {
Status,
} from '../../types'
import { IPopulatedUser, IUserSchema } from '../../types/user'
import { MB } from '../constants/filesize'
import { OverrideProps } from '../modules/form/admin-form/admin-form.types'
import { getFormFieldById, transformEmails } from '../modules/form/form.utils'
import { validateWebhookUrl } from '../modules/webhook/webhook.validation'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { IncomingHttpHeaders } from 'http'
import { pick } from 'lodash'
import { mocked } from 'ts-jest/utils'

import { MB } from 'src/app/constants/filesize'
import { MB } from 'src/shared/constants'
import { BasicField } from 'src/types'

import {
Expand Down Expand Up @@ -52,8 +52,8 @@ describe('email-submission.receiver', () => {
expect(MockBusboy).toHaveBeenCalledWith({
headers: MOCK_HEADERS,
limits: {
fieldSize: 3 * 1048576,
fileSize: 7 * 1048576,
fieldSize: 3 * MB,
fileSize: 7 * MB,
},
})
expect(result._unsafeUnwrap()).toEqual(MOCK_BUSBOY)
Expand All @@ -71,8 +71,8 @@ describe('email-submission.receiver', () => {
expect(MockBusboy).toHaveBeenCalledWith({
headers: MOCK_HEADERS,
limits: {
fieldSize: 3 * 1048576,
fileSize: 7 * 1048576,
fieldSize: 3 * MB,
fileSize: 7 * MB,
},
})
expect(result._unsafeUnwrapErr()).toEqual(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import Busboy from 'busboy'
import { IncomingHttpHeaders } from 'http'
import { err, ok, Result, ResultAsync } from 'neverthrow'

import { MB } from '../../../../shared/constants'
import { IAttachmentInfo } from '../../../../types'
import { createLoggerWithLabel } from '../../../config/logger'
import { MB } from '../../../constants/filesize'

import {
InitialiseMultipartReceiverError,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ProcessedAttachmentResponse } from 'src/app/modules/submission/submissi
import { IAttachmentField } from 'src/types/field'
import { ResponseValidator } from 'src/types/field/utils/validation'

const MILLION = 1000000
import { MB } from '../../../../shared/constants'

type AttachmentValidator = ResponseValidator<ProcessedAttachmentResponse>
type AttachmentValidatorConstructor = (
Expand Down Expand Up @@ -41,7 +41,7 @@ const attachmentContentValidator: AttachmentValidator = (response) => {
const makeAttachmentSizeValidator: AttachmentValidatorConstructor =
(attachmentField) => (response) => {
const { attachmentSize } = attachmentField
const byteSizeLimit = parseInt(attachmentSize) * MILLION
const byteSizeLimit = parseInt(attachmentSize) * MB
return response.content.byteLength < byteSizeLimit
? right(response)
: left(`AttachmentValidator:\t File size more than limit`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const cloneDeep = require('lodash/cloneDeep')
const {
VALID_UPLOAD_FILE_TYPES,
MAX_UPLOAD_FILE_SIZE,
MB,
} = require('shared/constants')
const { UPDATE_FORM_TYPES } = require('../constants/update-form-types')
const { uploadImage } = require('../../../../services/FileHandlerService')
Expand Down Expand Up @@ -559,8 +560,8 @@ function EditFieldsModalController(
field.uploadedFile = ''
switch (ngfError.$error) {
case 'maxSize':
vm.uploadError = `${(ngfError.size / 1000000).toFixed(2)} MB / ${
vm.maxImageSize / 1000000
vm.uploadError = `${(ngfError.size / MB).toFixed(2)} MB / ${
vm.maxImageSize / MB
karrui marked this conversation as resolved.
Show resolved Hide resolved
} MB: File size exceeded`
break
case 'resize':
Expand All @@ -585,7 +586,7 @@ function EditFieldsModalController(
field.url = result.url
field.fileMd5Hash = result.fileMd5Hash
field.name = result.name
field.size = `${(result.size / 1000000).toFixed(2)} MB`
field.size = `${(result.size / MB).toFixed(2)} MB`
})
.catch((uploadError) => {
// This is a reference to the ng-model of the upload button, which points to the uploaded file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const axios = require('axios').default
const {
MAX_UPLOAD_FILE_SIZE,
VALID_UPLOAD_FILE_TYPES,
MB,
} = require('shared/constants')
const { uploadLogo } = require('../../../../services/FileHandlerService')
const { FormLogoState } = require('../../../../../types')
Expand Down Expand Up @@ -32,6 +33,7 @@ function EditStartPageController(

vm.maxLogoSize = MAX_UPLOAD_FILE_SIZE
vm.validLogoExtensions = VALID_UPLOAD_FILE_TYPES
vm.MB = MB
vm.FormLogoState = FormLogoState

vm.logoUrl = getFormLogo(myform)
Expand Down Expand Up @@ -116,8 +118,8 @@ function EditStartPageController(
vm.uploaded.file = ''
switch (ngfError.$error) {
case 'maxSize':
vm.uploadError = `${(ngfError.size / 1000000).toFixed(2)} MB / ${
vm.maxLogoSize / 1000000
vm.uploadError = `${(ngfError.size / MB).toFixed(2)} MB / ${
vm.maxLogoSize / MB
karrui marked this conversation as resolved.
Show resolved Hide resolved
} MB: File size exceeded`
break
case 'resize':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ <h2 class="modal-title">Edit Welcome</h2>
>
<span id="image-size"
>{{
(vm.myform.startPage.logo.fileSizeInBytes/1000000).toFixed(2)
(vm.myform.startPage.logo.fileSizeInBytes/vm.MB).toFixed(2)
}} MB</span
>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
ngf-resize="{width: 1024, centerCrop: false}"
ngf-validate-after-resize="true"
ngf-max-files="1"
ngf-max-size="vm.field.attachmentSize * 1000000"
ngf-max-size="{{ vm.field.attachmentSize * vm.MB }}"
ngf-select="vm.uploadFile($file, $invalidFiles, vm.field._id)"
ng-model="vm.field.fieldValue"
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const {
isInvalidFileExtension,
getInvalidFileExtensionsInZip,
} = require('../../../../../shared/util/file-validation')
const { FilePlatforms } = require('../../../../../shared/constants')
const { FilePlatforms, KB, MB } = require('../../../../../shared/constants')

angular.module('forms').component('attachmentFieldComponent', {
templateUrl:
Expand All @@ -22,6 +22,8 @@ angular.module('forms').component('attachmentFieldComponent', {
function attachmentFieldComponentController($timeout) {
const vm = this

vm.MB = MB
mantariksh marked this conversation as resolved.
Show resolved Hide resolved

vm.isAttachmentTouched = false
vm.touchAttachment = function () {
vm.isAttachmentTouched = true
Expand All @@ -37,7 +39,7 @@ function attachmentFieldComponentController($timeout) {
if (errFiles.length > 0) {
err = errFiles[0].$error
if (err === 'maxSize') {
const currentSize = (errFiles[0].size / 1000000).toFixed(2)
const currentSize = (errFiles[0].size / MB).toFixed(2)
showAttachmentError(
`${currentSize} MB / ${vm.field.attachmentSize} MB: File size exceeded`,
)
Expand Down Expand Up @@ -136,11 +138,11 @@ function attachmentFieldComponentController($timeout) {
vm.fileAttached = true
vm.fileError = false
vm.fileName = file.name
vm.fileSize = file.size / 1000
if (file.size / 1000 > 1000) {
vm.fileSize = String((file.size / 1000000).toFixed(2)) + ' MB'
vm.fileSize = file.size / KB
if (file.size / KB > KB) {
vm.fileSize = String((file.size / MB).toFixed(2)) + ' MB'
} else {
vm.fileSize = String((file.size / 1000).toFixed(2)) + ' KB'
vm.fileSize = String((file.size / KB).toFixed(2)) + ' KB'
}
vm.isLoading = false
})
Expand Down
5 changes: 4 additions & 1 deletion src/shared/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ export enum DateSelectedValidation {
// File types that can be uploaded for form image/logo
export const VALID_UPLOAD_FILE_TYPES = ['image/jpeg', 'image/png', 'image/gif']

export const KB = 1000
export const MB = 1000 * KB

// Define max file size as 2MB
export const MAX_UPLOAD_FILE_SIZE = 2 * 1000 * 1000 // 2 Million/Mega Bytes, or 2 MB
export const MAX_UPLOAD_FILE_SIZE = 2 * MB // 2 Million/Mega Bytes, or 2 MB

export const LINKS = {
supportFormLink: 'https://go.gov.sg/formsg-support',
Expand Down