Skip to content

Commit

Permalink
feat: add 300mb file upload limit for admins
Browse files Browse the repository at this point in the history
  • Loading branch information
iSCJT authored and benfurber committed Oct 30, 2024
1 parent 0d97970 commit 0c91bb6
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 12 deletions.
5 changes: 4 additions & 1 deletion src/common/Form/FileInput.field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import { FileInput } from './FileInput/FileInput'

import type { FieldProps } from './types'

export const FileInputField = ({ input, ...rest }: FieldProps) => (
export const FileInputField = ({
input,
...rest
}: FieldProps & { admin: boolean }) => (
<FileInput
{...rest}
onFilesChange={(files) => {
Expand Down
9 changes: 8 additions & 1 deletion src/common/Form/FileInput/FileInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Button, DownloadStaticFile } from 'oa-components'
import { Flex } from 'theme-ui'

import { UPPY_CONFIG } from './UppyConfig'
import { UPPY_CONFIG_ADMIN } from './UppyConfigAdmin'

import type { UppyFile } from '@uppy/core'

Expand All @@ -18,14 +19,20 @@ interface IUppyFiles {
interface IProps {
onFilesChange?: (files: (Blob | File)[]) => void
'data-cy'?: string
admin: boolean
}
interface IState {
open: boolean
}
export const FileInput = (props: IProps) => {
const [state, setState] = useState<IState>({ open: false })
const uploadConfig = props.admin ? UPPY_CONFIG_ADMIN : UPPY_CONFIG
const [uppy] = useState(
() => new Uppy({ ...UPPY_CONFIG, onBeforeUpload: () => uploadTriggered() }),
() =>
new Uppy({
...uploadConfig,
onBeforeUpload: () => uploadTriggered(),
}),
)

useEffect(() => {
Expand Down
26 changes: 26 additions & 0 deletions src/common/Form/FileInput/UppyConfigAdmin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { UppyOptions } from '@uppy/core'

export const UPPY_CONFIG_ADMIN: Partial<UppyOptions> = {
restrictions: {
// max upload file size in bytes (i.e. 300 x 1048576 => 300 MB)
maxFileSize: 300 * 1048576,
maxNumberOfFiles: 5,
minNumberOfFiles: null,
allowedFileTypes: null,
},
locale: {
strings: {
youCanOnlyUploadX: {
0: 'You can only upload %{smart_count} file',
1: 'You can only upload %{smart_count} files',
},
youHaveToAtLeastSelectX: {
0: 'You have to select at least %{smart_count} file',
1: 'You have to select at least %{smart_count} files',
},
exceedsSize: 'This file exceeds maximum allowed size of %{size}',
youCanOnlyUploadFileTypes: 'You can only upload: %{types}',
companionError: 'Connection with Companion failed',
},
},
}
37 changes: 31 additions & 6 deletions src/pages/Howto/Content/Common/HowtoFieldFileUpload.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Field } from 'react-final-form'
import { UserRole } from 'oa-shared'
import { AuthWrapper } from 'src/common/AuthWrapper'
import { FileInputField } from 'src/common/Form/FileInput.field'
import { FormFieldWrapper } from 'src/pages/Howto/Content/Common/FormFieldWrapper'
import { Text } from 'theme-ui'
Expand All @@ -10,11 +12,34 @@ export const HowtoFieldFileUpload = () => {
const name = 'files'

return (
<FormFieldWrapper htmlFor={name} text={title}>
<Field id={name} name={name} data-cy={name} component={FileInputField} />
<Text color={'grey'} mt={4} sx={{ fontSize: 1 }}>
{description}
</Text>
</FormFieldWrapper>
<AuthWrapper
roleRequired={UserRole.ADMIN}
fallback={
<FormFieldWrapper htmlFor={name} text={title}>
<Field
id={name}
name={name}
data-cy={name}
component={FileInputField}
/>
<Text color={'grey'} mt={4} sx={{ fontSize: 1 }}>
{description}
</Text>
</FormFieldWrapper>
}
>
<FormFieldWrapper htmlFor={name} text={title}>
<Field
id={name}
name={name}
data-cy={name}
admin={true}
component={FileInputField}
/>
<Text color={'grey'} mt={4} sx={{ fontSize: 1 }}>
{'Maximum file size 300MB'}
</Text>
</FormFieldWrapper>
</AuthWrapper>
)
}
33 changes: 29 additions & 4 deletions src/pages/Research/Content/CreateResearch/Form/FilesFields.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as React from 'react'
import { Field } from 'react-final-form'
import { Button, DownloadStaticFile, FieldInput } from 'oa-components'
import { UserRole } from 'oa-shared'
import { AuthWrapper } from 'src/common/AuthWrapper'
import { FileInputField } from 'src/common/Form/FileInput.field'
import { MAX_LINK_LENGTH } from 'src/pages/constants'
import { buttons, update as updateLabels } from 'src/pages/Research/labels'
Expand Down Expand Up @@ -77,10 +79,33 @@ const UploadNewFiles = () => {
<Label mb={2} htmlFor={identity} style={{ fontSize: '12px' }}>
{files.title}
</Label>
<Field hasText={false} name={'files'} component={FileInputField} />
<Text color={'grey'} mt={4} sx={{ fontSize: 1 }}>
{files.description}
</Text>
<AuthWrapper
roleRequired={UserRole.ADMIN}
fallback={
<>
<Field
hasText={false}
name={'files'}
component={FileInputField}
/>
<Text color={'grey'} mt={4} sx={{ fontSize: 1 }}>
{files.description}
</Text>
</>
}
>
<>
<Field
hasText={false}
name={'files'}
admin={true}
component={FileInputField}
/>
<Text color={'grey'} mt={4} sx={{ fontSize: 1 }}>
{'Maximum file size 300MB'}
</Text>
</>
</AuthWrapper>
</Flex>
</>
)
Expand Down

0 comments on commit 0c91bb6

Please sign in to comment.