[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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. import { Container } from '@azure/cosmos'
  2. import { ContainerSASPermissions, generateBlobSASQueryParameters, StorageSharedKeyCredential, BlobServiceClient } from '@azure/storage-blob'
  3. import moment from 'moment'
  4. import { MEDIA_PARTITION_KEY } from '../constants'
  5. import { Media } from '../types/collections'
  6. export const getBlobUrl = () => `https://${process.env.BLOB_STORAGE_ACCOUNT!}.blob.core.windows.net/${process.env.BLOB_STORAGE_CONTAINER!}`
  7. export function generateSAS(permissions: string, expirationMinutes: number) {
  8. const sharedKeyCredential = new StorageSharedKeyCredential(process.env.BLOB_STORAGE_ACCOUNT!, process.env.BLOB_STORAGE_ACCOUNT_KEY!)
  9. return generateBlobSASQueryParameters({
  10. containerName: process.env.BLOB_STORAGE_CONTAINER!,
  11. permissions: ContainerSASPermissions.parse(permissions),
  12. startsOn: moment().subtract(1, 'm').toDate(),
  13. expiresOn: moment().add(expirationMinutes, 'm').toDate(),
  14. }, sharedKeyCredential).toString()
  15. }
  16. export async function deleteMedia(name: string) {
  17. const blobServiceClient = new BlobServiceClient(
  18. `${getBlobUrl()}/${name}`,
  19. new StorageSharedKeyCredential(process.env.BLOB_STORAGE_ACCOUNT!, process.env.BLOB_STORAGE_ACCOUNT_KEY!)
  20. )
  21. const containerClient = blobServiceClient.getContainerClient(process.env.BLOB_STORAGE_CONTAINER!)
  22. containerClient.deleteBlob(name)
  23. try {
  24. await containerClient.deleteBlob(name)
  25. } catch (err) {
  26. console.error(err)
  27. }
  28. }
  29. export async function attachMedia(container: Container, name: string) {
  30. const mediaItem = container.item(name, MEDIA_PARTITION_KEY)
  31. const { resource: media } = await mediaItem.read<Media>()
  32. await mediaItem.replace<Media>({
  33. ...media,
  34. attached: true,
  35. })
  36. }