Skip to content

Commit

Permalink
fix: allow creating folders even if the mime type validation is set (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
fenos authored Jun 26, 2024
1 parent 6dda887 commit c2681b8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/storage/limits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,7 @@ export function parseFileSizeToBytes(valueWithUnit: string) {
export function isUuid(value: string) {
return /^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(value)
}

export function isEmptyFolder(object: string) {
return object.endsWith('.emptyFolderPlaceholder')
}
9 changes: 7 additions & 2 deletions src/storage/uploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ERRORS } from '@internal/errors'
import { FileUploadedSuccess, FileUploadStarted } from '@internal/monitoring/metrics'

import { ObjectMetadata, StorageBackendAdapter } from './backend'
import { getFileSizeLimit } from './limits'
import { getFileSizeLimit, isEmptyFolder } from './limits'
import { Database } from './database'
import { ObjectAdminDelete, ObjectCreatedPostEvent, ObjectCreatedPutEvent } from './events'
import { getConfig } from '../config'
Expand Down Expand Up @@ -81,10 +81,15 @@ export class Uploader {
async upload(request: FastifyRequest, options: UploaderOptions) {
const version = await this.prepareUpload(options)

// When is an empty folder we restrict it to 0 bytes
if (isEmptyFolder(options.objectName)) {
options.fileSizeLimit = 0
}

try {
const file = await this.incomingFileInfo(request, options)

if (options.allowedMimeTypes) {
if (options.allowedMimeTypes && !isEmptyFolder(options.objectName)) {
this.validateMimeType(file.mimeType, options.allowedMimeTypes)
}

Expand Down
37 changes: 37 additions & 0 deletions src/test/object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,43 @@ describe('testing POST object via multipart upload', () => {
expect(S3Backend.prototype.uploadObject).not.toHaveBeenCalled()
})

test('can create an empty folder when mime-type is set', async () => {
const form = new FormData()
const headers = Object.assign({}, form.getHeaders(), {
authorization: `Bearer ${serviceKey}`,
'x-upsert': 'true',
})

form.append('file', Buffer.alloc(0))

const response = await app().inject({
method: 'POST',
url: '/object/public-limit-mime-types/nested/.emptyFolderPlaceholder',
headers,
payload: form,
})
expect(response.statusCode).toBe(200)
expect(S3Backend.prototype.uploadObject).toHaveBeenCalled()
})

test('cannot create an empty folder with more than 0kb', async () => {
const form = new FormData()
const headers = Object.assign({}, form.getHeaders(), {
authorization: `Bearer ${serviceKey}`,
'x-upsert': 'true',
})

form.append('file', Buffer.alloc(1))

const response = await app().inject({
method: 'POST',
url: '/object/public-limit-mime-types/nested-2/.emptyFolderPlaceholder',
headers,
payload: form,
})
expect(response.statusCode).toBe(400)
})

test('return 422 when uploading an object with a malformed mime-type', async () => {
const form = new FormData()
form.append('file', fs.createReadStream(`./src/test/assets/sadcat.jpg`))
Expand Down

0 comments on commit c2681b8

Please sign in to comment.