Skip to content

Commit

Permalink
Merge branch 'master' of github.com:supabase/storage into feat/tus-st…
Browse files Browse the repository at this point in the history
…able-v1
  • Loading branch information
fenos committed Dec 27, 2023
2 parents f4f1d2f + b8ac8f6 commit 1c8fd48
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 10 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ jobs:
- name: Prettier checks
run: |
npm run format
git diff
npm run prettier:check
- name: ESLint checks
Expand Down
2 changes: 1 addition & 1 deletion src/http/routes/tus/file-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Upload } from '@tus/server'
import fsExtra from 'fs-extra'
import path from 'path'
import { FileBackend } from '../../../storage/backend'
import { Configstore } from '@tus/file-store/configstores'
import { Configstore } from '@tus/file-store'

type FileStoreOptions = {
directory: string
Expand Down
36 changes: 33 additions & 3 deletions src/http/routes/tus/s3-store.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { S3Store as BaseS3Store } from '@tus/s3-store'
import { Upload } from '@tus/server'
import { S3 } from '@aws-sdk/client-s3'
import { TUS_RESUMABLE, Upload } from '@tus/server'
import AWS, { S3 } from '@aws-sdk/client-s3'

type MetadataValue = {
file: Upload
Expand All @@ -10,11 +10,41 @@ type MetadataValue = {

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore - overwriting private getMetadata function for backwards compatibility
// TODO: remove this class after all tenants are migrated to the new tus server version
export class S3Store extends BaseS3Store {
public async create(upload: Upload) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const bucket = this.bucket as string
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const client = this.client as S3

const request: AWS.CreateMultipartUploadCommandInput = {
Bucket: bucket,
Key: upload.id,
Metadata: { 'tus-version': TUS_RESUMABLE },
}

if (upload.metadata?.contentType) {
request.ContentType = upload.metadata.contentType
}

if (upload.metadata?.cacheControl) {
request.CacheControl = upload.metadata.cacheControl
}

upload.creation_date = new Date().toISOString()

const res = await client.createMultipartUpload(request)
await (this as any).saveMetadata(upload, res.UploadId as string)

return upload
}

/**
* Get the metadata for a file.
* It keeps backwards compatibility from version 0.9 to 1.0.0
* TODO: remove this after all tenants are migrated to the new tus server version
* @param id
*/
private async getMetadata(id: string): Promise<MetadataValue> {
Expand Down
12 changes: 10 additions & 2 deletions src/storage/uploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,11 @@ export class Uploader {
const requestedMime = mimeType.split('/')

if (requestedMime.length < 2) {
throw new StorageBackendError('invalid_mime_type', 422, 'mime type is not formatted properly')
throw new StorageBackendError(
'invalid_mime_type',
422,
`mime type ${mimeType} is not formatted properly`
)
}

const [type, ext] = requestedMime
Expand All @@ -231,7 +235,11 @@ export class Uploader {
}
}

throw new StorageBackendError('invalid_mime_type', 422, 'mime type not supported')
throw new StorageBackendError(
'invalid_mime_type',
422,
`mime type ${mimeType} is not supported`
)
}

protected async incomingFileInfo(
Expand Down
26 changes: 25 additions & 1 deletion src/test/object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,31 @@ describe('testing POST object via multipart upload', () => {
expect(response.statusCode).toBe(400)
expect(await response.json()).toEqual({
error: 'invalid_mime_type',
message: 'mime type not supported',
message: `mime type image/png is not supported`,
statusCode: '422',
})
expect(S3Backend.prototype.uploadObject).not.toHaveBeenCalled()
})

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`))
const headers = Object.assign({}, form.getHeaders(), {
authorization: `Bearer ${serviceKey}`,
'x-upsert': 'true',
'content-type': 'thisisnotarealmimetype',
})

const response = await app().inject({
method: 'POST',
url: '/object/public-limit-mime-types/sadcat-upload23.png',
headers,
payload: form,
})
expect(response.statusCode).toBe(400)
expect(await response.json()).toEqual({
error: 'invalid_mime_type',
message: `mime type thisisnotarealmimetype is not formatted properly`,
statusCode: '422',
})
expect(S3Backend.prototype.uploadObject).not.toHaveBeenCalled()
Expand Down
2 changes: 1 addition & 1 deletion src/test/tus.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ describe('Tus multipart', () => {
expect(e).toBeInstanceOf(DetailedError)

const err = e as DetailedError
expect(err.originalResponse.getBody()).toEqual('Request Entity Too Large\n')
expect(err.originalResponse.getBody()).toEqual('Maximum size exceeded\n')
expect(err.originalResponse.getStatus()).toEqual(413)
}
})
Expand Down

0 comments on commit 1c8fd48

Please sign in to comment.