|
| 1 | +import { Memoize } from 'typescript-memoize'; |
| 2 | +import { |
| 3 | + Byte, |
| 4 | + ByteParser, |
| 5 | + Duration, |
| 6 | + DurationParser, |
| 7 | + NumberParser, |
| 8 | +} from '@internetarchive/field-parsers'; |
| 9 | + |
| 10 | +/** |
| 11 | + * This represents an Internet Archive File |
| 12 | + * |
| 13 | + * @export |
| 14 | + * @class File |
| 15 | + */ |
| 16 | +export class File { |
| 17 | + /* eslint-disable-next-line @typescript-eslint/no-explicit-any */ |
| 18 | + readonly rawValue: Readonly<Record<string, any>>; |
| 19 | + |
| 20 | + get name(): string { |
| 21 | + return this.rawValue.name; |
| 22 | + } |
| 23 | + |
| 24 | + get source(): string { |
| 25 | + return this.rawValue.source; |
| 26 | + } |
| 27 | + |
| 28 | + get btih(): string { |
| 29 | + return this.rawValue.btih; |
| 30 | + } |
| 31 | + |
| 32 | + get md5(): string { |
| 33 | + return this.rawValue.md5; |
| 34 | + } |
| 35 | + |
| 36 | + get format(): string { |
| 37 | + return this.rawValue.format; |
| 38 | + } |
| 39 | + |
| 40 | + @Memoize() get mtime(): Date | undefined { |
| 41 | + if (this.rawValue.mtime == null) { |
| 42 | + return undefined; |
| 43 | + } |
| 44 | + const numberValue = NumberParser.shared.parseValue(this.rawValue.mtime); |
| 45 | + if (numberValue) { |
| 46 | + return new Date(numberValue * 1000); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + get crc32(): string { |
| 51 | + return this.rawValue.crc32; |
| 52 | + } |
| 53 | + |
| 54 | + get sha1(): string { |
| 55 | + return this.rawValue.sha1; |
| 56 | + } |
| 57 | + |
| 58 | + get original(): string | undefined { |
| 59 | + return this.rawValue.original; |
| 60 | + } |
| 61 | + |
| 62 | + @Memoize() get size(): Byte | undefined { |
| 63 | + return this.rawValue.size != null |
| 64 | + ? ByteParser.shared.parseValue(this.rawValue.size) |
| 65 | + : undefined; |
| 66 | + } |
| 67 | + |
| 68 | + get title(): string | undefined { |
| 69 | + return this.rawValue.title; |
| 70 | + } |
| 71 | + |
| 72 | + @Memoize() get length(): Duration | undefined { |
| 73 | + return this.rawValue.length != null |
| 74 | + ? DurationParser.shared.parseValue(this.rawValue.length) |
| 75 | + : undefined; |
| 76 | + } |
| 77 | + |
| 78 | + @Memoize() get height(): number | undefined { |
| 79 | + return this.rawValue.height != null |
| 80 | + ? NumberParser.shared.parseValue(this.rawValue.height) |
| 81 | + : undefined; |
| 82 | + } |
| 83 | + |
| 84 | + @Memoize() get width(): number | undefined { |
| 85 | + return this.rawValue.width != null |
| 86 | + ? NumberParser.shared.parseValue(this.rawValue.width) |
| 87 | + : undefined; |
| 88 | + } |
| 89 | + |
| 90 | + @Memoize() get track(): number | undefined { |
| 91 | + return this.rawValue.track != null |
| 92 | + ? NumberParser.shared.parseValue(this.rawValue.track) |
| 93 | + : undefined; |
| 94 | + } |
| 95 | + |
| 96 | + get external_identifier(): string | string[] | undefined { |
| 97 | + return this.rawValue.external_identifier; |
| 98 | + } |
| 99 | + |
| 100 | + get creator(): string | undefined { |
| 101 | + return this.rawValue.creator; |
| 102 | + } |
| 103 | + |
| 104 | + get album(): string | undefined { |
| 105 | + return this.rawValue.album; |
| 106 | + } |
| 107 | + |
| 108 | + /* eslint-disable-next-line @typescript-eslint/no-explicit-any */ |
| 109 | + constructor(json: Record<string, any> = {}) { |
| 110 | + this.rawValue = json; |
| 111 | + } |
| 112 | +} |
0 commit comments