asset-utils.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { AssetEntity } from '@app/infra';
  2. import { AssetResponseDto } from '@app/domain';
  3. import fs from 'fs';
  4. const deleteFiles = (asset: AssetEntity | AssetResponseDto) => {
  5. fs.unlink(asset.originalPath, (err) => {
  6. if (err) {
  7. console.log('error deleting ', asset.originalPath);
  8. }
  9. });
  10. // TODO: what if there is no asset.resizePath. Should fail the Job?
  11. // => panoti report: Job not fail
  12. if (asset.resizePath) {
  13. fs.unlink(asset.resizePath, (err) => {
  14. if (err) {
  15. console.log('error deleting ', asset.resizePath);
  16. }
  17. });
  18. }
  19. if (asset.webpPath) {
  20. fs.unlink(asset.webpPath, (err) => {
  21. if (err) {
  22. console.log('error deleting ', asset.webpPath);
  23. }
  24. });
  25. }
  26. if (asset.encodedVideoPath) {
  27. fs.unlink(asset.encodedVideoPath, (err) => {
  28. if (err) {
  29. console.log('error deleting ', asset.encodedVideoPath);
  30. }
  31. });
  32. }
  33. };
  34. const isWebPlayable = (mimeType: string | null): boolean => {
  35. const WEB_PLAYABLE = ['video/webm', 'video/mp4'];
  36. if (mimeType !== null) {
  37. return WEB_PLAYABLE.includes(mimeType);
  38. }
  39. return false;
  40. };
  41. export const assetUtils = { deleteFiles, isWebPlayable };