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

patch: return file id on upload #332

Merged
merged 1 commit into from
May 19, 2023
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
6 changes: 5 additions & 1 deletion src/http/routes/object/createObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ const createObjectParamsSchema = {
const successResponseSchema = {
type: 'object',
properties: {
Id: {
type: 'string',
},
Key: {
type: 'string',
examples: ['avatars/folder/cat.png'],
Expand Down Expand Up @@ -61,7 +64,7 @@ export default async function routes(fastify: FastifyInstance) {
const isUpsert = request.headers['x-upsert'] === 'true'
const owner = request.owner as string

const { objectMetadata, path } = await request.storage
const { objectMetadata, path, id } = await request.storage
.from(bucketName)
.uploadNewObject(request, {
objectName,
Expand All @@ -70,6 +73,7 @@ export default async function routes(fastify: FastifyInstance) {
})

return response.status(objectMetadata?.httpStatusCode ?? 200).send({
Id: id,
Key: path,
})
}
Expand Down
6 changes: 5 additions & 1 deletion src/http/routes/object/updateObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ const updateObjectParamsSchema = {
const successResponseSchema = {
type: 'object',
properties: {
Id: {
type: 'string',
},
Key: { type: 'string', examples: ['avatars/folder/cat.png'] },
},
required: ['Key'],
Expand Down Expand Up @@ -56,14 +59,15 @@ export default async function routes(fastify: FastifyInstance) {
const objectName = request.params['*']
const owner = request.owner as string

const { objectMetadata, path } = await request.storage
const { objectMetadata, path, id } = await request.storage
.from(bucketName)
.uploadOverridingObject(request, {
owner,
objectName: objectName,
})

return response.status(objectMetadata?.httpStatusCode ?? 200).send({
Id: id,
Key: path,
})
}
Expand Down
2 changes: 1 addition & 1 deletion src/storage/database/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export interface Database {

upsertObject(
data: Pick<Obj, 'name' | 'owner' | 'bucket_id' | 'metadata' | 'version'>
): Promise<Omit<Obj, 'id'>>
): Promise<Obj>
updateObject(
bucketId: string,
name: string,
Expand Down
19 changes: 12 additions & 7 deletions src/storage/database/knex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,15 +235,20 @@ export class StorageKnexDB implements Database {
metadata: data.metadata,
version: data.version,
}
await this.runQuery('UpsertObject', (knex) => {
return knex.from<Obj>('objects').insert(objectData).onConflict(['name', 'bucket_id']).merge({
metadata: data.metadata,
version: data.version,
owner: data.owner,
})
const [object] = await this.runQuery('UpsertObject', (knex) => {
return knex
.from<Obj>('objects')
.insert(objectData)
.onConflict(['name', 'bucket_id'])
.merge({
metadata: data.metadata,
version: data.version,
owner: data.owner,
})
.returning('*')
})

return objectData
return object
}

async updateObject(
Expand Down
8 changes: 4 additions & 4 deletions src/storage/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ export class ObjectStorage {

const uploader = new Uploader(this.backend, this.db)

const { metadata } = await uploader.upload(request, {
const { metadata, obj } = await uploader.upload(request, {
...options,
bucketId: this.bucketId,
fileSizeLimit: bucket.file_size_limit,
allowedMimeTypes: bucket.allowed_mime_types,
})

return { objectMetadata: metadata, path }
return { objectMetadata: metadata, path, id: obj.id }
}

public async uploadOverridingObject(request: FastifyRequest, options: UploadObjectOptions) {
Expand All @@ -93,15 +93,15 @@ export class ObjectStorage {

const uploader = new Uploader(this.backend, this.db)

const { metadata } = await uploader.upload(request, {
const { metadata, obj } = await uploader.upload(request, {
...options,
bucketId: this.bucketId,
fileSizeLimit: bucket.file_size_limit,
allowedMimeTypes: bucket.allowed_mime_types,
isUpsert: true,
})

return { objectMetadata: metadata, path }
return { objectMetadata: metadata, path, id: obj.id }
}

/**
Expand Down
28 changes: 24 additions & 4 deletions src/test/object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,12 @@ describe('testing POST object via multipart upload', () => {
})
expect(response.statusCode).toBe(200)
expect(S3Backend.prototype.uploadObject).toBeCalled()
expect(response.body).toBe(`{"Key":"bucket2/authenticated/casestudy1.png"}`)
expect(await response.json()).toEqual(
expect.objectContaining({
Id: expect.any(String),
Key: 'bucket2/authenticated/casestudy1.png',
})
)
})

test('check if RLS policies are respected: anon user is not able to upload authenticated resource', async () => {
Expand Down Expand Up @@ -500,7 +505,12 @@ describe('testing POST object via binary upload', () => {
})
expect(response.statusCode).toBe(200)
expect(S3Backend.prototype.uploadObject).toBeCalled()
expect(response.body).toBe(`{"Key":"bucket2/authenticated/binary-casestudy1.png"}`)
expect(await response.json()).toEqual(
expect.objectContaining({
Id: expect.any(String),
Key: 'bucket2/authenticated/binary-casestudy1.png',
})
)
})

test('check if RLS policies are respected: anon user is not able to upload authenticated resource', async () => {
Expand Down Expand Up @@ -731,7 +741,12 @@ describe('testing PUT object', () => {
})
expect(response.statusCode).toBe(200)
expect(S3Backend.prototype.uploadObject).toBeCalled()
expect(response.body).toBe(`{"Key":"bucket2/authenticated/cat.jpg"}`)
expect(await response.json()).toEqual(
expect.objectContaining({
Id: expect.any(String),
Key: 'bucket2/authenticated/cat.jpg',
})
)
})

test('check if RLS policies are respected: anon user is not able to update authenticated resource', async () => {
Expand Down Expand Up @@ -826,7 +841,12 @@ describe('testing PUT object via binary upload', () => {
})
expect(response.statusCode).toBe(200)
expect(S3Backend.prototype.uploadObject).toBeCalled()
expect(response.body).toBe(`{"Key":"bucket2/authenticated/cat.jpg"}`)
expect(await response.json()).toEqual(
expect.objectContaining({
Id: expect.any(String),
Key: 'bucket2/authenticated/cat.jpg',
})
)
})

test('check if RLS policies are respected: anon user is not able to update authenticated resource', async () => {
Expand Down