Changeset View
Changeset View
Standalone View
Standalone View
keyserver/src/creators/upload-creator.js
Show All 15 Lines | |||||
export type UploadInput = { | export type UploadInput = { | ||||
name: string, | name: string, | ||||
mime: string, | mime: string, | ||||
mediaType: MediaType, | mediaType: MediaType, | ||||
buffer: Buffer, | buffer: Buffer, | ||||
dimensions: Dimensions, | dimensions: Dimensions, | ||||
loop: boolean, | loop: boolean, | ||||
encryptionKey?: string, | |||||
}; | }; | ||||
async function createUploads( | async function createUploads( | ||||
viewer: Viewer, | viewer: Viewer, | ||||
uploadInfos: $ReadOnlyArray<UploadInput>, | uploadInfos: $ReadOnlyArray<UploadInput>, | ||||
): Promise<UploadMultimediaResult[]> { | ): Promise<UploadMultimediaResult[]> { | ||||
if (!viewer.loggedIn) { | if (!viewer.loggedIn) { | ||||
throw new ServerError('not_logged_in'); | throw new ServerError('not_logged_in'); | ||||
} | } | ||||
const ids = await createIDs('uploads', uploadInfos.length); | const ids = await createIDs('uploads', uploadInfos.length); | ||||
const uploadRows = uploadInfos.map(uploadInfo => { | const uploadRows = uploadInfos.map(uploadInfo => { | ||||
const id = ids.shift(); | const id = ids.shift(); | ||||
const secret = crypto.randomBytes(8).toString('hex'); | const secret = crypto.randomBytes(8).toString('hex'); | ||||
const { dimensions, mediaType, loop } = uploadInfo; | const { dimensions, mediaType, loop, encryptionKey } = uploadInfo; | ||||
return { | return { | ||||
uploadResult: { | uploadResult: { | ||||
id, | id, | ||||
uri: getUploadURL(id, secret), | uri: getUploadURL(id, secret), | ||||
dimensions, | dimensions, | ||||
mediaType, | mediaType, | ||||
loop, | loop, | ||||
}, | }, | ||||
insert: [ | insert: [ | ||||
id, | id, | ||||
viewer.userID, | viewer.userID, | ||||
mediaType, | mediaType, | ||||
uploadInfo.name, | uploadInfo.name, | ||||
uploadInfo.mime, | uploadInfo.mime, | ||||
uploadInfo.buffer, | uploadInfo.buffer, | ||||
secret, | secret, | ||||
Date.now(), | Date.now(), | ||||
JSON.stringify({ ...dimensions, loop }), | JSON.stringify({ ...dimensions, loop, encryptionKey }), | ||||
], | ], | ||||
}; | }; | ||||
}); | }); | ||||
const insertQuery = SQL` | const insertQuery = SQL` | ||||
INSERT INTO uploads(id, uploader, type, filename, | INSERT INTO uploads(id, uploader, type, filename, | ||||
mime, content, secret, creation_time, extra) | mime, content, secret, creation_time, extra) | ||||
VALUES ${uploadRows.map(({ insert }) => insert)} | VALUES ${uploadRows.map(({ insert }) => insert)} | ||||
`; | `; | ||||
await dbQuery(insertQuery); | await dbQuery(insertQuery); | ||||
return uploadRows.map(({ uploadResult }) => uploadResult); | return uploadRows.map(({ uploadResult }) => uploadResult); | ||||
} | } | ||||
export default createUploads; | export default createUploads; |