Skip to content

Commit

Permalink
feat: Store Smart Wearables video preview (#675)
Browse files Browse the repository at this point in the history
* feat: Add new column video in the item table

* feat: Add new endpoint to upload videos to distinguish max file sizes

* fix: Video is optional
  • Loading branch information
kevinszuchet committed Jul 7, 2023
1 parent 78249f4 commit 2127322
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
10 changes: 10 additions & 0 deletions migrations/1688635167925_add-video-to-items.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { MigrationBuilder } from 'node-pg-migrate'
import { Item } from '../src/Item'

const tableName = Item.tableName

export const up = (pgm: MigrationBuilder) => {
pgm.addColumn(tableName, {
video: { type: 'TEXT', default: null, notNull: false },
})
}
30 changes: 26 additions & 4 deletions src/Item/Item.router.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { hashV1 } from '@dcl/hashing'
import { server } from 'decentraland-server'
import { env } from 'decentraland-commons'
import { omit } from 'decentraland-commons/dist/utils'
import { Router } from '../common/Router'
import { HTTPError, STATUS_CODES } from '../common/HTTPError'
Expand Down Expand Up @@ -28,6 +29,7 @@ import {
getPaginationParams,
} from '../Pagination/utils'
import { CurationStatus } from '../Curation'
import { MulterFile } from '../S3/types'
import { Item } from './Item.model'
import { ItemAttributes } from './Item.types'
import { areItemRepresentationsValid, upsertItemSchema } from './Item.schema'
Expand All @@ -48,6 +50,9 @@ import {
URNAlreadyInUseError,
} from './Item.errors'

export const MAX_VIDEO_SIZE =
parseInt(env.get('AWS_MAX_VIDEO_SIZE', ''), 10) || 5e7 // 50MB

export class ItemRouter extends Router {
// To be removed once we move everything to the item service
public collectionService = new CollectionService()
Expand All @@ -61,6 +66,11 @@ export class ItemRouter extends Router {
return this.itemService.isOwnedOrManagedBy(id, ethAddress)
}

private getFileStreamKey = async (file: MulterFile) => {
const hash = await hashV1(file.stream)
return new S3Content().getFileKey(hash)
}

mount() {
const withItemExists = withModelExists(Item, 'id')
const withCollectionExist = withModelExists(Collection, 'id')
Expand Down Expand Up @@ -141,10 +151,22 @@ export class ItemRouter extends Router {
withItemExists,
withItemAuthorization,
getUploader({
getFileStreamKey: async (file) => {
const hash = await hashV1(file.stream)
return new S3Content().getFileKey(hash)
},
getFileStreamKey: this.getFileStreamKey,
}).any(),
server.handleRequest(this.uploadItemFiles)
)

/**
* Upload the videos for an item
*/
this.router.post(
'/items/:id/videos',
withAuthentication,
withItemExists,
withItemAuthorization,
getUploader({
maxFileSize: MAX_VIDEO_SIZE,
getFileStreamKey: this.getFileStreamKey,
}).any(),
server.handleRequest(this.uploadItemFiles)
)
Expand Down
1 change: 1 addition & 0 deletions src/Item/Item.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type ItemAttributes = {
name: string
description: string
thumbnail: string
video?: string
eth_address: string
collection_id: string | null
blockchain_item_id: string | null
Expand Down
4 changes: 3 additions & 1 deletion src/S3/uploads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ class Storage implements multer.StorageEngine {

const [key] = await Promise.all([
this.getKey(file, req),
uploadFile(id, uploadStream, ACL.publicRead),
uploadFile(id, uploadStream, ACL.publicRead, {
ContentType: file.mimetype,
}),
])

// move file to key
Expand Down

0 comments on commit 2127322

Please sign in to comment.