Useful methods for file-related functions.
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.

42 lines
1.1 KiB

  1. import NodeBlob from 'node-blob'
  2. import isBrowser from './isBrowser'
  3. interface NodeFileConfig {
  4. type: string
  5. lastModified?: number
  6. }
  7. class File extends NodeBlob {
  8. public readonly name: string
  9. public readonly lastModified: number
  10. constructor(blobParts: unknown[], name: string, config: Partial<NodeFileConfig>) {
  11. super(blobParts, config)
  12. this.name = name
  13. switch (typeof config.lastModified!) {
  14. case 'number':
  15. if (isNaN(config.lastModified as number)) {
  16. break
  17. }
  18. this.lastModified = Math.floor(config.lastModified as number)
  19. return
  20. case 'object':
  21. // force native Date objects only!
  22. if (((config.lastModified as unknown) as Record<string, unknown>).constructor.name !== 'Date') {
  23. break
  24. }
  25. const tryDate = new Date(config.lastModified!).getTime()
  26. if (isNaN(tryDate)) {
  27. break
  28. }
  29. this.lastModified = tryDate
  30. return
  31. default:
  32. break
  33. }
  34. this.lastModified = 0
  35. }
  36. }
  37. export default isBrowser() ? window.File : File