You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
1.5 KiB
36 lines
1.5 KiB
import { Container } from '@azure/cosmos'
|
|
import { BlockBlobURL, SharedKeyCredential, Aborter, ContainerSASPermissions, generateBlobSASQueryParameters, AnonymousCredential } from '@azure/storage-blob'
|
|
import moment from 'moment'
|
|
|
|
import { MEDIA_PARTITION_KEY } from '../constants'
|
|
import { Media } from '../types/collections'
|
|
|
|
export function generateSAS(permissions: string, expirationMinutes: number) {
|
|
const sharedKeyCredential = new SharedKeyCredential(process.env.BLOB_STORAGE_ACCOUNT!, process.env.BLOB_STORAGE_ACCOUNT_KEY!)
|
|
|
|
return generateBlobSASQueryParameters({
|
|
containerName: process.env.BLOB_STORAGE_CONTAINER!,
|
|
permissions: ContainerSASPermissions.parse(permissions).toString(),
|
|
startTime: new Date(),
|
|
expiryTime: moment().add(expirationMinutes, 'm').toDate(),
|
|
}, sharedKeyCredential).toString()
|
|
}
|
|
|
|
export async function deleteMedia(name: string) {
|
|
const blockBlobURL = new BlockBlobURL(
|
|
`https://${process.env.BLOB_STORAGE_ACCOUNT!}.blob.core.windows.net/${process.env.BLOB_STORAGE_CONTAINER!}/${name}`,
|
|
BlockBlobURL.newPipeline(new SharedKeyCredential(process.env.BLOB_STORAGE_ACCOUNT!, process.env.BLOB_STORAGE_ACCOUNT_KEY!))
|
|
)
|
|
|
|
await blockBlobURL.delete(Aborter.none)
|
|
}
|
|
|
|
export async function attachMedia(container: Container, name: string) {
|
|
const mediaItem = container.item(name, MEDIA_PARTITION_KEY)
|
|
const { resource: media } = await mediaItem.read<Media>()
|
|
|
|
await mediaItem.replace<Media>({
|
|
...media,
|
|
attached: true,
|
|
})
|
|
}
|