[ABANDONED] API server for Flexor social network.
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.
 

45 lines
1.8 KiB

import { Container } from '@azure/cosmos'
import { ContainerSASPermissions, generateBlobSASQueryParameters, StorageSharedKeyCredential, BlobServiceClient } from '@azure/storage-blob'
import moment from 'moment'
import { MEDIA_PARTITION_KEY } from '../constants'
import { Media } from '../types/collections'
export const getBlobUrl = () => `https://${process.env.BLOB_STORAGE_ACCOUNT!}.blob.core.windows.net/${process.env.BLOB_STORAGE_CONTAINER!}`
export function generateSAS(permissions: string, expirationMinutes: number) {
const sharedKeyCredential = new StorageSharedKeyCredential(process.env.BLOB_STORAGE_ACCOUNT!, process.env.BLOB_STORAGE_ACCOUNT_KEY!)
return generateBlobSASQueryParameters({
containerName: process.env.BLOB_STORAGE_CONTAINER!,
permissions: ContainerSASPermissions.parse(permissions),
startsOn: moment().subtract(1, 'm').toDate(),
expiresOn: moment().add(expirationMinutes, 'm').toDate(),
}, sharedKeyCredential).toString()
}
export async function deleteMedia(name: string) {
const blobServiceClient = new BlobServiceClient(
`${getBlobUrl()}/${name}`,
new StorageSharedKeyCredential(process.env.BLOB_STORAGE_ACCOUNT!, process.env.BLOB_STORAGE_ACCOUNT_KEY!)
)
const containerClient = blobServiceClient.getContainerClient(process.env.BLOB_STORAGE_CONTAINER!)
containerClient.deleteBlob(name)
try {
await containerClient.deleteBlob(name)
} catch (err) {
console.error(err)
}
}
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,
})
}